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