c语言最后一个程序链表

来源:互联网 发布:软件联盟推广平台源码 编辑:程序博客网 时间:2024/05/23 18:34
#include <stdio.h>#include <malloc.h>typedef struct Node{int data;Node * next; }; Node * createList(){printf("请输入你要创建的元素的个数:\n");printf("len=");int len;scanf("%d",&len);Node * phead=(Node *)malloc((sizeof(Node))); if(NULL==phead){printf("分配失败");exit(-1); } Node * ptail=phead; int i; for(i=0; i<len; i++){printf("请输入第%d个节点的值\n",i+1);int data;scanf("%d",&data); Node * pnew=(Node *)malloc(sizeof(Node));if(NULL==pnew){printf("分配失败");exit(-1); } pnew->data=data;pnew->next=NULL;ptail->next=pnew;ptail=pnew;  } return phead; }void showList(Node *phead){phead=phead->next; while(phead!=NULL){printf("%d\n",phead->data);phead=phead->next; } } void main(){Node *phead=createList(); showList(phead); }