225. Implement Stack using Queues Easy
1/**
2 * [225] Implement Stack using Queues
3 *
4 * Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
5 * Implement the MyStack class:
6 *
7 * void push(int x) Pushes element x to the top of the stack.
8 * int pop() Removes the element on the top of the stack and returns it.
9 * int top() Returns the element on the top of the stack.
10 * boolean empty() Returns true if the stack is empty, false otherwise.
11 *
12 * Notes:
13 *
14 * You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty operations are valid.
15 * Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
16 *
17 *
18 * Example 1:
19 *
20 * Input
21 * ["MyStack", "push", "push", "top", "pop", "empty"]
22 * [[], [1], [2], [], [], []]
23 * Output
24 * [null, null, null, 2, 2, false]
25 * Explanation
26 * MyStack myStack = new MyStack();
27 * myStack.push(1);
28 * myStack.push(2);
29 * myStack.top(); // return 2
30 * myStack.pop(); // return 2
31 * myStack.empty(); // return False
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= x <= 9
37 * At most 100 calls will be made to push, pop, top, and empty.
38 * All the calls to pop and top are valid.
39 *
40 *
41 * Follow-up: Can you implement the stack using only one queue?
42 *
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/implement-stack-using-queues/
47// discuss: https://leetcode.com/problems/implement-stack-using-queues/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51struct MyStack {
52 false
53 }
54
55
56/**
57 * `&self` means the method takes an immutable reference.
58 * If you need a mutable reference, change it to `&mut self` instead.
59 */
60impl MyStack {
61
62 fn new() -> Self {
63
64 }
65
66 fn push(&self, x: i32) {
67
68 }
69
70 fn pop(&self) -> i32 {
71
72 }
73
74 fn top(&self) -> i32 {
75
76 }
77
78 fn empty(&self) -> bool {
79
80 }
81}
82
83/**
84 * Your MyStack object will be instantiated and called as such:
85 * let obj = MyStack::new();
86 * obj.push(x);
87 * let ret_2: i32 = obj.pop();
88 * let ret_3: i32 = obj.top();
89 * let ret_4: bool = obj.empty();
90 */
91
92// submission codes end
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_225() {
100 }
101}
102
Back
© 2025 bowen.ge All Rights Reserved.