您的位置:首页 > 编程语言 > C语言/C++

一个正确的c语言链表代码(中间也有些bug)

2008-11-21 14:49 363 查看
本程序在vi编辑器里运行通过!

#include <stdio.h>
#include <string.h>

typedef struct mylist
{
char content[8];
struct mylist * next;
}List;
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->next;
while(tmp!=NULL)
{
printf("%s",tmp->content);
tmp=tmp->next;
}
return 1;
}

int main()
{
List * head = newList("abcdefg");
List * element;
int i=0;

for(;i<5;i++)
{
element = newList("1234567/n");

element = newList("1234567");
addtailer(head,element);
}
search(head);

system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐