- 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)]
fnmain(){}
structSolution;
/**
* 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();
*/
structMyQueue{
data:Vec<i32>,
}
/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
implMyQueue{
/** Initialize your data structure here. */
fnnew() -> Self{
Self{data:vec![]}
}
/** Push element x to the back of queue. */
fnpush(&mutself,x:i32){
self.data.push(x);
}
/** Removes the element from in front of queue and returns that element. */
fnpop(&mutself) -> i32{
let data = self.peek();
self.data = self.data[1..].to_vec();
data
}
/** Get the front element. */
fnpeek(&mutself) -> i32{
let data = self.data[0];
data
}
/** Returns whether the queue is empty. */
fnempty(&self) -> bool{
self.data.len() == 0
}
}