123
返回列表 发新帖
楼主: wwwyhx
跳转到指定楼层
上一主题 下一主题
收起左侧

Microsoft : 一个栈实现队列

🔗
qqibrow 2012-3-20 23:38:10 | 只看该作者
全局:
回复 17# ilovexiao77

当时上课的时候老师也出过这个题:但问的不是如何实现,而是这样的队列的最大容积是多少,当然,两个栈的容积都是n。这样我觉得比最简单是实现多了很多意思。
回复

使用道具 举报

🔗
derekdomo 2012-3-22 22:31:45 | 只看该作者
全局:
长见识了。。。还可以这么写!
回复

使用道具 举报

🔗
GTea 2013-8-18 16:06:17 | 只看该作者
全局:
只用一个stack的话,就要保证在任何时候,这个stack里都是越早加入的元素越靠top,越晚的越靠bottom。

这跟stack的性质正好相反,怎么实现呢?用递归。
回复

使用道具 举报

🔗
GTea 2013-8-18 16:08:24 | 只看该作者
全局:
Java代码:
  1. // Implementing queue using only one stack and recusion

  2. public class SSQueue {
  3.         private Stack<Integer> stack;

  4.         public SSQueue() {
  5.                 this.stack = new Stack<Integer>();
  6.         }

  7.         public void enqueue(int e) {
  8.                 this.stack = this.addAtBottom(e, this.stack);
  9.         }

  10.         private Stack<Integer> addAtBottom(int e, Stack<Integer> stack) {
  11.                 if (stack == null) {
  12.                         return null;
  13.                 }

  14.                 if (stack.size() == 0) {
  15.                         stack.push(e);
  16.                         return stack;
  17.                 }

  18.                 Integer top = stack.pop();
  19.                 stack = this.addAtBottom(e, stack);
  20.                 stack.push(top);

  21.                 return stack;
  22.         }

  23.         public int dequeue() {
  24.                 int e = -1;

  25.                 e = this.stack.pop();

  26.                 return e;
  27.         }

  28.         public boolean isEmpty() {
  29.                 boolean isEmpty = false;

  30.                 isEmpty = this.stack.isEmpty();

  31.                 return isEmpty;
  32.         }

  33.         public int peek() {
  34.                 int head = -1;

  35.                 int top = this.stack.peek();
  36.                 head = top;

  37.                 return head;
  38.         }

  39.         /**
  40.          * @param args
  41.          */
  42.         public static void main(String[] args) {
  43.                 SSQueue myQueue = new SSQueue();
  44.                
  45.                 myQueue.enqueue(1);
  46.                 myQueue.enqueue(2);
  47.                 myQueue.enqueue(3);
  48.                 myQueue.enqueue(4);
  49.                 myQueue.enqueue(5);
  50.                
  51.                 while (!myQueue.isEmpty()) {
  52.                         System.out.println(myQueue.dequeue());
  53.                 }
  54.         }
  55. }
复制代码


运行结果:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
复制代码


回复

使用道具 举报

🔗
vng 2013-8-26 15:27:04 | 只看该作者
全局:
本帖最后由 vng 于 2013-8-26 15:35 编辑

#include <stack>
#include <iostream>

using namespace std;

template <typename T>
class Fifo : public std::stack<T>
{
        public:
                Fifo() {}
                void push(const T &val){
                        if(empty()) {
                                this->std::stack<T>::push(val);
                        } else {
                                T ntop = top();
                                pop();
                                push(val);
                                this->std::stack<T>::push(ntop);
                        }
                }
};

int main()
{
        Fifo<int> mf;
        int i;
        for(i = 0 ; i < 10 ; i ++)
        {
                mf.push(i);
        }

        i = 0;
        while(!mf.empty())
        {
                int data = mf.top();
                mf.pop();
                std::cout << i++  << " is  " << data << std::endl;
        }


        return 0;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表