栈和队列编程题(基于java实现)

文章介绍了如何通过两个队列实现栈的功能,以及如何用两个栈实现队列,展示了数据结构中栈和队列基本操作的灵活运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

之前讲述了栈和队列相关操作,现在能否我们使用栈来实现队列和用队列实现栈呢?(基于单向队列和栈)


一、代码实现

1.队列实现栈

一个栈无法实现队列的功能(如果能,这两者也没必要分开讲述了,直接替代不就行了么),所以我们这里用两个队列来实现栈
代码如下(示例):

public class MyStack2 {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;     //创建两个队列
    public MyStack2(){
        qu1= new LinkedList<>();
        qu2= new LinkedList<>();     //初始化
    }
    public void push(int x){
        if(!qu1.isEmpty()){            //入不为空的队列
            qu1.offer(x);
        }else  if (!qu2.isEmpty()){
            qu2.offer(x);
        }else {
            qu1.offer(x);
        }
    }
    public int pop(){
        if (empty()){
            return -1; //两个队列都为空 当前栈为空
        }
        if (!qu1.isEmpty()){
            int size =qu1.size();
            for (int i = 0; i< size-1;i++){        //弹出size-1个数到另外一个队列,然后最后出的元素就是栈先出的元素
                int val = qu1.poll();
                qu2.offer(val);
            }
            return qu1.poll();
        }else {
                int size =qu2.size();
                for (int i = 0; i< size-1;i++){
                    int val = qu2.poll();
                    qu1.offer(val);
                }
                return qu2.poll();
        }
    }
    public boolean empty(){
        return  qu1.isEmpty() && qu2.isEmpty();
    }
    //peek
    public int top(){
        if (empty()){
            return -1; //两个队列都为空 当前栈为空
        }
        if (!qu1.isEmpty()){
            int size =qu1.size();
            int val = -1;
            for (int i = 0; i< size;i++){
                val = qu1.poll();
                qu2.offer(val);
            }
            return val;
        }else {
            int size =qu2.size();
            int val =-1;
            for (int i = 0; i< size;i++){
                 val = qu2.poll();
                qu1.offer(val);
            }
            return val;
        }
    }
}

2.栈实现队列

代码如下(示例):

public class MyQueue2 {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;    //创建两个栈
    public MyQueue2(){
        stack1 = new Stack<>();
        stack2 = new Stack<>();             //初始化
    }
    public void push(int x){
        stack1.push(x);
    }
    public int pop(){
        if(empty()){
            return -1;
        }
        if (stack2.empty()){
            while (!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
    public int peek(){
        if(empty()){
            return -1;
        }
        if (stack2.empty()){
            while (!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();

    }
    public boolean empty(){
        return stack1.empty() && stack2.empty();
    }
}

总结

栈和队列无非就是谁先进,谁先出的问题。用两个同类型的数据结构,相互挪动元素,本质上就能改变其进出顺序,从而实现对方的功能。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值