Skip to content

Commit 157c14a

Browse files
authored
Improve Queue Implementation (TheAlgorithms#791)
ref: improve queue implementation
1 parent 6287ba6 commit 157c14a

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

src/data_structures/queue.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! This module provides a generic `Queue` data structure, implemented using
2+
//! Rust's `LinkedList` from the standard library. The queue follows the FIFO
3+
//! (First-In-First-Out) principle, where elements are added to the back of
4+
//! the queue and removed from the front.
5+
16
use std::collections::LinkedList;
27

38
#[derive(Debug)]
@@ -6,33 +11,50 @@ pub struct Queue<T> {
611
}
712

813
impl<T> Queue<T> {
14+
// Creates a new empty Queue
915
pub fn new() -> Queue<T> {
1016
Queue {
1117
elements: LinkedList::new(),
1218
}
1319
}
1420

21+
// Adds an element to the back of the queue
1522
pub fn enqueue(&mut self, value: T) {
1623
self.elements.push_back(value)
1724
}
1825

26+
// Removes and returns the front element from the queue, or None if empty
1927
pub fn dequeue(&mut self) -> Option<T> {
2028
self.elements.pop_front()
2129
}
2230

31+
// Returns a reference to the front element of the queue, or None if empty
2332
pub fn peek_front(&self) -> Option<&T> {
2433
self.elements.front()
2534
}
2635

36+
// Returns a reference to the back element of the queue, or None if empty
37+
pub fn peek_back(&self) -> Option<&T> {
38+
self.elements.back()
39+
}
40+
41+
// Returns the number of elements in the queue
2742
pub fn len(&self) -> usize {
2843
self.elements.len()
2944
}
3045

46+
// Checks if the queue is empty
3147
pub fn is_empty(&self) -> bool {
3248
self.elements.is_empty()
3349
}
50+
51+
// Clears all elements from the queue
52+
pub fn drain(&mut self) {
53+
self.elements.clear();
54+
}
3455
}
3556

57+
// Implementing the Default trait for Queue
3658
impl<T> Default for Queue<T> {
3759
fn default() -> Queue<T> {
3860
Queue::new()
@@ -44,55 +66,26 @@ mod tests {
4466
use super::Queue;
4567

4668
#[test]
47-
fn test_enqueue() {
48-
let mut queue: Queue<u8> = Queue::new();
49-
queue.enqueue(64);
50-
assert!(!queue.is_empty(), "Queue should not be empty after enqueue");
51-
}
52-
53-
#[test]
54-
fn test_dequeue() {
55-
let mut queue: Queue<u8> = Queue::new();
56-
queue.enqueue(32);
57-
queue.enqueue(64);
58-
let retrieved_dequeue = queue.dequeue();
59-
assert_eq!(
60-
retrieved_dequeue,
61-
Some(32),
62-
"Dequeue should return the first element"
63-
);
64-
}
69+
fn test_queue_functionality() {
70+
let mut queue: Queue<usize> = Queue::default();
6571

66-
#[test]
67-
fn test_peek_front() {
68-
let mut queue: Queue<u8> = Queue::new();
72+
assert!(queue.is_empty());
6973
queue.enqueue(8);
7074
queue.enqueue(16);
71-
let retrieved_peek = queue.peek_front();
72-
assert_eq!(
73-
retrieved_peek,
74-
Some(&8),
75-
"Peek should return a reference to the first element"
76-
);
77-
}
75+
assert!(!queue.is_empty());
76+
assert_eq!(queue.len(), 2);
7877

79-
#[test]
80-
fn test_size() {
81-
let mut queue: Queue<u8> = Queue::new();
82-
queue.enqueue(8);
83-
queue.enqueue(16);
84-
assert_eq!(
85-
2,
86-
queue.len(),
87-
"Queue length should be equal to the number of enqueued elements"
88-
);
89-
}
78+
assert_eq!(queue.peek_front(), Some(&8));
79+
assert_eq!(queue.peek_back(), Some(&16));
9080

91-
#[test]
92-
fn test_is_empty() {
93-
let mut queue: Queue<u8> = Queue::new();
94-
assert!(queue.is_empty(), "Newly created queue should be empty");
95-
queue.enqueue(8);
96-
assert!(!queue.is_empty(), "Queue should not be empty after enqueue");
81+
assert_eq!(queue.dequeue(), Some(8));
82+
assert_eq!(queue.len(), 1);
83+
assert_eq!(queue.peek_front(), Some(&16));
84+
assert_eq!(queue.peek_back(), Some(&16));
85+
86+
queue.drain();
87+
assert!(queue.is_empty());
88+
assert_eq!(queue.len(), 0);
89+
assert_eq!(queue.dequeue(), None);
9790
}
9891
}

0 commit comments

Comments
 (0)