fw-3.5 - queue by 2 stacks - C++ version - 2013年12月16日21:20:06
2013-12-17 10:19
543 查看
#include <iostream> #include <stack> using namespace std; class queueby2stacks { stack<int> * front_stack; stack<int> * back_stack; public: queueby2stacks() { front_stack = new stack<int>(); back_stack = new stack<int>(); } void enqueue(int i) { front_stack->push(i); } int dequeue() { if (back_stack->size() == 0) { if (front_stack->size() == 0) { cout << "queue is empty" << endl; return -1; } else { while (front_stack->size()!=0) { int ele = front_stack->top(); front_stack->pop(); back_stack->push(ele); } int tempele = back_stack->top(); back_stack->pop(); return tempele; } } else { int element = back_stack->top(); back_stack->pop(); return element; } } }; int main() { cout << "this is the test" << endl; queueby2stacks qtostack; for (int i = 0; i < 10; ++i) { qtostack.enqueue(i); } for (int j = 0; j < 11; ++j) { cout << qtostack.dequeue() << endl;; } }
相关文章推荐
- fw 3.5 -2013年12月16日21:02:12 - two stacks make a queue- java version -
- leetcode 232 Implement Queue using Stacks C++
- fw 3.6 - stack sort- java version - 2013年12月16日23:23:32
- Leetcode-Implement Queue using Stacks(C++)
- 【LeetCode-232】 Implement Queue using Stacks(C++)
- LeetCode 之 Implement Queue using Stacks — C++ 实现
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
- leetcode_c++:栈: Implement Queue using Stacks(232)
- C++ STL--stack/queue 的使用方法
- 算法系列—— Implement Queue using Stacks
- 有序队列(sorted_queue)的C++实现
- leetcode 232 Implement Queue using Stacks(难易度:Easy)
- Leetcode_pascals-triangle (updated c++ and python version)
- leetcode 232. Implement Queue using Stacks
- Q3.5 Implement a MyQueue class which implements a queue using two stacks.
- leetcode stack 232. Implement Queue using Stacks
- C++中queue的用法
- LeetCode 232 Implement Queue using Stacks(利用栈实现队列)
- **Leetcode_median-of-two-sorted-arrays (c++ and python version)
- Leetcode_permutation-sequence(c++ and python version)