zoukankan      html  css  js  c++  java
  • java——栈和队列 面试题

    (1)栈的创建

    (2)队列的创建

    (3)两个栈实现一个队列

    (4)两个队列实现一个栈

    (5)设计含最小函数min()的栈,要求min、push、pop、的时间复杂度都是O(1)

    (6)判断栈的push和pop序列是否一致

    -------------------------------------------------------------------------------------------------

    1. 栈的创建

    public class Stack{
        public Node head;
        public Node current;
    
        //入栈
        public void push(int data){
            if(head==null){
                head=new Node(data);
                current=head;
            }else{
                Node node=new Node(data);
                node.pre=current;
                current=node;  ////让current结点永远指向新添加的那个结点
            }
        }
    
        public Node pop(){
            if(current==null){
                return null;    
            } 
            Node node=current;
            current=current.pre; //current结点是我们要出栈的结点
            return node;
        }
    
        class Node{
            int data;
            Node pre;
            public Node(int data){
                this.data=data;
            }
        }
    
    
        public static void main(String[] args) {
            Stack stack=new Stack();
            stack.push(1);
            stack.push(2);
            stack.push(3);
    
            System.out.println(stack.pop().data);
            System.out.println(stack.pop().data);
            System.out.println(stack.pop().data);
        }
    }
    View Code

    2. 队列的创建

    队列的创建有两种形式:基于数组结构实现(顺序队列)、基于链表结构实现(链式队列)。

    我们接下来通过链表的形式来创建队列,这样的话,队列在扩充时会比较方便。队列在出队时,从头结点head开始。

    入栈时,和在普通的链表中添加结点的操作是一样的;出队时,出的永远都是head结点。

    public class Queue{
        public Node head;
        public Node current;
        
        //链表中添加节点
        public void add(int data){
            if(head==null){
                head=new Node(data);
                current=head;
            }else{
                current.next=new Node(data);
                current=current.next;
            }
        }
    
        //出队操作
        public int pop() throws Exception{
            if(head==null){
                throw new Exception("队列为空");
            }
            Node node=head;
            head=node.next;
            return node.data;
        }
    
        class Node{
            int data;
            Node next;
            public Node(int data){
                this.data=data;
            }
        }
    
        public static void main(String[] args) throws Exception{
            Queue queue=new Queue();
    
            for(int i=0; i<5; i++){
                queue.add(i);
            }
    
            System.out.println(queue.pop());
            System.out.println(queue.pop());
            System.out.println(queue.pop());
        }
    }
    View Code

    3. 两个栈实现一个队列

    栈1用于存储元素,栈2用于弹出元素,负负得正。

    说的通俗一点,现在把数据1、2、3分别入栈一,然后从栈一中出来(3、2、1),放到栈二中,那么,从栈二中出来的数据(1、2、3)就符合队列的规律了,即负负得正。

    import java.util.Stack;
     
    /**
    * Created by smyhvae on 2015/9/9.
    */
    public class Queue {
     
    private Stack<Integer> stack1 = new Stack<>();//执行入队操作的栈
     private Stack<Integer> stack2 = new Stack<>();//执行出队操作的栈
     
    //方法:给队列增加一个入队的操作
     public void push(int data) {
      stack1.push(data);
     
    }
     
    //方法:给队列正价一个出队的操作
     public int pop() throws Exception {
     
    if (stack2.empty()) {//stack1中的数据放到stack2之前,先要保证stack2里面是空的(要么一开始就是空的,要么是stack2中的数据出完了),不然出队的顺序会乱的,这一点很容易忘
     
    while (!stack1.empty()) {
        stack2.push(stack1.pop());//把stack1中的数据出栈,放到stack2中【核心代码】
       }
     
    }
     
    if (stack2.empty()) { //stack2为空时,有两种可能:1、一开始,两个栈的数据都是空的;2、stack2中的数据出完了
       throw new Exception("队列为空");
      }
     
    return stack2.pop();
     }
     
    public static void main(String[] args) throws Exception {
      Queue queue = new Queue();
      queue.push(1);
      queue.push(2);
      queue.push(3);
     
    System.out.println(queue.pop());
     
    queue.push(4);
     
    System.out.println(queue.pop());
      System.out.println(queue.pop());
      System.out.println(queue.pop());
     
    }
     
    }
    View Code

    4. 两个队列实现一个栈

    解法:

    1. 有两个队列q1和q2,先往q1内插入a,b,c,这做的都是栈的push操作。
    2. 现在要做pop操作,即要得到c,这时可以将q1中的a,b两个元素全部dequeue并存入q2中,这时q2中元素为a,b,对q1再做一次dequeue操作即可得到c。
    3. 如果继续做push操作,比如插入d,f,则把d,f插入到q2中,
    4. 此时若要做pop操作,则做步骤2
    5. 以此类推,就实现了用两个队列来实现一个栈的目的。

    注意在此过程中,新push进来的元素总是插入到非空队列中,空队列则用来保存pop操作之后的那些元素,那么此时空队列不为空了,原来的非空队列变为空了,总是这样循环。

    对于push和pop操作,其时间为O(n).

    import java.util.ArrayDeque;
    import java.util.Queue;
     
    /**
    * Created by smyhvae on 2015/9/9.
    */
    public class Stack {
     
    Queue<Integer> queue1 = new ArrayDeque<Integer>();
     Queue<Integer> queue2 = new ArrayDeque<Integer>();
     
    //方法:入栈操作
     public void push(int data) {
      queue1.add(data);
     }
     
    //方法:出栈操作
     public int pop() throws Exception {
      int data;
      if (queue1.size() == 0) {
       throw new Exception("栈为空");
      }
     
    while (queue1.size() != 0) {
       if (queue1.size() == 1) {
        data = queue1.poll();
        while (queue2.size() != 0) { //把queue2中的全部数据放到队列一中
         queue1.add(queue2.poll());
         return data;
        }
       }
       queue2.add(queue1.poll());
      }
      throw new Exception("栈为空");//不知道这一行的代码是什么意思
     }
     
    public static void main(String[] args) throws Exception {
      Stack stack = new Stack();
     
    stack.push(1);
      stack.push(2);
      stack.push(3);
     
    System.out.println(stack.pop());
      System.out.println(stack.pop());
      stack.push(4);
     }
    }
    View Code

    5. 设计含最小函数min()的栈,要求min、push、pop、的时间复杂度都是O(1)。min方法的作用是:就能返回是栈中的最小值

    普通思路:

    一般情况下,我们可能会这么想:利用min变量,每次添加元素时,都和min元素作比较,这样的话,就能保证min存放的是最小值。但是这样的话,会存在一个问题:如果最小的元素出栈了,那怎么知道剩下的元素中哪个是最小的元素呢?

    改进思路:

    这里需要加一个辅助栈,用空间换取时间。辅助栈中,栈顶永远保存着当前栈中最小的数值。具体是这样的:原栈中,每次添加一个新元素时,就和辅助栈的栈顶元素相比较,如果新元素小,就把新元素的值放到辅助栈中,如果新元素大,就把辅助栈的栈顶元素再copy一遍放到辅助栈的栈顶;原栈中,出栈时

    import java.util.Stack;
    public class MinStack {
     
    private Stack<Integer> stack = new Stack<Integer>();
     private Stack<Integer> minStack = new Stack<Integer>(); //辅助栈:栈顶永远保存stack中当前的最小的元素
     
    public void push(int data) {
      stack.push(data); //直接往栈中添加数据
     
    //在辅助栈中需要做判断
      if (minStack.size() == 0 || data < minStack.peek()) {
       minStack.push(data);
      } else {
       minStack.add(minStack.peek()); //【核心代码】peek方法返回的是栈顶的元素
      }
     }
     
    public int pop() throws Exception {
      if (stack.size() == 0) {
       throw new Exception("栈中为空");
      }
     
    int data = stack.pop();
      minStack.pop(); //核心代码
      return data;
     }
     
    public int min() throws Exception {
      if (minStack.size() == 0) {
       throw new Exception("栈中空了");
      }
      return minStack.peek();
     }
     
    public static void main(String[] args) throws Exception {
      MinStack stack = new MinStack();
      stack.push(4);
      stack.push(3);
      stack.push(5);
     
    System.out.println(stack.min());
     }
    }
     
    View Code

    6. 判断栈的push和pop序列是否一致

    通俗一点讲:已知一组数据1、2、3、4、5依次进栈,那么它的出栈方式有很多种,请判断一下给出的出栈方式是否是正确的?

    例如:

    数据:

    1、2、3、4、5

    出栈1:

    5、4、3、2、1(正确)

    出栈2:

    4、5、3、2、1(正确)

    出栈3:

    4、3、5、1、2(错误)

    如果我们希望pop的数字正好是栈顶数字,直接pop出栈即可;如果希望pop的数字目前不在栈顶,我们就到push序列中还没有被push到栈里的数字中去搜索这个数字,并把在它之前的所有数字都push进栈。如果所有的数字都被push进栈仍然没有找到这个数字,表明该序列不可能是一个pop序

    import java.util.Stack;
    
    
    public class StackTest {
    
        //方法:data1数组的顺序表示入栈的顺序。现在判断data2的这种出栈顺序是否正确
        public static boolean sequenseIsPop(int[] data1, int[] data2) {
            Stack<Integer> stack = new Stack<Integer>(); //这里需要用到辅助栈
    
            for (int i = 0, j = 0; i < data1.length; i++) {
                stack.push(data1[i]);
    
                while (stack.size() > 0 && stack.peek() == data2[j]) {
                    stack.pop();
                    j++;
                }
            }
            return stack.size() == 0;
        }
    
        public static void main(String[] args) {
    
            Stack<Integer> stack = new Stack<Integer>();
    
            int[] data1 = {1, 2, 3, 4, 5};
            int[] data2 = {4, 5, 3, 2, 1};
            int[] data3 = {4, 5, 2, 3, 1};
    
            System.out.println(sequenseIsPop(data1, data2));
            System.out.println(sequenseIsPop(data1, data3));
        }
    }
  • 相关阅读:
    Flex 布局语法教程
    2017年总结的前端文章——border属性的多方位应用和实现自适应三角形
    html 里 checkbox里 只要选中就会自动添加checked=“checked”么?
    jQuery遍历DOM
    checkbox 全选操作
    ubuntu下安装jdk
    ubuntu下安装nodejs
    nodejs express route 的用法
    聊天室业务分析
    一般使用场景
  • 原文地址:https://www.cnblogs.com/zxqstrong/p/5311918.html
Copyright © 2011-2022 走看看