关于C语言链表的问题

tankey0909 2013-01-22 05:05:55
#include <stdio.h>

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->next)
{
p = p->next;
printf("your linklist is:%d\n", p->data);

}
}


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);
printf("*************\n");
printLinkList(pMyList);

return 0;
}
输出结果:

1 is your linklist :
2 is your linklist :
3 is your linklist :
4 is your linklist :
*************
1 is your linklist :
1 is your linklist :
2 is your linklist :
3 is your linklist :
4 is your linklist :


有2个问题,
1)运行程序时,输入第一个数字时没有反应,需要再次输入
2)再次printf后,出现了2个1

请问这是什么问题,怎样解决,多谢
...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtee 2013-01-23
  • 打赏
  • 举报
回复
针对你这种情况单步 断点调试
newtee 2013-01-23
  • 打赏
  • 举报
回复
newtee 2013-01-23
  • 打赏
  • 举报
回复
有些东西是习惯 帮你改了一下
#include <stdio.h>
typedef struct node
{
   int data;
   struct node *next;
}Node;
typedef Node * LinkList;
LinkList creatLinkList()
{
    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",&num);
        if(num == -1)
        {
              printf("finish creating linklist!\n");
              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->next)
    {
       p = p->next;
       printf("your linklist is:%d\n", p->data);
    }
}
int main()
{
      LinkList pMyList =  creatLinkList();
      printLinkList(pMyList);
      printf("*************\n");
      printLinkList(pMyList);
      return 0;
}
aljadyan 2013-01-23
  • 打赏
  • 举报
回复
你用的是linux环境,还是VC, 要是linux scanf("%d\n",&num); -> scanf("%d",&num);
ForestDB 2013-01-22
  • 打赏
  • 举报
回复
有种工具叫debugger,LZ可以自己先试试。 屡试不爽。

69,371

社区成员

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

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