链表存储——小练习

来源:互联网 发布:如何做好酒店网络销售 编辑:程序博客网 时间:2024/06/05 05:07
多编码来强化编程流程
#include <stdio.h>#include <stdlib.h>#include <string.h>struct list{    char name[20];    struct list * next;};typedef struct list list;list* createlist(){    list *head,*p,*q;    head=(list*)malloc(sizeof(list));    if(NULL==head)    {        printf("memory allocate failure!\n");        free(head);        return NULL;    }    q=head;    printf("please input a character!\n");    char name[20]={0};//    scanf("%s\n",name);    gets(name);    while(strlen(name)!=0)    {        p=(list*)malloc(sizeof(list));        if(NULL==p) return head;        strcpy(p->name,name);        q->next=p;//与头节点链接        q=p;//头节点向后挪动一个位置        printf("continue? if you want to move on,please input what you want to input,if not,please input enter.\n");//        scanf("%s",name);        gets(name);    }    q->next=NULL;    return head;}void print(list *head){    list *q;    q=head->next;    while(q!=NULL)    {        printf("%s\n",q->name);        q=q->next;    }}int main(){    list *head=NULL;    head=createlist();    print(head);    free(head);    return 0;}

1 0
原创粉丝点击