#include "stdafx.h"
#include <iostream>
using namespace std;
class Data {
public:
  Data():data(0),next(NULL){}
  int data;
  Data *next;
};
class Stack{
public:
  Stack():Top(NULL){}
        void EnStack(int d);
        void DeStack(Data &d);    
        bool IsEmpty();
  Data *Top;
};

void Stack::EnStack(int d){
  Data *p=new Data;
  p->data=d;
  p->next=Top;
  Top=p;
}
void Stack::DeStack(Data &d){
  if (IsEmpty()){
    cout<<"Empty Stack"<<endl;
  }
  else{
  Data *p=NULL;
  p=Top;
  d.data=p->data;
  Top=Top->next;
  delete p;
  }
}
bool Stack::IsEmpty(){
  if (Top==NULL){
    return 1;
  }
  else
    return 0;
}
class Queue{
public:
  void EnQueue(int d);
  void DeQueue(Data &d);
  bool IsEmpty();
private:
  Stack s1;//入队
  Stack s2;//出队
};
void Queue::EnQueue(int d){
  s1.EnStack(d);
}
void Queue::DeQueue(Data &d){
  Data temp;
  if (!s2.IsEmpty()){
    s2.DeStack(d);
  }
  if (s2.IsEmpty()){
    if (!s1.IsEmpty()){
    while (!s1.IsEmpty()){
      s1.DeStack(temp);
      s2.EnStack(temp.data);
    }
    s2.DeStack(d);
    }
  }
}
bool Queue::IsEmpty(){
  if (s1.IsEmpty() && s2.IsEmpty()){
    return 1;
  }
  else
    return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
  Queue q;
  Data d;
  q.EnQueue(1);
  q.EnQueue(2);
  q.EnQueue(3);
  q.EnQueue(4);
  while (!q.IsEmpty()){
                q.DeQueue(d);
    cout<<"DeStack "<<d.data<<endl;
  }
        system("pause");
  return 0;
}