#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);
}