栈和队列的互相转换

2 篇文章 0 订阅
2 篇文章 0 订阅

用两个栈实现一个队列,包括入队,出队,弹出队首元素,判断队列是否为空功能。

import java.util.Stack;

public class StackToQueue<T> {
    private Stack<T> s1 = new Stack<>();
    private Stack<T> s2 = new Stack<>();
    //入队
    public void offer(T val){
        this.s1.push(val);
    }
    //出队
    public T poll(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        if (this.s2.isEmpty()){
            while(!this.s1.isEmpty()){
                T val = this.s1.pop();
                this.s2.push(val);
            }
        }
        return this.s2.pop();
    }
    //弹出队首元素
    public T peek(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        if (this.s2.isEmpty()){
            while(!this.s1.isEmpty()){
                T val = this.s1.pop();
                this.s2.push(val);
            }
        }
        return this.s2.peek();
    }
    //队列是否为空
    public boolean isEmpty(){
        return s1.isEmpty() && s2.isEmpty();
    }
}


来看看测试:

public class TestDemo4 {
    public static void main(String[] args) {
        StackToQueue<Integer> queue = new StackToQueue<>();
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
        System.out.println(queue.peek());
        System.out.println(queue.isEmpty());
        queue.poll();
        queue.poll();
        queue.poll();
        System.out.println(queue.isEmpty());
        queue.poll();
    }
}

在这里插入图片描述

符合预期结果。

用一个队列实现一个栈,包括入栈,出栈,弹出栈顶元素,判断栈是否为空功能

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

public class QueueToStack1<T> {
    private Queue<T> queue = new LinkedList<>();
    //入栈
    public void push(T val){
        this.queue.offer(val);
    }
    //出栈
    public T pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空");
        }
        int size = this.queue.size();
        for (int i = 0; i < size-1; i++) {
            T val = this.queue.poll();
            this.queue.offer(val);
        }
        return this.queue.poll();
    }
    //弹出栈顶元素
    public T peek(){
        if (isEmpty()){
            throw new RuntimeException("栈为空");
        }
        int size = this.queue.size();
        for (int i = 0; i < size-1; i++) {
            T val = this.queue.poll();
            this.queue.offer(val);
        }
        T res = this.queue.poll();
        this.queue.offer(res);
        return res;
    }
    //判断栈是否为空
    public boolean isEmpty(){
        return this.queue.isEmpty();
    }
}


来看看测试:

public class TestDemo5 {
    public static void main(String[] args) {
        QueueToStack1<Integer> s = new QueueToStack1<>();
        s.push(1);
        s.push(2);
        s.push(3);
        System.out.println(s.peek());
        System.out.println(s.isEmpty());
        s.pop();
        s.pop();
        s.pop();
        System.out.println(s.isEmpty());
        System.out.println(s.peek());
    }
}


在这里插入图片描述

符合预期结果。

用两个队列实现一个栈,包括入栈,出栈,弹出栈顶元素,判断栈是否为空功能

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

public class QueueToStack2 <T>{
    private Queue<T> q1 = new LinkedList<>();
    private Queue<T> q2 = new LinkedList<>();
    //入栈
    public void push(T val){
       if (!q2.isEmpty() && q1.isEmpty()){
           q1.offer(val);
           while(!q2.isEmpty()){
               T res = q2.poll();
               q1.offer(res);
           }
       }else if(q2.isEmpty() && !q1.isEmpty()){
           q2.offer(val);
           while(!q1.isEmpty()){
               T res = q1.poll();
               q2.offer(res);
           }
       }else if(q2.isEmpty() && q1.isEmpty()){
           q1.offer(val);
       }
    }
    //出栈
    public T pop(){
        if (isEmpty()){
            throw new RuntimeException("栈为空");
        }else if (!this.q1.isEmpty()){
            return this.q1.poll();
        }else{
            return this.q2.poll();
        }
    }
    //弹出栈顶元素
    public T peek(){
        if (this.q1.isEmpty() && this.q2.isEmpty()){
            throw new RuntimeException("栈为空");
        }else if (!this.q1.isEmpty()){
            return this.q1.peek();
        }else{
            return this.q2.peek();
        }
    }
    //判断栈是否为空
    public boolean isEmpty(){
        return this.q1.isEmpty() && this.q2.isEmpty();
    }
}

来看看测试:

public class TestDemo6 {
    public static void main(String[] args) {
        QueueToStack2<Integer> s = new QueueToStack2<>();
        s.push(1);
        s.push(2);
        s.push(3);
        System.out.println(s.peek());
        System.out.println(s.isEmpty());
        s.pop();
        s.pop();
        s.pop();
        System.out.println(s.isEmpty());
        s.peek();
    }

}

在这里插入图片描述

符合预期结果。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋丹尼尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值