帮忙看哪里错了

tomatobin 2011-03-04 09:21:25
#include<iostream>
using namespace std;
typedef int Item;
struct Node{friend class List;Item item;Node *next;};
class List{
private:
Node * front,*rear;
int count;
public:
List();
~List();
void Creat_List();
int Count() const{return count;}
bool Enlist(Item & item);
bool Delist(Item &item);
bool Isempty(){return count==0;}
friend ostream &operator<<(ostream &os,List &list);
int Front();
int Rear();
};

List::List()
{
front=NULL;rear=NULL;
count=0;
}

void List::Creat_List()
{
double temp;
cout<<"Please input a number(negative to quit):";
cin>>temp;
while(temp>=0)
{
++count;
Node *ptemp=new Node;
ptemp->item=temp;
ptemp->next=NULL;//最后一个节点
if(front==NULL) front=ptemp;//创建首节点
else rear->next=ptemp;
rear=ptemp;
cout<<"Please input next number(negative to quit):";
cin>>temp;
}
if(rear!=NULL) rear->next=NULL;//很重要
}
int List::Rear()
{
if(rear==NULL) return 0;
return rear->item;
}
int List::Front()
{
if(front==NULL) return 0;
return front->item;
}

List::~List()
{
Node *temp;
while(front!=NULL)
{
temp=front;
front=front->next;
delete temp;
}
}
bool List::Enlist(Item &item)
{
Node *temp=new Node;
temp->item=item;
temp->next=NULL;
count++;
if(front==NULL)
front=temp;
else
rear->next=temp;
rear=temp;
return true;
}
bool List::Delist(Item &item)
{
if(Isempty()) {cout<<"Empty!\n";return false;}
item=rear->item;
count--;
if(count==0)
{
delete rear;
front=rear=NULL;
}
else
{
Node *temp=front;
while(temp->next!=rear)
temp=temp->next;
delete rear;
rear=temp;
}

return true;
}
ostream &operator<<(ostream &os,List &list)
{
if(list.front==NULL) os<<"NULL.\n";
else
{
Node *temp=list.front;
while(temp)
{
os<<temp->item<<'\t';
temp=temp->next;
}
}
return os;
}
int main()
{
List list;
Item item;
list.Creat_List();

cout<<list<<endl;
cout<<"The list contains "<<list.Count()<<" items."<<endl;
for(int i=0;i<5;i++)
{
Item a;
if(list.Delist(a))
cout<<"The list contains "<<list.Count()<<" items."<<endl;
else
break;
}
cout<<list.Front()<<endl;
cout<<list.Rear()<<endl;
cout<<list<<endl; //****************************************
while(1);
return 0;
}

当输入的数大于删除的时候,带**********的就不能正常显示 了。。。

...全文
152 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomatobin 2011-03-04
  • 打赏
  • 举报
回复
就是单链表,错在哪里了啊?
無_1024 2011-03-04
  • 打赏
  • 举报
回复
只能说你这个不是循环链表 只是一个含有头指针和尾指针的单链表
無_1024 2011-03-04
  • 打赏
  • 举报
回复

#include<iostream>
using namespace std;
typedef int Item;
struct Node{friend class List;Item item;Node *next;};
class List{
private:
Node * front,*rear;
int count;
public:
List();
~List();
void Creat_List();
int Count() const{return count;}
bool Enlist(Item & item);
bool Delist(Item &item);
bool Isempty(){return count==0;}
friend ostream &operator<<(ostream &os,List &list);
int Front();
int Rear();
};

List::List()
{
front=NULL;rear=NULL;
count=0;
}

void List::Creat_List()
{
int temp;
cout<<"Please input a number(negative to quit):";
cin>>temp;
while(temp>=0)
{
++count;
Node *ptemp=new Node;
ptemp->item=temp;
ptemp->next=NULL;//最后一个节点
if(front==NULL) front=ptemp;//创建首节点
else rear->next=ptemp;
rear=ptemp;
cout<<"Please input next number(negative to quit):";
cin>>temp;
}
if(rear!=NULL) rear->next=NULL;//很重要
}
int List::Rear()
{
if(rear==NULL) return 0;
return rear->item;
}
int List::Front()
{
if(front==NULL) return 0;
return front->item;
}

List::~List()
{
Node *temp;
while(front!=NULL)
{
temp=front;
front=front->next;
delete temp;
}
}
bool List::Enlist(Item &item)
{
Node *temp=new Node;
temp->item=item;
temp->next=NULL;
count++;
if(front==NULL)
front=temp;
else
rear->next=temp;
rear=temp;
return true;
}
bool List::Delist(Item &item)
{
if(Isempty()) {cout<<"Empty!\n";return false;}
item=rear->item;
count--;
if(count==0)
{
delete rear;
front=rear=NULL;
}
else
{
Node *temp=front;
while(temp->next!=rear)
temp=temp->next;
delete rear;
rear=temp;
rear->next = NULL;//----------------------------error
}

return true;
}
ostream &operator<<(ostream &os,List &list)
{
if(list.front==NULL) os<<"NULL.\n";
else
{
Node *temp=list.front;
while(temp)
{
os<<temp->item<<'\t';
temp=temp->next;
}
}
return os;
}
int main()
{
List list;
Item item;
list.Creat_List();

cout<<list<<endl;
cout<<"The list contains "<<list.Count()<<" items."<<endl;
for(int i=0;i<5;i++)
{
Item a;
if(list.Delist(a))
cout<<"The list contains "<<list.Count()<<" items."<<endl;
else
break;
}
cout<<list.Front()<<endl;
cout<<list.Rear()<<endl;
cout<<list<<endl; //****************************************
while(1);
return 0;
}


pengzhixi 2011-03-04
  • 打赏
  • 举报
回复
没问题啊
tomatobin 2011-03-04
  • 打赏
  • 举报
回复
就是直接删除尾节点的,就这还出错了。。。。
pengzhixi 2011-03-04
  • 打赏
  • 举报
回复
bool List::Delist(Item &item)//你这个是否是要删除数据项为item的节点。如果是那么你得重新实现下,因为你这里只是删除了尾节点。
pengzhixi 2011-03-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]
if(front==NULL) front=ptemp;应该是rear=front=ptemp;
[/Quote]

可以忽略这里的回答。没看到你后面有rear=ptemp;
pengzhixi 2011-03-04
  • 打赏
  • 举报
回复
另外你节点里面是int类型数据,但是你输入的是double会有精度损失。
pengzhixi 2011-03-04
  • 打赏
  • 举报
回复
if(front==NULL) front=ptemp;应该是rear=front=ptemp;
tomatobin 2011-03-04
  • 打赏
  • 举报
回复
果然是这样!!谢了!
newfarmerchi 2011-03-04
  • 打赏
  • 举报
回复

/* 1. 我用的是vc,所以用#include <iostream.h>
2. 只需在bool List::Delist(Item &item) 加个rear->next=NULL;//加上一个它
3. 可以正常显示了。
*/


#include<iostream.h>
//using namespace std;
typedef int Item;
struct Node{friend class List;Item item;Node *next;};
class List{
private:
Node * front,*rear;
int count;
public:
List();
~List();
void Creat_List();
int Count() const{return count;}
bool Enlist(Item & item);
bool Delist(Item &item);
bool Isempty(){return count==0;}
friend ostream &operator<<(ostream &os,List &list);
int Front();
int Rear();
};

List::List()
{
front=NULL;rear=NULL;
count=0;
}

void List::Creat_List()
{
double temp;
cout<<"Please input a number(negative to quit):";
cin>>temp;
while(temp>=0)
{
++count;
Node *ptemp=new Node;
ptemp->item=temp;
ptemp->next=NULL;//最后一个节点
if(front==NULL) front=ptemp;//创建首节点
else rear->next=ptemp;
rear=ptemp;
cout<<"Please input next number(negative to quit):";
cin>>temp;
}
if(rear!=NULL) rear->next=NULL;//很重要
}
int List::Rear()
{
if(rear==NULL) return 0;
return rear->item;
}
int List::Front()
{
if(front==NULL) return 0;
return front->item;
}

List::~List()
{
Node *temp;
while(front!=NULL)
{
temp=front;
front=front->next;
delete temp;
}
}
bool List::Enlist(Item &item)
{
Node *temp=new Node;
temp->item=item;
temp->next=NULL;
count++;
if(front==NULL)
front=temp;
else
rear->next=temp;
rear=temp;
return true;
}
bool List::Delist(Item &item)
{
if(Isempty()) {cout<<"Empty!\n";return false;}
item=rear->item;
count--;
if(count==0)
{
delete rear;
front=rear=NULL;
}
else
{
Node *temp=front;
while(temp->next!=rear)
temp=temp->next;
delete rear;
rear=temp;
rear->next=NULL;//加上一个它
}

return true;
}
ostream &operator<<(ostream &os,List &list)
{
if(list.front==NULL) os<<"NULL.\n";
else
{
Node *temp=list.front;
while(temp)
{
os<<temp->item<<'\t';
temp=temp->next;
}
}
return os;
}
int main()
{
List list;
Item item;
list.Creat_List();

cout<<list<<endl;
cout<<"The list contains "<<list.Count()<<" items."<<endl;
for(int i=0;i<5;i++)
{
Item a;
if(list.Delist(a))
cout<<"The list contains "<<list.Count()<<" items."<<endl;
else
break;
}
cout<<list.Front()<<endl;
cout<<list.Rear()<<endl;
cout<<list<<endl; //****************************************
//while(1);
return 0;
}






tianxia1121 2011-03-04
  • 打赏
  • 举报
回复
bool List::Delist(Item &item)
{
if(Isempty()) {cout<<"Empty!\n";return false;}
item=rear->item;
count--;
if(count==0)
{
delete rear;
front=rear=NULL;
}
else
{
Node *temp=front;
while(temp->next!=rear)
temp=temp->next;
delete rear;
rear=temp;
rear->next = NULL;//少了把rear->next置为NULL }

return true;
}

64,646

社区成员

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

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