链表的输入输出c语言实现

#include<stdio.h>
#include<stdlib.h>
typedef struct node{
    char data;
    struct node *next;
} Node ,*Linklist;
//链表建立函数
Linklist CreateList(void)
{
    char ch;
    Linklist head;
    Linklist s , r;
    head = NULL;
    s = NULL;
    while( ( ch = getchar()) != '\n')
    {
        r = (struct node *) malloc ( sizeof( struct node) );
        r->data = ch;
        if(head == NULL)
            head = r;
        else
            s->next = r;
        s = r;
    }
   
    if(s != NULL)
    {
        s->next = NULL;
    }
    return head ;
}
//链表输出函数
void Print( Linklist list)
{
    Linklist a,b;
    a = list;
    while(a != NULL)
    {
        printf("%c\n", a->data) ;
        b = a;
        a = a->next;
        free(b);
    }
}
int main()
{
    Linklist head;
    head = CreateList();
    Print( head );
    return 0;
}

猜你喜欢

转载自zhangmingwei.iteye.com/blog/1765643
今日推荐