#include<iostream>
#include<string.h>
#include"LinQueue.h"
#include"LinStack.h"
using namespace std;
void main(){
Queue<int> Q;
LinStack<int> S;
int i,z=0;
int a[]={1,3,5,7,9},b[]={10,8,6,4,2};
for(i=0;i<5;i++)
{Q.EnQue(a[i]);
S.Push(b[i]);
}
while(z<i){
int temp,temp2;
temp=Q.DeQue();
Q.EnQue(temp);
temp2=S.pop();
Q.EnQue(temp2);
z++;
}
for(i=0;i<10;i++)
cout<<Q.DeQue()<<'\t';
cout<<'\n';
}
//LinQueue.h
#include<iostream>
#include<assert.h>
using namespace std;
template<typename T>class Queue;
template<typename T>class Node{
T info;
Node<T> *link;
public:
Node(T data=0,Node *l=NULL);
friend class Queue<T>;
};
template<typename T> Node<T>::Node(T data,Node *l){
info=data;
link=l;
}
template<typename T>class Queue{
Node<T> *front,*rear;
public:
Queue(){rear=front=NULL;}
~Queue();
bool IsEmpty(){ return front==NULL;}
void EnQue(const T &data);
T DeQue();
T GetFront();
void MakeEmpty();
};
template<typename T>void Queue<T>::MakeEmpty(){
Node<T> *temp;
while(front!=NULL){
temp=front;front=front->link;delete temp;
}
}
template<typename T>Queue<T>::~Queue(){MakeEmpty();}
template<typename T>void Queue<T>::EnQue(const T &data){
if(front==NULL) front=rear=new Node<T>(data,NULL);
else rear=rear->link=new Node<T>(data,NULL);
}
template<typename T>T Queue<T>::DeQue(){
assert(!IsEmpty());
Node<T> *temp=front;
T data=temp->info;
front=front->link;
delete temp;
return data;
}
template<typename T>T Queue<T>::GetFront(){
assert(!IsEmpty());
return front->info;
}
//LinStack.h
#include<iostream>
#include<stdlib.h>
using namespace std;
template<typename T>class LinStack;
template<typename T>class StackNode{
StackNode<T> *next;
public:
T data;
StackNode(const T &item,StackNode<T> *perNext=NULL);
~StackNode(){};
friend class LinStack<T>;
};
template<typename T>StackNode<T>::StackNode(const T &item,StackNode<T> *perNext=NULL){
data=item;
next=perNext;
}
template<typename T>class LinStack{
StackNode<T> *top;
int size;
public:
LinStack();
~LinStack();
void MakeEmpty();
void Push(const T &item);
T pop();
T GetTop(){ if(size==0){
cerr<<"堆栈已空!"<<endl;
exit(1);
}
return top->data;
}
int Empty(){ return size<=0;}
void print(){ while(top!=NULL){cout<<top->data<<'\t';top=top->next;}}
};
template<typename T>LinStack<T>::LinStack(){
top=NULL;
size=0;
}
template<typename T>LinStack<T>::~LinStack(){
MakeEmpty();
}
template<typename T>void LinStack<T>::MakeEmpty(){
StackNode<T> *temp;
while(top!=NULL){
temp=top;
top=top->next;
delete temp;
}
}
template<typename T>void LinStack<T>::Push(const T &item){
StackNode<T> *newNode;
newNode=new StackNode<T>(item,top);
top=newNode;
size++;
}
template<typename T>T LinStack<T>::pop(){
if(size==0){
cerr<<"堆栈已空!"<<endl;
exit(1);
}
StackNode<T> *temp;
temp=top;
T data=temp->data;
top=temp->next;
delete temp;
size--;
return data;
}