forked from algorithm-archivists/algorithm-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.rs
43 lines (34 loc) · 881 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
use std::collections::VecDeque;
struct Queue<T> {
list: VecDeque<T>
}
impl<T> Queue<T> {
fn new() -> Self {
Queue{
list: VecDeque::new(),
}
}
// Note that this returns a reference to the value
// This is in contrast to dequeue which gives ownership of the value
fn front(&self) -> Option<&T> {
self.list.front()
}
fn dequeue(&mut self) -> Option<T> {
self.list.pop_front()
}
fn enqueue(&mut self, item: T) {
self.list.push_back(item);
}
fn size(&self) -> usize {
self.list.len()
}
}
fn main() {
let mut i32queue = Queue::new();
i32queue.enqueue(4);
i32queue.enqueue(5);
i32queue.enqueue(6);
println!("{:?}", i32queue.dequeue().unwrap()); // 4
println!("{:?}", i32queue.size()); // 2
println!("{:?}", i32queue.front().unwrap()); // 5
}