64,646
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}
/* 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;
}