深拷贝

#include <iostream.h>
typedef struct s
{
char  ch;
s *next;
}ss;
typedef struct 
{
ss *front;
ss *rear;
}queue;
class Queue
{
public:
Queue();               //构造    
Queue(Queue &Q);       //拷贝构造
~Queue();              //析构
void print();          //显示
void in();             //进队
void out();            //出队
private:
queue *point;
};
Queue::Queue()
{
ss * node;
node = new ss;
point= new queue;
node->next = NULL;
point->front=node;
    point->rear = node;
}
Queue::~Queue()
{
while(point->front)
{
point->rear = point->front->next;
delete (point->front);
point->front=point->rear;
}
delete point;
}
Queue::Queue(Queue &Q)               //深拷贝
{
ss *p , *node ;
node = new ss;
point= new queue;
node->next = NULL;
point->front = point->rear = node;
p = (Q.point)->front->next;
for( ; p!=NULL ; p = p->next)
{
node = new ss;
node->ch = p->ch;
node->next = NULL;
point->rear->next = node;
point->rear = node;
}
}
void Queue::in()
{
ss *node;
char m;
node = new ss;
cout<<"请输入进队的字母:";
cin>>m;
node->ch=m;
node->next = NULL;
point->rear->next = node;
point->rear = node;
}
void Queue::print()
{
ss *p;
p=point->front->next;
while(p->next)
{
cout<<p->ch<<"<-";
p=p->next;
}
cout<<p->ch;
}
void Queue::out()
{
ss *node;
node = point->front->next;
cout<<node->ch<<"出队"<<endl;
point->front->next=node->next;
if(point->rear==node)
point->rear=point->front;
delete node;
}
int main()
{
Queue Q1;
int i;
    for(i=0;i<5;i++)
Q1.in();
Queue Q2=Q1;
cout<<"           队Q1已经拷贝到Q2中"<<endl;
cout<<"对于Q1,";
Q1.in();
cout<<"Q2: "; Q2.out();
    cout<<"Q1: "; Q1.print();
cout<<endl;
cout<<"Q2: "; Q2.print();
cout<<endl;
return 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值