初学链表 遇到问题了
#include <stdio.h>#include <stdlib.h>
struct st
{
int a;
struct st * pNext;
};
struct st * create(void);
void show(struct st *);
int main()
{
struct st * pHead;
pHead=create();
show(pHead);
}
struct st * create(void)
{
int a,b,val;
struct st * pHead;
struct st * pMid;
struct st * pNew;
printf("请输入节点数:");
scanf("%d",&b);
pHead=(struct st *)malloc(sizeof(struct st));
pMid=pHead;
pMid->pNext =NULL;
for(a=1;a<=b;a++)
{
pNew=(struct st *)malloc(sizeof(struct st));
printf("请输入第%d个节点的数据:",a);
scanf("%d",&val);
pNew->a =val;
pMid->pNext =pNew;
pMid=pNew;
pMid->pNext =NULL;
}
printf("数据输入完毕\n");
return pHead;
}
void show(struct st * pHead)
{
struct st * pMid;
if (pHead->pNext ==NULL)
{
printf( "链表为空");
exit(-1);
}
else
{
pMid=pHead->pNext ;
while(pMid->pNext !=NULL)
{
printf("%d ",pMid->a );
pMid=pMid->pNext ;
}
printf("\n输出完毕");
}
}
最后只能输入第一个有效节点的值 不知道哪里错了 求教