栈-队和队-栈

  • 用两个栈实现队列的操作。

         因为栈是单边的,而队列是双边的,所以两个栈,需要一个作为入队,一个作为出队。

         入队:判断secondStack是否为空,若是空,就将数据直接存到firstStack;若不是空,就将secondStack的数据全部入栈到firstStack,然后数据入栈。

         出队:判断SecondStack是否而空,如果不是的,就直接SecondStack出栈,如果是的,就判断firstStack是否为空,若不是,将firstStack出栈到到SecondStack,SecondStack在出栈。

         这样设计的好处是每一次操作完必有一个栈是空的,下一次出队的时候,不需要设计到两个栈。

  • 用两个队列实现栈

         队是双边的,栈是单边的。所以入栈的操作,就是找到不是空的对,入队;出栈的操作稍微复杂,将不是空的对全部出队到是空的队,除了第一个,再将第一个输出,就可以了。

         入栈:找到不是空的的队,入队;

         出栈:找到不是空的队,全部出队到空的队,然后把最后一个出队的输出。

         一样的,可以保证每一次操作都只涉及到一个队。

import java.util.Stack;

public class MyQueue {
    private Stack<String> stackFirst = new Stack<String>();
    private Stack<String> stackSecond = new Stack<String>();
    
    public boolean offer(String str){
        if(stackSecond.empty()){
            stackFirst.push(str);
        }else{
            while(!stackSecond.empty()){
                stackFirst.push(stackSecond.pop());
            }
            stackFirst.push(str);
        }
        return true;
    }
    
    public String poll(){
        if(!stackSecond.empty()){
            return stackSecond.pop();
        }else{
            while(!stackFirst.empty()){
                stackSecond.push(stackFirst.pop());
            }
            return stackSecond.pop();
        }
    }
    
    public boolean empty(){
        if(stackFirst.empty() && stackSecond.empty())
            return true;
        return false;
    }
    
    public static void main(String[] args){
        MyQueue queue = new MyQueue();
        queue.offer("hello ");
        queue.offer("baby ");
        queue.offer("!");
        
        while(!queue.empty()){
            System.out.print(queue.poll());
        }
    }
}
import java.util.LinkedList;
import java.util.Queue;


public class MyStack {
    private Queue<String> queueFirst = new LinkedList<String>();
    private Queue<String> queueSecond = new LinkedList<String>();
    
    public String push(String str){
        if(queueSecond.isEmpty()){
            queueFirst.offer(str);
        }else if(queueFirst.isEmpty()){
            queueSecond.offer(str);
        }
        return str;
    }
    
    public String pop(){
        if(!queueFirst.isEmpty()){
            while(queueFirst.size() > 1){
                queueSecond.offer(queueFirst.poll());
            }
            return queueFirst.poll();
        }else if(!queueSecond.isEmpty()){
            while(queueSecond.size() > 1){
                queueFirst.offer(queueSecond.poll());
            }
            return queueSecond.poll();
        }
        return null;
    }
    
    public boolean empty(){
        if(queueFirst.isEmpty() && queueSecond.isEmpty())
            return true;
        return false;
    }
    
    public static void main(String[] args){
        MyStack stack = new MyStack();
        stack.push("hello");
        stack.push("baby");
        stack.push("!");
        
        while(!stack.empty()){
            System.out.print(stack.pop());
        }
    }
}
原文地址:https://www.cnblogs.com/lxq0309/p/3677044.html