| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 464 人关注过本帖
标题:[求助]为什么是死循环
只看楼主 加入收藏
swr88
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-5-7
收藏
 问题点数:0 回复次数:4 
[求助]为什么是死循环

#include <iostream>
#include <string>
using namespace std;
typedef struct node
{
int number;
int password;
struct node *next;
}SLink;

int GetLength(SLink *sq)
{

int i=1;
SLink *p=sq->next;
while(p!=sq)
{
i++;p=p->next;
}
return i;
}

int DelElem(SLink *sq,int i)
{
int j=1;
SLink *p=sq,*q;
if(i<1 || i>GetLength(sq))
return 0;
else if(i==1)
{
if(GetLength(sq)==1)
{q=sq;free(q);
sq=NULL;}
else
{
while(j<GetLength(sq))
{
p=p->next;j++;
}
q=sq;
p->next=q->next;
sq=q->next;
free(q);
return 1;
}
}
else
{
while(j<i-1)
{
p=p->next;j++;
}
q=p->next;
p->next=q->next;
free(q);
return 1;
}
}


void main()
{
int n;
cout << "n=";
cin >> n;
int m;
cout <<"m=";
cin >> m;

SLink *sq ;
SLink *people = (SLink *)malloc(sizeof(SLink));
people->number=1;
cout << "people->password=" ;
cin >> people->password;
people->next=people;
SLink *lq =people;

for(int i=1;i<n;i++)
{
sq =(SLink *)malloc(sizeof(SLink));
sq->number =i+1;
cout <<"sq->password=";
cin >> sq->password;
lq->next =sq;
sq->next =people;
lq = sq;
}

DelElem(people,1);
for(sq=people;sq->next!=people;sq=sq->next)
{

cout << sq->number;

//cout << sq->next;
}
cout << sq->number;

}
为什么是死循环?

2006-05-07 21:52
lang750636
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-10-6
收藏
得分:0 

这个同学,你给点解释好不好啊
这样看太麻烦,太难懂了啊
不知道你要做什么啊

2006-05-08 21:47
linlin
Rank: 1
等 级:新手上路
帖 子:134
专家分:0
注 册:2006-3-14
收藏
得分:0 
以下是引用swr88在2006-5-7 21:52:00的发言:

#include <iostream>
#include <string>
using namespace std;
typedef struct node
{
int number;
int password;
struct node *next;
}SLink;

int GetLength(SLink *sq)
{

int i=1;
SLink *p=sq->next;
while(p!=sq)//原因在这
{
i++;p=p->next;
}
return i;
}

int DelElem(SLink *sq,int i)
{
int j=1;
SLink *p=sq,*q;
if(i<1 || i>GetLength(sq))
return 0;
else if(i==1)
{
if(GetLength(sq)==1)
{q=sq;free(q);
sq=NULL;}
else
{
while(j<GetLength(sq))
{
p=p->next;j++;
}
q=sq;
p->next=q->next;
sq=q->next;
free(q);
return 1;
}
}
else
{
while(j<i-1)
{
p=p->next;j++;
}
q=p->next;
p->next=q->next;
free(q);
return 1;
}
}


void main()
{
int n;
cout << "n=";
cin >> n;
int m;
cout <<"m=";
cin >> m;

SLink *sq ;
SLink *people = (SLink *)malloc(sizeof(SLink));
people->number=1;
cout << "people->password=" ;
cin >> people->password;
people->next=people;
SLink *lq =people;

for(int i=1;i<n;i++)
{
sq =(SLink *)malloc(sizeof(SLink));
sq->number =i+1;
cout <<"sq->password=";
cin >> sq->password;
lq->next =sq;
sq->next =people;
lq = sq;
}

DelElem(people,1);
for(sq=people;sq->next!=people;sq=sq->next)
{

cout << sq->number;

//cout << sq->next;
}
cout << sq->number;

}
为什么是死循环?


woyaochengshuyidianle 我真的什么也不会
2006-05-08 22:08
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
q=sq;
p->next=q->next;
sq=q->next;
free(q);

没有保存q上一个接点,删除错误
一个比较好的办法以后用这样的方便
例如删一个接点
不要用这个接点本身去判断,用他的上一个接点去判断
if(pNow->next=要删除的)
{
struct tmp=pNow->next;
pNow->next=pNow->next->next;
free(tmp);
这样免去了保存删除接点上一接点的麻烦
}

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-05-09 12:46
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
while(p!=sq)//原因在这
{
i++;p=p->next;
}
我跟踪了一下
楼主的链表绕成了一圈
(估计是楼主故意的)
这句话楼主想让P找到sq

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-05-09 13:25
快速回复:[求助]为什么是死循环
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018305 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved