zoukankan      html  css  js  c++  java
  • 225. Implement Stack using Queues

         /*
          * 225. Implement Stack using Queues
          * 12.10 by Mingyang
          * 这里就是用两个queue,先放一个,然后把另一个的一次移过来,然后交换,下次进的就是进另外一个
          */
         class MyStack {
             private Queue<Integer> q1 = new LinkedList<Integer>();
             private Queue<Integer> q2 = new LinkedList<Integer>();  
             // Push element x onto stack.
             public void push(int x) {
                 q2.offer(x);
                 while (!q1.isEmpty()) {
                     q2.offer(q1.poll());
                 }      
                 Queue tmp = q1;   //如何交换
                 q1 = q2;
                 q2 = tmp;
             }
             // Removes the element on top of the stack.
             public void pop() {
                 q1.poll();
             }
             // Get the top element.
             public int top() {
                 return q1.peek();
             }
             // Return whether the stack is empty.
             public boolean empty() {
                 return q1.isEmpty();
             }
         }
  • 相关阅读:
    until循环
    linux的shell使用
    shell通配符(元字符)
    linu运算
    mail邮件
    linux命令
    redis笔记
    mysql 5.7安装方法
    mysql5.7.25搭建mysql-5.7.25.tar.gz包(亲验)
    mysql数据迁移
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5580199.html
Copyright © 2011-2022 走看看