list

来源:互联网 发布:sqlserver linux 编辑:程序博客网 时间:2024/06/07 19:31

#include <stdio.h>#include <malloc.h>struct Node{int data;Node *next; }; Node * createList(){int len=0;int iData=1;Node *phead=(Node*)malloc(sizeof(Node)); if (NULL==phead){printf("内存分配失败!\n");return NULL;}Node * ptail=phead;while(iData!=0){printf("请输入第%d个元素",len);scanf("%d",&iData);Node* pnew=(Node*)malloc(sizeof(Node));pnew->data=iData;pnew->next=NULL;ptail->next=pnew;ptail=pnew;++len;}return phead;}void showList(Node *phead){phead=phead->next; while(phead!=NULL){printf("%d  ",phead->data);phead=phead->next; } } int main(){Node* phead;phead=createList();showList(phead);return 0;}