用队列实现栈,用栈实现队列

用两个栈实现队列:
思想:比如 栈stack1,栈stack2,
向stack1中push元素:1、2、3、4、5、6
将stack1中的元素都出栈pop,然后push进stack2中:6、5、4、3、2、1;
这样stack1中无元素,stack2中元素:6、5、4、3、2、1,
然后出栈的时候从stack2中出,1就会先出栈;这样就实现了先进先出的效果;

import java.util.Stack;

/*
* 用两个栈实现一个队列
* */
public class QueueByStack {
    Stack<Integer> stack1=new Stack<>();
    Stack<Integer> stack2=new Stack<>();

    public void add(Integer num){
        stack1.push(num);
    }
    public Integer poll(){

        /*当第二个栈stack2是空时,将栈stack1中的元素全部出栈放到stack2中,此时最先进入到栈stack1中的元素就会被放到最顶端,就会在stack2中先出栈*/
        if (stack2.isEmpty()){
            while (!stack1.isEmpty()) {

                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
    public Integer size(){
        return stack1.size()+stack2.size();
    }

    public Integer peek(){

        /*栈stack2中的栈顶元素就是最先进入栈stack1中的元素*/
        if (stack2.isEmpty()){
            while (!stack1.isEmpty()) {

                stack2.push(stack1.pop());
            }

        }
        return stack2.peek();
    }
    public static void main(String[] args) {

        QueueByStack queue=new QueueByStack();
        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);
        queue.add(5);

        System.out.println("出队列:"+queue.poll());
        System.out.println("出队列:"+queue.poll());
        System.out.println("出队列:"+queue.poll());
        
        queue.add(6);
        queue.add(7);
        System.out.println("队列长度"+queue.size());
        System.out.println("队列顶部元素:"+queue.peek());

        System.out.println("出队列:"+queue.poll());
        System.out.println("出队列:"+queue.poll());
        System.out.println("出队列:"+queue.poll());



    }
}

结果:
在这里插入图片描述
用一个队列实现栈:
思想:队列queue1;
向队列中add元素:1、2、3、4、5、6
要想实现先进后出的效果,就将1、2、3、4、5(除了最后一个元素),这前面的元素都先出(poll)队列,然后再add进队列;
队列中元素就变成了6、5、4、3、2、1;这样子出元素时,就是6先出(最后进入的先出),实现了队列的效果;
每一次出队列的时候都这样进行;

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

/*
* 用队列实现栈
*
* */
public class StackByQueue {

    Queue<Integer> queue=new LinkedList<>();
    public void push(Integer num){
              queue.add(num);
    }

    public Integer pop(){


        /*将队列前面的元素(除了最后一个元素)都先出队列,再进队列,这样最后一个元素就变成了队列中的首位,这样子,最后一个进队列的就会先出队列*/
       for(int i=0;i<queue.size()-1;i++) {
            queue.add(queue.poll());
        }
        return queue.poll();
    }
    public Integer size(){
        return queue.size();
    }
    public Integer peek(){
        if(queue.size()==0){
            return null;
        }
        /*将队列前面的元素(除了最后一个元素)都先出队列,再进队列,这样子最后一个元素就会成为队列的首元素*/
        for(int i=0;i<queue.size()-1;i++) {
            queue.add(queue.poll());
        }
        Integer result=queue.peek();//此时的栈顶元素
        queue.add(queue.poll());//要再将栈顶元素还原到它原本的位置(队列中最后),要不然在进行pop操作时,就会进行两次上面的“出队列,再进队”列操作
        return result;
    }

    public static void main(String[] args) {

         StackByQueue stack=new StackByQueue();
         stack.push(1);
         stack.push(2);
         stack.push(3);
         stack.push(4);
        System.out.println("出栈:"+stack.pop());
        System.out.println("出栈:"+stack.pop());

        System.out.println("栈顶元素:"+stack.peek());
        System.out.println("栈的长度:"+stack.size());

        System.out.println("出栈:"+stack.pop());
         stack.push(5);
         stack.push(6);
        System.out.println("出栈:"+stack.pop());
    }
}

结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值