我建的链表不能输出,找不出哪里出错,请帮我看看是哪出错了?
我建的链表不能输出,找不出哪里出错,请帮我看看是哪出错了?谢谢
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
typedef struct node
{
int data; //数据域
struct node * pnext;//指针域
}NODE, * PNODE; // NODE等价于struct node, PNODE等价于struct node *
PNODE init_list();
PNODE creat_list(void);
void show_list(PNODE);
int main(void)
{
PNODE pHead = NULL;
pHead = creat_list();//创建一个链表并把返回值付给pHead;
show_list(pHead);
getchar ();
getchar ();
return 0;
}
PNODE creat_list(void)
{
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if(NULL == pHead)
{
printf ("分配失败,程序终止!\n");
exit(-1);
}
PNODE pTail;
pTail = pHead;
pTail->pnext = NULL;
int i = 1, n = 0, val;//i用于判断是否结束while循环,n用于结点计算。
while(i)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf ("分配失败,程序终止!\n");
exit(-1);
}
printf ("请输入结点成员值:\n");
scanf("%d", &val);
pNew->data = val;
pTail->pnext = pNew;
pTail = pNew;
n++;
printf ("继续请按1;结束请按0!\n"); //判断是否退出循环,结束结点输入;
scanf("%d",&i);
}
printf("共有%d个结点\n", n);
return pHead;
}
void show_list(PNODE pHead)
{
PNODE p = pHead->pnext;
while(NULL != p)
{
printf ("%d",p->data);
p = p->pnext;
}
printf ("\n");
return;
}
运行时提示这个,请问哪出了错呢?