forked from alexfertel/rust-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.rs
113 lines (88 loc) · 2.44 KB
/
stack.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// a vector-based implementation of the stack data type
pub struct Stack<T> {
vec: Vec<T>,
}
impl<T> Stack<T> {
// a constructor that returns an instance of Stack<T> backed by an empty Vec<T>
pub fn new() -> Self {
Stack { vec: Vec::new() }
}
// mutates the stack by adding an item of type T and returns true
pub fn push(&mut self, item: T) -> bool {
self.vec.push(item);
true
}
// returns the number of elements in the stack
pub fn len(&self) -> usize {
self.vec.len()
}
// returns true if stack is empty else false
pub fn is_empty(&self) -> bool {
self.vec.is_empty()
}
// returns a Some<&T> if stack is non-empty else None
// &T is an immutable reference to an element of type T
pub fn peek(&self) -> Option<&T> {
self.vec.last()
}
// mutates the stack by removing and returning a Some<T>
// if stack is non-empty else None
pub fn pop(&mut self) -> Option<T> {
self.vec.pop()
}
}
#[cfg(test)]
mod tests {
use super::Stack;
#[test]
fn starts_empty() {
let mut q: Stack<String> = Stack::new();
assert!(q.is_empty());
assert_eq!(q.pop(), None);
assert_eq!(q.len(), 0);
assert_eq!(q.peek(), None);
}
#[test]
fn fills_and_empties() {
let mut q = Stack::new();
q.push(1);
q.push(2);
q.push(3);
assert!(!q.is_empty());
assert_eq!(q.len(), 3);
assert_eq!(q.peek(), Some(&3));
q.pop();
q.pop();
q.pop();
assert!(q.is_empty());
assert_eq!(q.pop(), None);
assert_eq!(q.len(), 0);
assert_eq!(q.peek(), None);
}
#[test]
fn alternate_mutations() {
let mut q = Stack::new();
q.push("A");
assert!(!q.is_empty());
assert_eq!(q.len(), 1);
assert_eq!(q.peek(), Some(&"A"));
q.push("B");
assert!(!q.is_empty());
assert_eq!(q.len(), 2);
assert_eq!(q.peek(), Some(&"B"));
q.pop();
assert!(!q.is_empty());
assert_eq!(q.len(), 1);
assert_eq!(q.peek(), Some(&"A"));
q.push("C");
q.push("D");
assert!(!q.is_empty());
assert_eq!(q.len(), 3);
assert_eq!(q.peek(), Some(&"D"));
q.pop();
q.pop();
assert!(!q.is_empty());
assert_eq!(q.len(), 1);
assert_eq!(q.peek(), Some(&"A"));
}
}