-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimplement-stack-using-queues.rs
50 lines (42 loc) · 1.14 KB
/
implement-stack-using-queues.rs
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
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
/**
* Your MyStack object will be instantiated and called as such:
* let obj = MyStack::new();
* obj.push(x);
* let ret_2: i32 = obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: bool = obj.empty();
*/
struct MyStack {
data: std::cell::RefCell<Vec<i32>>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyStack {
/** Initialize your data structure here. */
fn new() -> Self {
Self {
data: std::cell::RefCell::new(Vec::new()),
}
}
/** Push element x onto stack. */
fn push(&self, x: i32) {
self.data.borrow_mut().push(x);
}
/** Removes the element on top of the stack and returns that element. */
fn pop(&self) -> i32 {
self.data.borrow_mut().pop().unwrap()
}
/** Get the top element. */
fn top(&self) -> i32 {
*self.data.borrow_mut().last().unwrap()
}
/** Returns whether the stack is empty. */
fn empty(&self) -> bool {
self.data.borrow().len() == 0
}
}