Skip to content

Commit

Permalink
feat: modified queue to use VecDeque instead of Vec (#98)
Browse files Browse the repository at this point in the history
Also removed pointless return value of Queue::enqueue (if it fails it panics)
  • Loading branch information
Daniel729 authored Oct 9, 2024
1 parent efcd869 commit 06c906d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/data_structures/queue.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::collections::VecDeque;

pub struct Queue<T> {
vec: Vec<T>,
vec: VecDeque<T>,
}

impl<T> Queue<T> {
pub fn new() -> Self {
Queue { vec: Vec::new() }
Queue {
vec: VecDeque::new(),
}
}

pub fn enqueue(&mut self, item: T) -> bool {
self.vec.push(item);
true
pub fn enqueue(&mut self, item: T) {
self.vec.push_back(item);
}

pub fn len(&self) -> usize {
Expand All @@ -21,15 +24,11 @@ impl<T> Queue<T> {
}

pub fn peek(&self) -> Option<&T> {
self.vec.first()
self.vec.front()
}

pub fn dequeue(&mut self) -> Option<T> {
if self.is_empty() {
None
} else {
Some(self.vec.remove(0))
}
self.vec.pop_front()
}
}

Expand Down

0 comments on commit 06c906d

Please sign in to comment.