两个队列实现一个栈 + 两个栈实现一个队列

面试中常出现让你手写两个队列实现一个栈,两个栈实现一个队列的问题,很是头疼!今天就仔细将我分析,思考过的Java代码给大家分享一下:(一)两个队列实现一个栈:

 

两个队列添加元素,哪个队列为空,由于在输出元素时,要进行相应元素的移动(除去尾部元素),所以要在对应不为空的队列进行元素的添加;在输出数据时,要进行两个队列的变相操作,不为空的队列要依次向为空的队列中添加元素,直到尾元素输出即可!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * 两个队列实现一个栈
 * @auther yangchao
 * @date 2019/7/18
 */
 
public class TwoQueueImplStack {
 
    private Queue<Integer> queue1 = new ArrayDeque<>();
 
    private Queue<Integer> queue2 = new ArrayDeque<>();
 
    /**
     * 向栈中压入数据
     * @param element
     */
    public void push(Integer element) {
        //两个队列为空时,优先考虑queue1
        if (queue1.isEmpty() && queue2.isEmpty()) {
            queue1.add(element);
            return;
        }
 
        //如果queue1为空,queue2有数据,直接放入queue2
        if (queue1.isEmpty()) {
            queue2.add(element);
            return;
        }
 
        //如果queue1为空,queue2有数据,直接放入queue2
        if (queue2.isEmpty()) {
            queue1.add(element);
            return;
        }
    }
 
    /**
     * 取出栈中的数据
     * @return
     */
    public Integer poll() {
        //两个队列为空时,直接抛出异常
        if (queue1.isEmpty() && queue2.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
 
        //如果queue1为空,将queue2中的元素依次加入到 queue1, 弹出最后一个元素
        if (queue1.isEmpty()) {
            while(queue2.size() > 1) {
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }
 
        //如果queue2为空,将queue1中的元素依次加入到 queue2, 弹出最后一个元素
        if (queue2.isEmpty()) {
            while(queue1.size() > 1) {
                queue2.add(queue1.poll());
            }
            return queue1.poll();
        }
        return null;
    }
 
    public static void main(String[] args) {
        TwoQueueImplStack qs = new TwoQueueImplStack();
        qs.push(2);
        qs.push(4);
        qs.push(7);
        qs.push(5);
        System.out.println(qs.poll());
        System.out.println(qs.poll());
 
        qs.push(1);
        System.out.println(qs.poll());
    }
 
}

输出结果:

(二)两个栈实现一个队列:

 

第一个栈只负责添加元素,第二个栈在弹出元素时,首先判断当前栈是否为空,若为空就直接将其第一个栈中的数据全部压入第二个栈中,然后输出栈顶元素,即可实现队列效果;若第二个栈中有数据,添加直接将其数据压入第一个栈中,输出时直接输出第二个栈顶的元素即可!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * 两个栈实现一个队列
 * @auther yangchao
 * @date 2019/7/18
 */
 
public class TwoStackImplQueue {
 
    private Stack<Integer> stack1 = new Stack<>();
 
    private Stack<Integer> stack2 = new Stack<>();
 
    /**
     * stack1只负责压入队列元素
     * @param element
     */
    public void push(Integer element) {
        stack1.add(element);
    }
 
    /**
     * 取出队列顶部元素
     * @return
     */
    public Integer poll() {
        //若stack2为空,将 stack1 中的元素压入 stack2
        if (stack2.isEmpty()) {
            while (stack1.size() > 0) {
                stack2.add(stack1.pop());
            }
        }
        if (stack2.isEmpty()) {
            throw new RuntimeException("queue is Empty!");
        }
        Integer head = stack2.pop();
        return head;
    }
 
    public static void main(String[] args) {
        TwoStackImplQueue sq = new TwoStackImplQueue();
        sq.push(1);
        sq.push(3);
        sq.push(5);
        sq.push(4);
        sq.push(2);
 
        System.out.println(sq.poll());
        System.out.println(sq.poll());
 
        sq.push(7);
        System.out.println(sq.poll());
    }
 
}

输出结果:

  每天进步一点点,继续加油......

 

posted @   菜鸟的奋斗之路  阅读(5793)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示