标签:des style blog http io ar color os sp
#include<iostream> #include<stdio.h> #include<string.h> #include<stack> using namespace std; template <typename T> class CQueue{ public: //CQueue(void); //~CQueue(void); void appendTail(const T& node); T deleteHead(); private: stack<T> stack1; stack<T> stack2; }; template <typename T> void CQueue<T>::appendTail(const T& node){ stack1.push(node); } template <typename T> T CQueue<T>::deleteHead(){ if(stack2.size()<=0){ while(stack1.size()>0){ T& data = stack1.top(); stack1.pop(); stack2.push(data); } } if(stack2.size()==0){ throw "error"; } T head = stack2.top(); stack2.pop(); return head; } int main(){ int n; char ch[10]; int num; CQueue<int> cqueue; scanf("%d",&n); while(n--){ scanf("%s",ch); if(strcmp("PUSH",ch)==0){ scanf("%d",&num); cqueue.appendTail(num); }else{ try{ printf("%d\n",cqueue.deleteHead()); }catch(const char * str){ printf("-1\n"); } } } return 0; }
标签:des style blog http io ar color os sp
原文地址:http://blog.csdn.net/hackcoder/article/details/41727951