帮我看看这个循环单链表为什么总是输出的时候只有一个元素

geyao1266 2008-03-20 03:18:52
#include<iostream.h>
struct conf
{
int data;
conf *next;
};

class circle
{
private:
conf *first;
conf *last;
public:
void init(circle & b1)
{
b1.first = new conf;
conf *p;
p = b1.first;
int n;
cout<<"请输入元素个数:";
cin>>n;
cout<<endl;
for(int i = 0;i<n;i++)
{

conf *q;
q = new conf;
cout<<"第"<<i+1<<"个数据为:";
cin>>q->data;
cout<<endl;
p->next=q;

}
b1.last = p;
b1.last->next = b1.first->next;


}

void del(circle &b1)
{
conf *p;
p=b1.first;
b1.first->next = b1.first->next->next;
b1.last->next = p->next->next;
p->next = NULL;
}

int length(circle &b1)
{
conf *p=b1.first->next;
int count = 0;
while(p->next= b1.)
{
p=p->next;
count++;
}
return (count+1);
}

void disp(circle& b1)
{
conf *p;
cout<<"循环链表中数据为:"<<endl;
p = b1.first;
if(p !=b1.last)
{
cout<<p->next->data<<" ";
p = p->next;
}
cout<<endl;
}
};

void main()
{
circle b1;
b1.init(b1);
b1.disp(b1);

int a =b1.length(b1);
cout<<"循环链表的元素个数为:"<<a<<endl;

/*b1.del(b1);
b1.disp(b1);
a = b1.length(b1);
cout<<"循环链表的元素个数为:"<<a<<endl;*/


}
...全文
260 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
geyao1266 2008-03-21
  • 打赏
  • 举报
回复
恩,肯定有用,谢谢~
Lynn_Ran 2008-03-20
  • 打赏
  • 举报
回复
希望对lz有帮助 ^_^


#include <iostream>
using namespace std;

struct conf
{
int data;
conf *next;
};

class circle
{
private:
conf *first;
conf *last;
public:
void init(circle &b1); //创建
void del(circle &b1); //删除
int length(circle &b1); //求长
void disp(circle &b1); //打印
};

void circle::init(circle &b1)
{
b1.first = new conf;
conf *p;
p = b1.first;
int n;
cout << "请输入元素个数:";
cin >> n;
cout << endl;
for( int i=0; i<n; i++ ) //<====这个循环有问题,不能形成链表
{
conf *q = new conf;
cout << "第" << i+1 <<"个数据为:";
cin >> q->data;
cout << endl;
q->next = NULL;
p->next = q;
p = q;
}
b1.last = p;
b1.last->next = b1.first->next;
}

void circle::del(circle &b1) //我做的是删除第一个结点,别的大同小异
{
conf *p;
p = b1.first->next;
b1.first->next = b1.first->next->next;
p->next = NULL;
}

int circle::length(circle &b1)
{
conf *p = b1.first->next;
int count = 0;
while( p != b1.last)//<=====
{
p = p->next;
count++;
}
return (count+1);
}

void circle::disp(circle &b1)
{
conf *p;
cout << "循环链表中数据为:" ;
if ( p = b1.first->next )//<====
{
while ( p != b1.last )
{
cout << p->data << " ";
p = p->next;
}
cout << p->data << endl;
}
else
cout << "此链表为空" << endl;
return;
}

void main()
{
circle b1;
b1.init(b1);
b1.disp(b1);
int a =b1.length(b1);

cout << "循环链表的元素个数为:" << a << endl;

b1.del(b1);
b1.disp(b1);
a = b1.length(b1);
cout << "循环链表的元素个数为:" << a << endl;

system("pause");
return;
}
geyao1266 2008-03-20
  • 打赏
  • 举报
回复
问题全部解决...谢谢大家~
geyao1266 2008-03-20
  • 打赏
  • 举报
回复
恩,问题解决了。能不能再详细解释一下改动的代码的意义。谢谢
geyao1266 2008-03-20
  • 打赏
  • 举报
回复
我知道是这个函数的里面的错误,但不知道应该怎么改
我现在把尾指针去掉试试
lzy340623339 2008-03-20
  • 打赏
  • 举报
回复
刚才说错了点,是有了循环,就不必要用尾指针了
这个是正确运行的程序
#include  <iostream> 
using namespace std;


struct conf
{
int data;
conf *next;
};

class circle
{
private:
conf *first;
conf *last;
public:
void init(circle & b1)
{
b1.first = new conf;
conf *p;
p = b1.first;
int n;
cout<<"请输入元素个数:";
cin>> n;
cout<<endl;
for(int i = 0;i <n;i++)
{

conf *q;
q = new conf;
cout<<"第"<<i+1<<"个数据为:";
cin>> q-> data;
cout<<endl;
q->next=NULL;
p-> next=q;
p=q;

}
b1.last = p;
b1.last-> next = b1.first-> next;


}

void del(circle &b1)
{
conf *p;
p=b1.first;
b1.first-> next = b1.first-> next-> next;
b1.last-> next = p-> next-> next;
p-> next = NULL;
}

int length(circle &b1)
{
conf *p=b1.first-> next;
int count = 0;
while(p-> next= b1.first->next)
{
p=p-> next;
count++;
}
return (count+1);
}

void disp(circle& b1)
{
conf *p;
cout<<"循环链表中数据为:"<<endl;
p = b1.first->next;
if(p!=0)
do
{
cout<<p->data<<" ";
p = p-> next;
}while(p !=b1.first->next);
cout<<endl;
}
};

void main()
{
circle b1;
b1.init(b1);
b1.disp(b1);

int a =b1.length(b1);
cout<<"循环链表的元素个数为:"<<a<<endl;

/*b1.del(b1);
b1.disp(b1);
a = b1.length(b1);
cout<<"循环链表的元素个数为:"<<a<<endl;*/


}
geyao1266 2008-03-20
  • 打赏
  • 举报
回复
你的意思是我应该把尾指针给去掉是么
我这样做感觉处理起来会方便一点
geyao1266 2008-03-20
  • 打赏
  • 举报
回复
还是一样,无法输出
我觉得问题应该是初始化时头指针和尾指针的问题,但又不知道怎么改
Chappell 2008-03-20
  • 打赏
  • 举报
回复
void init(circle & b1)
有问题
lzy340623339 2008-03-20
  • 打赏
  • 举报
回复
其实既然你已经有了链头指针和链尾指针,干吗还要是循环链表呢?
ryfdizuo 2008-03-20
  • 打赏
  • 举报
回复
struct conf 
{
int data;
conf *next;
conf():next(NULL)
{}
};
你可以这样子:next初始化为NULL
geyao1266 2008-03-20
  • 打赏
  • 举报
回复
我是这么想的在这地方用一个循环输出,但是结果并不对
我觉得应该是初始化的问题
ryfdizuo 2008-03-20
  • 打赏
  • 举报
回复
void disp(circle& b1) 
{
conf *p;
cout < <"循环链表中数据为:" < <endl;
p = b1.first;
if(p !=b1.last) //这里最起码应该是循环了吧?
{
cout < <p-> next-> data < <" ";
p = p-> next;
}
cout < <endl;
}
};

64,654

社区成员

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

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