链表的生成求解,编译没有报错,运行输入值以后,程序就崩溃了。
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *pNext;
}NODE,*PNODE;
PNODE create_list();
void main()
{
PNODE phead = NULL;
phead = create_list();
}
PNODE create_list()
{
int i,len;
int val;
PNODE pnew = NULL;
PNODE phead = NULL;
PNODE tail = NULL;
phead = (PNODE)malloc(sizeof(NODE));
if(phead==NULL)
{
printf("分配失败");
exit(-1);
}
phead = tail;
printf("请输入需要创建多少个结点?");
scanf("%d",&len);
for(i = 0;i<len;i++)
{
printf("请输入第%d个结点的数值",i+1);
scanf("%d",&val);
pnew = (PNODE)malloc(sizeof(NODE));
if(pnew==NULL)
{
printf("分配失败");
exit(-1);
}
pnew->data = val;
tail->pNext = pnew;
pnew->pNext = NULL;
tail = pnew;
}
return phead;
}