读书人

C语言写链表的有关问题

发布时间: 2013-01-23 10:44:49 作者: rapoo

C语言写链表的问题

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);
这句话说是有错误,为什么啊
[解决办法]
printf("your linklist is:%d\n",p->next->data);


[解决办法]
写程序要仔细。少了%d了

读书人网 >C语言

热点推荐