diff --git a/src/data_structures/queue.rs b/src/data_structures/queue.rs index 565583cca91..a0299155490 100644 --- a/src/data_structures/queue.rs +++ b/src/data_structures/queue.rs @@ -1,3 +1,8 @@ +//! This module provides a generic `Queue` data structure, implemented using +//! Rust's `LinkedList` from the standard library. The queue follows the FIFO +//! (First-In-First-Out) principle, where elements are added to the back of +//! the queue and removed from the front. + use std::collections::LinkedList; #[derive(Debug)] @@ -6,33 +11,50 @@ pub struct Queue { } impl Queue { + // Creates a new empty Queue pub fn new() -> Queue { Queue { elements: LinkedList::new(), } } + // Adds an element to the back of the queue pub fn enqueue(&mut self, value: T) { self.elements.push_back(value) } + // Removes and returns the front element from the queue, or None if empty pub fn dequeue(&mut self) -> Option { self.elements.pop_front() } + // Returns a reference to the front element of the queue, or None if empty pub fn peek_front(&self) -> Option<&T> { self.elements.front() } + // Returns a reference to the back element of the queue, or None if empty + pub fn peek_back(&self) -> Option<&T> { + self.elements.back() + } + + // Returns the number of elements in the queue pub fn len(&self) -> usize { self.elements.len() } + // Checks if the queue is empty pub fn is_empty(&self) -> bool { self.elements.is_empty() } + + // Clears all elements from the queue + pub fn drain(&mut self) { + self.elements.clear(); + } } +// Implementing the Default trait for Queue impl Default for Queue { fn default() -> Queue { Queue::new() @@ -44,55 +66,26 @@ mod tests { use super::Queue; #[test] - fn test_enqueue() { - let mut queue: Queue = Queue::new(); - queue.enqueue(64); - assert!(!queue.is_empty(), "Queue should not be empty after enqueue"); - } - - #[test] - fn test_dequeue() { - let mut queue: Queue = Queue::new(); - queue.enqueue(32); - queue.enqueue(64); - let retrieved_dequeue = queue.dequeue(); - assert_eq!( - retrieved_dequeue, - Some(32), - "Dequeue should return the first element" - ); - } + fn test_queue_functionality() { + let mut queue: Queue = Queue::default(); - #[test] - fn test_peek_front() { - let mut queue: Queue = Queue::new(); + assert!(queue.is_empty()); queue.enqueue(8); queue.enqueue(16); - let retrieved_peek = queue.peek_front(); - assert_eq!( - retrieved_peek, - Some(&8), - "Peek should return a reference to the first element" - ); - } + assert!(!queue.is_empty()); + assert_eq!(queue.len(), 2); - #[test] - fn test_size() { - let mut queue: Queue = Queue::new(); - queue.enqueue(8); - queue.enqueue(16); - assert_eq!( - 2, - queue.len(), - "Queue length should be equal to the number of enqueued elements" - ); - } + assert_eq!(queue.peek_front(), Some(&8)); + assert_eq!(queue.peek_back(), Some(&16)); - #[test] - fn test_is_empty() { - let mut queue: Queue = Queue::new(); - assert!(queue.is_empty(), "Newly created queue should be empty"); - queue.enqueue(8); - assert!(!queue.is_empty(), "Queue should not be empty after enqueue"); + assert_eq!(queue.dequeue(), Some(8)); + assert_eq!(queue.len(), 1); + assert_eq!(queue.peek_front(), Some(&16)); + assert_eq!(queue.peek_back(), Some(&16)); + + queue.drain(); + assert!(queue.is_empty()); + assert_eq!(queue.len(), 0); + assert_eq!(queue.dequeue(), None); } }