我正在尝试打印反向链接列表。但我只获得一个价值。我哪里错了?因为我是新来的C.反向链接列表中的c
#include<stdio.h>
#include<stdlib.h>
struct itemlist
{
int value;
struct itemlist *next;
};
typedef struct itemlist item;
int main(void)
{
itemlist *curr,*head,*tail;
head=NULL;
tail=NULL;
for(int i=1;i<10;i++)
{
curr=(itemlist *)malloc(sizeof(itemlist));
curr->value=i;
curr->next=tail;
tail=curr;
if(!head)
head=curr;
}
curr=head;
while(curr)
{
printf("Curr value is:%d\n",curr->value);
curr=curr->next;
}
return 0;
}
这是C++,而不是C,因为您不需要将itemlist引用为'struct itemlist'。既然是这样,为什么不使用新的malloc? – 2012-04-27 19:55:23
@ RichardJ.RossIII:代码中有一个'typedef'。 – dirkgently 2012-04-27 19:56:00
@dirkgently是真的,但他没有使用它。 – 2012-04-27 19:57:13