69,373
社区成员
发帖
与我相关
我的任务
分享
LinkList creatLinkList_2()
{
LinkList pRear,pHead,pCur;
int num, data;
printf("creating pHead...\n");
if ( (pHead = (LinkList)malloc(sizeof(Node))) == NULL)
{
printf("failed to allocate memery for pHead");
return NULL;
}
else
{
printf("successfully create pHead!\n");
}
pHead->next = NULL;
pRear = pHead;
printf("input the number of the node int the linklist you want to create:");
printf("(-1 as end )\n");
scanf("%d\n",&num);
if(num <= -1)
{
printf("finish creating linklist!");
pHead->data = 0;
return pHead;
}
pHead->data = num;
while(num-- >0 )
{
pCur = (LinkList)malloc(sizeof(Node));
if (!pCur) {
pHead->data -= num+1;
break;
}
scanf("%d",&data);
pCur->next = NULL;
pCur->data = data;
pRear->next = pCur;
pRear = pCur;
}
pRear->next = NULL;
return pHead;
}