//
// main.cpp
// Data Structure TRY1
//
// Created by zr9558 on 6/7/13.
// Copyright (c) 2013 zr9558. All rights reserved.
//
// Data Structure C++, Weiss, P.104 Section 3.7 The Queue ADT
/*
http://www.cplusplus.com/reference/queue/queue/
member functions:
empty Test whether container is empty (public member function)
size Return size (public member function)
front Access next element (public member function)
back Access last element (public member function)
push Insert element (public member function)
pop Delete next element (public member function)
*/
#include<iostream>
using namespace std;
template<typename Comparable>
class Queue
{
public:
Queue():head(NULL),tail(NULL){theSize=0;}
Queue(constQueue &rhs)
{
makeEmpty();
*this=rhs;
}
constQueue &operator =(constQueue &rhs)
{
if(this==&rhs)return *this;
makeEmpty();
for(Node *p=rhs.head; p!=NULL; p=p->next)
Push(p->element);
return *this;
}
~Queue() {makeEmpty();}
bool Empty()const {returntheSize==0;}
int Size()const {returntheSize;}
Comparable Front() {returnhead->element;}
Comparable Back() {returntail->element;}
void Push(const Comparable & x)
{
Node * tt=newNode(x,NULL);
if(head!=NULL) {tail->next=tt;tail=tt;}
elsehead=tail=tt;
++theSize;
}
void Pop( )
{
if(head!=NULL)
{
Node *tt=head->next;
Node *pp=head;
head=tt;
--theSize;
delete pp;
}
}
private:
struct Node
{
Comparable element;
Node * next;
Node(const Comparable &x=Comparable(),Node *p=NULL):element(x),next(p){}
};
void makeEmpty()
{
while(!Empty())Pop();
head=tail=NULL;
}
int theSize;
Node *head;
Node *tail;
};
int main()
{
Queue<int> Q;
for(int i=0; i<10; ++i)
Q.Push(i);
Queue<int> Q2, Q3(Q);
Q2=Q3;
cout<<Q.Size()<<endl;
while( !Q.Empty())
{
cout<<Q.Front()<<" "<<Q.Back()<<endl;
Q.Pop();
}
cout<<Q2.Size()<<endl;
cout<<Q3.Size()<<endl;
cout<<Q.Size()<<endl;
while( !Q2.Empty())
{
cout<<Q2.Front()<<" "<<Q2.Back()<<endl;
Q2.Pop();
}
while( !Q3.Empty())
{
cout<<Q3.Front()<<" "<<Q3.Back()<<endl;
Q3.Pop();
}
return 0;
}