Use dos colas para implementar la función de pila

Pila de implementación de cola

1. Ideas para resolver problemas

  • Primero, aclare las características de la pila y la cola: la pila es primero en entrar , último en salir , y la cola es primero en entrar, primero en salir ;
  1. Suponga que hay dos colas, qu1 y qu2;
  2. Ahora tienes un conjunto de números [12, 23, 45, 55, 35];
  3. Primero ingresa 12, 23 y 34 en la cola qu2, porque lo que se implementa es la función de la pila, y ahora tiene que hacer estallar la pila. La pila es la última en entrar, la primera en salir, por lo que la primera en salir es 34;
  4. Pero ahora 34 está al final de la cola, entonces tienes que mover 12 y 34 a la siguiente cola qu1 primero, y simplemente sacar los 34 restantes de qu2 directamente;
  5. Suponga que quiere jugar 23 ahora, el mismo principio; (siempre que la otra cola esté vacía)
  6. Suponga que aparece 34 y no 23, pero continúa agregando números, luego qué cola debe agregarse;
  7. Ahora que qu1 tiene un número, se unirá a qu1 (qué cola no está vacía, se unirá a cuál);
    Inserte la descripción de la imagen aquí

2. Realización concreta

2.1 Únete al equipo
//入队
    public void push(int x) {
    
    
        //1、谁不为空  入到哪个队列就好了
        //2、两个都为空   放到第一个qu1当中
        if (!qu1.isEmpty()){
    
    
            qu1.offer(x);
        }else if (!qu2.isEmpty()){
    
    
            qu2.offer(x);
        } else {
    
    
            qu1.offer(x);
        }
        usedSize++;
    }
2.2 Salida
在这里插入代码片//出队
    public int pop() {
    
    
        //1.先将前面size-1个移动到空的队列
        //2.将队列最后一个弹出,就是类似于出栈顺序
        //3.反复进行,直到俩个队列都为空的时候出队完毕
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size-1; i++) {
    
    
                qu2.offer(qu1.poll());
            }
            usedSize--;
            return qu1.poll();
        }else{
    
    

            for (int i = 0; i < size-1; i++) {
    
    
                qu1.offer(qu2.poll());
            }
            usedSize--;
            return qu2.poll();
        }

    }
2.3 Pop la parte superior de la pila
 //弹栈顶
    public int top() {
    
    
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int tmp = -1;
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size ; i++) {
    
    
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
        }else{
    
    
            for (int i = 0; i < size; i++) {
    
    
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
        }
        return tmp;
    }
2.4 Determine si está vacío
public boolean empty() {
    
    
        if(qu1.isEmpty() && qu2.isEmpty()){
    
    
            return true;
        }
        return false;
    }

3. Código completo

import java.util.LinkedList;
import java.util.Queue;

//用队列实现栈(需要俩个队)
class MyStack {
    
    

    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
    public int usedSize;


    /** Initialize your data structure here. */
    //构造方法
    public MyStack() {
    
    
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();

    }

    /** Push element x onto stack. */
    //入队
    public void push(int x) {
    
    
        //1、谁不为空  入到哪个队列就好了
        //2、两个都为空   放到第一个qu1当中
        if (!qu1.isEmpty()){
    
    
            qu1.offer(x);
        }else if (!qu2.isEmpty()){
    
    
            qu2.offer(x);
        } else {
    
    
            qu1.offer(x);
        }
        usedSize++;
    }

    /** Removes the element on top of the stack and returns that element. */
    //出队
    public int pop() {
    
    
        //1.先将前面size-1个移动到空的队列
        //2.将队列最后一个弹出,就是类似于出栈顺序
        //3.反复进行,直到俩个队列都为空的时候出队完毕
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size-1; i++) {
    
    
                qu2.offer(qu1.poll());
            }
            usedSize--;
            return qu1.poll();
        }else{
    
    

            for (int i = 0; i < size-1; i++) {
    
    
                qu1.offer(qu2.poll());
            }
            usedSize--;
            return qu2.poll();
        }

    }

    /** Get the top element. */
    //弹栈顶
    public int top() {
    
    
        if (empty()){
    
    //俩个队列都空
            return -1;
        }
        int tmp = -1;
        int size = usedSize;
        if (!qu1.isEmpty()){
    
    
            for (int i = 0; i < size ; i++) {
    
    
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
        }else{
    
    
            for (int i = 0; i < size; i++) {
    
    
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
        }
        return tmp;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
    
    
        if(qu1.isEmpty() && qu2.isEmpty()){
    
    
            return true;
        }
        return false;
    }
}


public class TestDemo2 {
    
    
    public static void main(String[] args) {
    
    
        MyStack myStack = new MyStack();
        myStack.push(10);
        myStack.push(20);
        myStack.push(30);
        System.out.println(myStack.top());
    }
}


Supongo que te gusta

Origin blog.csdn.net/qq_45665172/article/details/110219169
Recomendado
Clasificación