【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();
     }
    

    }

相关推荐
东方芷兰2 分钟前
算法笔记 04 —— 算法初步(下)
c++·笔记·算法
JNU freshman4 分钟前
图论 之 迪斯科特拉算法求解最短路径
算法·图论
魔道不误砍柴功15 分钟前
Java中的Stream API:从入门到实战
java·windows·python
晚安72018 分钟前
idea添加web工程
java·前端·intellij-idea
xinghuitunan20 分钟前
时间转换(acwing)c/c++/java/python
java·c语言·c++·python
青松@FasterAI26 分钟前
【NLP算法面经】本科双非,头条+腾讯 NLP 详细面经(★附面题整理★)
人工智能·算法·自然语言处理
八月五28 分钟前
Java并发编程——ThreadLocal
java·并发编程
旅僧1 小时前
代码随想录-- 第一天图论 --- 岛屿的数量
算法·深度优先·图论
Emplace1 小时前
ABC381E题解
c++·算法