标签:
Description:
Implement the following operations of a stack using queues.
Notes:
push to back
, peek/pop from front
, size
, and is empty
operations are valid.Code:
1 class Stack { 2 public: 3 // Push element x onto stack. 4 void push(int x) { 5 m.push_front(x); 6 } 7 8 // Removes the element on top of the stack. 9 void pop() { 10 if (!m.empty()) 11 m.pop_front(); 12 } 13 14 // Get the top element. 15 int top() { 16 if (!m.empty()) 17 return m.front(); 18 } 19 20 // Return whether the stack is empty. 21 bool empty() { 22 if (m.size()==0) 23 return true; 24 else 25 return false; 26 } 27 private: 28 deque<int>m; 29 };
标签:
原文地址:http://www.cnblogs.com/happygirl-zjj/p/4598850.html