博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
232. Implement Queue using Stacks,225. Implement Stack using Queues
阅读量:4972 次
发布时间:2019-06-12

本文共 3643 字,大约阅读时间需要 12 分钟。

232. Implement Queue using Stacks

Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.
Notes:
  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

 

class Queue {public:    // Push element x to the back of queue.    void push(int x) {        instk.push(x);    }    // Removes the element from in front of queue.    void pop(void) {        if(outstk.empty()){            while(!instk.empty()){                outstk.push(instk.top());                instk.pop();            }        }        outstk.pop();    }    // Get the front element.    int peek(void) {        if(outstk.empty()){            while(!instk.empty()){                outstk.push(instk.top());                instk.pop();            }        }        return outstk.top();    }    // Return whether the queue is empty.    bool empty(void) {        return instk.empty() && outstk.empty();    }private:    stack
instk; stack
outstk;};

 

 

225. Implement Stack using Queues

Total Accepted: 25628 Total Submissions: 84579 Difficulty: Easy

Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.
Notes:
  • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
  • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
  • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).

 

class Stack {public:    // Push element x onto stack.    void push(int x) {        if(que1.empty()){            que2.push(x);        }else{            que1.push(x);        }    }    // Removes the element on top of the stack.    void pop() {        if(que1.empty()){            while(que2.size() != 1){                que1.push(que2.front());                que2.pop();            }            que2.pop();        }else{            while(que1.size() != 1){                que2.push(que1.front());                que1.pop();            }            que1.pop();        }    }    // Get the top element.    int top() {        int x = 0;        if(que1.empty()){            while(!que2.empty()){                x = que2.front();                que2.pop();                que1.push(x);            }        }else{            while(!que1.empty()){                x = que1.front();                que1.pop();                que2.push(x);            }        }        return x;    }    // Return whether the stack is empty.    bool empty() {        return que1.empty() && que2.empty();    }private:    queue
que1; queue
que2;};

 

转载于:https://www.cnblogs.com/zengzy/p/5059345.html

你可能感兴趣的文章
NSURLSession的简单使用
查看>>
python征程3.0(python对象)
查看>>
jQuery 库 - 特性
查看>>
搜索好题2
查看>>
3 Selenium Python 数据库及文件
查看>>
Compounding绑定属性
查看>>
【转】jsp 和 servlet的联系和区别
查看>>
mvc4 用NPOI导出Excel
查看>>
格雷码与二进制码互相转换
查看>>
数码管的封装实验 --- verilog
查看>>
unity, 只发射一个粒子的粒子系统
查看>>
第十六章----面向对象(宠物乱斗之主面板)
查看>>
HDU 1564 Play a game
查看>>
Hdu 1407 测试你是否和LTC水平一样高
查看>>
别过来,过来我就撕票了!
查看>>
并发模型—共享内存模型(线程与锁)示例篇
查看>>
正则表达式---------------嵌套的分组
查看>>
转Keil 中使用 STM32F4xx 硬件浮点单元
查看>>
分区命令
查看>>
FreeBSD releaning(6)-chmod
查看>>