-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimplement-queue-using-stacks.rs
51 lines (43 loc) · 1.12 KB
/
implement-queue-using-stacks.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
51
#![allow(dead_code, unused, unused_variables)]
fn main() {}
struct Solution;
/**
* Your MyQueue object will be instantiated and called as such:
* let obj = MyQueue::new();
* obj.push(x);
* let ret_2: i32 = obj.pop();
* let ret_3: i32 = obj.peek();
* let ret_4: bool = obj.empty();
*/
struct MyQueue {
data: Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MyQueue {
/** Initialize your data structure here. */
fn new() -> Self {
Self { data: vec![] }
}
/** Push element x to the back of queue. */
fn push(&mut self, x: i32) {
self.data.push(x);
}
/** Removes the element from in front of queue and returns that element. */
fn pop(&mut self) -> i32 {
let data = self.peek();
self.data = self.data[1..].to_vec();
data
}
/** Get the front element. */
fn peek(&mut self) -> i32 {
let data = self.data[0];
data
}
/** Returns whether the queue is empty. */
fn empty(&self) -> bool {
self.data.len() == 0
}
}