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

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;*/

}
...全文
258 13 打赏 收藏 转发到动态 举报

13 条回复

geyao1266 2008-03-21
• 打赏
• 举报

Lynn_Ran 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);  //创建
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)
{}
};

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,636

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下