请大家看一下一个c语言中的链表问题,下面的代码是有错误的!!请大家说出错误的原因,以及修改的方法!!!
#include <stdio.h>
#include <string.h>
typedef struct mylist
{
char content[8];
struct mylist * next;
}List;
List * dig();
List * newList(char * content)
{
if(strlen(content)!=7)
return NULL;
List * tmp =(List *) malloc(sizeof(List));
strcpy(tmp->content,content);
tmp->next=0;
return tmp;
}
int addtailer(List* head,List * n)
{
while((head->next) != 0)
{
head=head->next;
}
head->next = n;
}
int search(List * head)
{
List * tmp=head;
while(tmp!=NULL)
{
printf("%s",tmp->content);
tmp=tmp->next;
}
return 1;
}
int main()
{
List * head = dig();
search(head);
}
List * dig()
{
List * head = newList("abcdefg");
List * element;
int i=0;
List list;
strcpy(list.content,"ererere");
for(;i<5;i++)
{
element = newList("1234567");
addtailer(head,element);
}
addtailer(head,&list);
return head;
}