使用两个栈实现一个队列


template<class T>
struct MyQueue
{
   void push(T &t)
   {
      s1.push(t);
   }


   T front()
   {
      if(s2.empty())
      {
         if(s1.size() == 0) throw;
         while(!s1.empty())
         {
            s2.push(s1.top());
            s1.pop();
         }
      }
      return s2.top();
   }


   void pop()
   {
      if(s2.empty())
      {
         while(!s1.empty())
         {
            s2.push(s1.top());
            s1.pop();
         }
      }
      if(!s2.empty())
         s2.pop();
   }


   stack<T> s1;  //入栈
   stack<T> s2;  //出栈
};

 

posted @ 2015-04-16 16:39  hy1hy  阅读(114)  评论(0编辑  收藏  举报