两个栈实现队列,两个队列实现栈
来源:互联网 发布:如何提高淘宝信誉度 编辑:程序博客网 时间:2024/05/29 10:08
两个栈实现队列
//直接插入到st1中去就可以。
void push(stack<int> *st1,int key){
st1.push(key);
}
//st2为空则把st1中的元素全部放到st1中,并且弹出st2.pop(); 若st2不为空则直接弹出
void pop(stack<int> *st1, stack<int> *st2){
if(st2.empty()){
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
}
if(st2.empty()){
cout<<"没有元素了"<<endl;
return;
}
st2.pop();
}
两个队列实现栈
class Stack {public: // Push element x onto stack. void push(int x) { if (!q1.empty()) { q2.push(x); while (!q1.empty()) { q2.push(q1.front()); q1.pop(); } } else { q1.push(x); while (!q2.empty()) { q1.push(q2.front()); q2.pop(); } } } // Removes the element on top of the stack. void pop() { if (q1.empty()&&q2.empty()) throw new exception("stack is empty"); else if (!q1.empty()) q1.pop(); else q2.pop(); } // Get the top element. int top() { if (!q1.empty()) return q1.front(); else return q2.front(); } // Return whether the stack is empty. bool empty() { return (q1.empty()&&q2.empty()); }private: queue<int> q1, q2;};
阅读全文
0 0
- 两个栈实现队列
- 两个队列实现栈
- 两个栈实现队列
- 两个栈实现队列
- 两个栈实现队列
- 两个队列实现栈
- 两个栈实现队列
- 两个 栈 实现队列
- 两个栈实现队列
- 两个队列实现栈
- 两个栈实现队列
- 两个栈实现队列
- 两个队列实现栈
- 两个栈实现队列
- 两个栈实现队列
- 两个栈实现队列
- 队列--两个栈实现
- 两个队列实现栈
- Android使用百度地图定位(获取当前经纬度和地址信息)
- 路由地址减1
- 关于微信浏览器video标签无法播放mp4视频
- 已解决:去掉myeclipse的web项目启动时右上角出现的CodeLive Panel
- JVM内存划分、JVM内存分配机制、JVM垃圾回收机制
- 两个栈实现队列,两个队列实现栈
- PHP垃圾回收
- 575. Distribute Candies
- alsa编译及使用
- 海量数据的处理
- jetty 启动项目报错:java.lang.ArrayIndexOutOfBoundsException: 52264
- 另一种创建线程的方式
- subscript下标
- 前端UI框架《Angulr》入门