笔试题:用两个栈实现队列

;template<typename T>class QUEUE{public:QUEUE(){}~QUEUE(){}void APPEND(const T val){while(st2.empty()==false){//我们只用st2保存数据,st1作为中间交换桥梁。//首先将st2中的数据逆序保存在st1中。st1.push(st2.top());st2.pop();}//然后将输入的值放在st1得顶部。 st1.push(val);while(st1.empty()==false){st2.push(st1.top());st1.pop();}}int DELHED(){int temp = st2.top();st2.pop();return temp;}void Printf(){stack<int> st3 = st2;//打印的话,为了不影响原来的栈,,选择使用了一个临时变量。while(st3.empty()==false){cout<<st3.top()<<” “;st3.pop();}cout<<endl;}private:stack<T> st1;stack<T> st2;};int main(){QUEUE<int> qe;for(int i=0;i<10;i++){qe.APPEND(i);}qe.Printf();qe.DELHED();qe.Printf();qe.DELHED();qe.Printf();qe.DELHED();qe.Printf();qe.DELHED();qe.Printf();qe.DELHED();qe.Printf();qe.DELHED();qe.Printf();qe.DELHED();qe.Printf();return 0;}

总结成功的经验能够让人越来越聪明,

笔试题:用两个栈实现队列

相关文章:

你感兴趣的文章:

标签云: