C语言写链表的问题

tankey0909 2013-01-17 12:36:03

typedef struct
{
int data;
struct Node *next;



}Node;
typedef Node * LinkList;


LinkList creatLinkList_2()
{
LinkList pRear,pHead,pCur;
int num;

printf("creating pHead...\n");
if( ( pHead = (LinkList)malloc(sizeof(Node)) ) == NULL)
{
printf("failed to allocate memery for pHead");


}
else
{
printf("successfully create pHead!\n");

}
pHead->next = NULL;
pRear = pHead;

while(1)
{
printf("input the number of linklist you want to create:");
printf("(-1 as end )\n");
scanf("%d\n",&num);

if(num == -1)

{
printf("finish creating linklist!");
break;
}

pCur = (LinkList)malloc(sizeof(Node));

pCur->data = num;
pRear->next = pCur;
pRear = pCur;

printf("%n");

}
pRear->next = NULL;

return pHead;



}
void printLinkList(LinkList pHead)
{
LinkList p = pHead;
while(p)
{

printf("your linklist is:\n", p->next->data);
p = p->next;
}
}

#include <stdio.h>
//#include "LinkList.h"
int main()
{
/* LinkList *L = (LinkList *)malloc(sizeof(LinkList));
create_LinkList(&L,5);
LinkList p = *L;
show_LinkList(p,5);
free(L);*/


LinkList pMyList = creatLinkList_2();
// printf("hehe");
printLinkList(pMyList);

return 0;
}


为何会有错误
printf("your linklist is:\n", p->next->data);
这句话说是有错误,为什么啊
...全文
169 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
永远的霸者 2013-01-17
  • 打赏
  • 举报
回复
		
if(p != NULL && p->next!=NULL)
   printf("your linklist is: %d\n", p->next->data);
你出错时因为p->next = NULL;
xiaoji0507 2013-01-17
  • 打赏
  • 举报
回复
写程序要仔细。少了%d了
欧阳春晖 2013-01-17
  • 打赏
  • 举报
回复
printf("your linklist is:%d\n",p->next->data);

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧