我想实现单链表的建立, 可下面程序为什么会出现死循环呀??? 各位帮我看看

xiangxuf 2008-10-24 03:11:44
实现一个单链表的建立

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;

typedef struct student
{
int data;
struct student *next;
}node;

node *create()
{
node *head, *p, *s;
int x,cycle=1;
head=(node*)malloc(sizeof(node));
p=head;
while(cycle)
{
printf("\nplease input the data: ");
scanf("%d",&x);
if(x!=0)
{
s=(node*)malloc(sizeof(node));
s->data=x;
printf("\n %d",s->data);
p->next=s->next;
s->next=p;

}
else cycle=0;
}
head = head->next;
p->next=NULL;
printf("\n YYY %d",head->data );
return(head);

}

int main(void)
{
node *head;
head=create();


return 0;
}
...全文
238 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangxuf 2008-10-24
  • 打赏
  • 举报
回复
哦 没事了 已经好了, 刚才犯了个低级错误 , 呵呵 谢谢啊 .
jisuanji3 2008-10-24
  • 打赏
  • 举报
回复
把编译错误的信息给我看一下
jisuanji3 2008-10-24
  • 打赏
  • 举报
回复
不好意思我刚写的有点问题
现更正如下:


#include <iostream>
#include <stdio.h>
//#include <string.h> 无用
#include <conio.h>
using namespace std;

typedef struct student
{
int data;
struct student *next;
}node;

node *create()
{
node *head, *p, *s;
int x,cycle=1;
head=(node*)malloc(sizeof(node));
head->next=0;//新加的代码
p=head;

while(cycle)
{
printf("\nplease input the data: ");
scanf("%d",&x);
if(x!=0)
{
s=(node*)malloc(sizeof(node));
s->data=x;
printf("\n %d",s->data);

/*以下二句是关键部分,注意顺序不能颠倒
上面的错了*/

s->next=p->next;
p->next=s;

}
else cycle=0;
}
head = head->next;
//p->next=NULL; 这句去掉

/*下面加的for语句用于检测建的链表是否正确*/

for(p=head;p!=0; p=p->next)
{
printf("\n YYY %d",p->data );

}

return(head);

}

int main(void)
{
node *head;
head=create();
return 0;
}
xiangxuf 2008-10-24
  • 打赏
  • 举报
回复
我按3楼改的,加到VC里测试, 为什么还是没能通过啊??? 什么原因啊?
yhwxxx 2008-10-24
  • 打赏
  • 举报
回复

3分....
jisuanji3 2008-10-24
  • 打赏
  • 举报
回复
我将修改后的程序代码放在下面:
(修改的部分有注释)


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;

typedef struct student
{
int data;
struct student *next;
}node;

node *create()
{
node *head, *p, *s;
int x,cycle=1;
head=(node*)malloc(sizeof(node));
head->next=0;//新加的代码
p=head;

while(cycle)
{
printf("\nplease input the data: ");
scanf("%d",&x);
if(x!=0)
{
s=(node*)malloc(sizeof(node));
s->data=x;
printf("\n %d",s->data);
p->next=s; //修改了这句
s->next=p;

}
else cycle=0;
}
head = head->next;
//p->next=NULL; 这句去掉
printf("\n YYY %d",head->data );
return(head);

}

int main(void)
{
node *head;
head=create();
return 0;
}
xiangxuf 2008-10-24
  • 打赏
  • 举报
回复
链表结尾该怎么改??
jisuanji3 2008-10-24
  • 打赏
  • 举报
回复
我将修改后的代码放在下面:
(修改部分加了注释)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <conio.h>
using namespace std;

typedef struct student
{
int data;
struct student *next;
}node;

node *create()
{
node *head, *p, *s;
int x,cycle=1;
head=(node*)malloc(sizeof(node));
head->next=0;//新加的代码
p=head;

while(cycle)
{
printf("\nplease input the data: ");
scanf("%d",&x);
if(x!=0)
{
s=(node*)malloc(sizeof(node));
s->data=x;
printf("\n %d",s->data);
p->next=s; //修改了这句
s->next=p;

}
else cycle=0;
}
head = head->next;
//p->next=NULL; 这句去掉
printf("\n YYY %d",head->data );
return(head);

}

int main(void)
{
node *head;
head=create();
return 0;
}
jisuanji3 2008-10-24
  • 打赏
  • 举报
回复
按逆序建立链表应该是将
p->next=s->next;
更改为
p->next=s;
不过对于链表结尾需要进行修改。
liumingrong 2008-10-24
  • 打赏
  • 举报
回复
p->next=s->next;
s->next=p;
==》
p->next=s;
p = s;

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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