forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.rs
46 lines (39 loc) · 832 Bytes
/
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
#[derive(Debug)]
struct Pilha<T> {
pilha: Vec<T>,
}
impl<T> Pilha<T> {
fn new() -> Self {
Pilha { pilha: Vec::new() }
}
fn length(&self) -> usize {
self.pilha.len()
}
fn push(&mut self, item: T) {
self.pilha.push(item)
}
fn pop(&mut self) -> Option<T> {
self.pilha.pop()
}
fn is_empty(&self) -> bool {
self.pilha.is_empty()
}
fn peek(&self) -> Option<&T> {
self.pilha.first()
}
}
fn main() {
let mut pilha: Pilha<i32> = Pilha::<i32>::new();
pilha.push(1);
pilha.push(2);
println!("{:?}", pilha);
pilha.pop();
println!("{:?}", pilha);
println!(
"length: {:?}, is empty? {:?}",
pilha.length(),
pilha.is_empty()
);
pilha.push(3);
println!("{:?}", pilha.peek());
}