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

来源:互联网 发布:阿里旺旺mac最新版本 编辑:程序博客网 时间:2024/05/17 11:06

本程序在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");
}

 

 

原创粉丝点击