【Java--数据结构】队列与栈的相互成就

欢迎关注个人主页:逸狼


创造不易,可以点点赞吗~

如有错误,欢迎指出~



目录

 用队列实现栈

用栈实现队列


 用队列实现栈

oj链接

一个队列是无法实现栈的

入栈push:把数据放到不为空的队列当中。

注意:第一次入栈时,两个队列都为空,规定放到第一个队列中。 

出栈pop:把一个队列的n-1个元素放到另一个队列中,此时第一个队列中的元素就是要出栈的元素

判空empty:两个队列同时为空时为空。

获取栈顶元素top:与出栈类似,将队列中n个元素放到另一个中,并且用ret接收每一个元素的值(会覆盖前一个值),直到队列为空时,最后一个ret的值为栈顶元素。

top示意图: 

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

class MyStackUsQueue {
    public Queue<Integer> queue1;
    public Queue<Integer> queue2;

    public MyStackUsQueue() {
        queue1=new LinkedList<>();
        queue2=new LinkedList<>();
    }
    //入栈
    public void push(int x) {
        if (empty()){
            queue1.offer(x);
            return;
        }
        if (!queue1.isEmpty()){
            queue1.offer(x);
        }else{
            queue2.offer(x);
        }
    }
    //出栈
    public int pop() {
        if (empty()) {
            return -1;
        }
            if(!queue1.isEmpty()){
                int size=queue1.size();
                for (int i = 0; i < size-1; i++) {
                    queue2.offer(queue1.poll());//出了n-1个元素
                }
                return queue1.poll();
            }else{
                    int size = queue2.size();
                    for (int i = 0; i < size - 1; i++) {
                        queue1.offer(queue2.poll());//出了n-1个元素
                    }
                    return queue2.poll();
            }
    }
    //获取栈顶元素
    public int top() {
        if (empty()) {
            return -1;
        }
        if(!queue1.isEmpty()){
            int size=queue1.size();
            int ret=-1;//
            for (int i = 0; i < size; i++) {
                ret=queue1.poll();//每出一个元素,都存一份
                queue2.offer(ret);//出了n个元素
            }
            return ret;
        }else{
            int size = queue2.size();
            int ret=-1;
            for (int i = 0; i < size ; i++) {
                ret=queue2.poll();
                queue1.offer(ret);
            }
            return ret;
        }
    }
    //判空
    public boolean empty() {
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */

用栈实现队列

oj链接

入队列:直接放到s1栈中。 

出队列:

  1. 入栈,放到s1栈中
  2. 出栈,把s1中所有元素全部放入s2栈中,将s2的元素出栈
  3. 如果s2里面是空的,就要把s1中的元素放入s2。
import java.util.Stack;

class MyQueueUsStack {
    public Stack<Integer> stack1;
    public Stack<Integer> stack2;

    public MyQueueUsStack() {
        stack1=new Stack<>();
        stack2=new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if (empty()){//队列为空
            return -1;
        }
        //走到这里,说明s1中一定有元素
        if(stack2.empty()){
            while(!stack1.empty()){//将s1中的所有元素放入s2中
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
    
    public int peek() {
        if (empty()){//队列为空
            return -1;
        }
        //走到这里,说明s1中一定有元素
        if(stack2.empty()){
            while(!stack1.empty()){//将s1中的所有元素放入s2中
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }
    
    public boolean empty() {
        return stack1.empty()&&stack2.empty();
    }
}

  • 17
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值