#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node* next;
} *graph;
int main(void)
{
int V,E,i,u,v;
struct node *ptr,*ptr1;
ptr=malloc(sizeof(struct node *));
ptr1=malloc(sizeof(struct node *));
scanf("%d",&V);
graph=malloc(V*(sizeof(struct node *)));
if(!graph)
printf("Not allocated");
for(i=0;i<V;i++)
{
graph[i].next=NULL;
}
scanf("%d",&E);
for(i=0;i<E;i++)
{
//printf("**\n");
scanf("%d %d",&u,&v);
ptr=malloc(sizeof(struct node *));
ptr1=malloc(sizeof(struct node *));
ptr->value=u;
ptr->next=graph[v].next;
graph[v].next=ptr;
ptr1->value=v;
ptr1->next=graph[u].next;
graph[u].next=ptr1;
}
for(i=0;i<V;i++)
{
ptr=graph[i].next;
printf("**\n");
printf("%d ===>\n",i);
while(ptr)
{
printf("%d->",ptr->value);
ptr=ptr->next;
}
printf("NULL\n");
}
}
我收到以下错误
a.out:malloc.c:2369:sysmalloc:声明`(old_top ==((((mbinptr)((((char *)&((av)-> bins [(((1)-1)* 2]))) -__
Builtin_offsetof(struct malloc_chunk,fd))))&& old_size == 0)|| ((无符号长)(old_size)> =(无符号长)
((((((__builtin_offsetof(struct malloc_chunk,fd_nextsize))+(((2 *(sizeof(size_t)))-1))&〜((2 *(sizeof(size_t)
))-1)))&&(((old_top)-> size&0x1)&&((unsigned long)old_end&pagemask)== 0)'失败。
中止(核心已弃用)
最佳答案
吸引眼球的第一件事:
struct node *ptr,*ptr1;
ptr=malloc(sizeof(struct node *));
ptr1=malloc(sizeof(struct node *));
ptr和ptr1旨在指向
struct node
,但是您为struct node *
分配了空间。 graph
的备注相同。关于c - 我正在使用邻接表实现图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26085421/