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
+
1
6
use std:: collections:: LinkedList ;
2
7
3
8
#[ derive( Debug ) ]
@@ -6,33 +11,50 @@ pub struct Queue<T> {
6
11
}
7
12
8
13
impl < T > Queue < T > {
14
+ // Creates a new empty Queue
9
15
pub fn new ( ) -> Queue < T > {
10
16
Queue {
11
17
elements : LinkedList :: new ( ) ,
12
18
}
13
19
}
14
20
21
+ // Adds an element to the back of the queue
15
22
pub fn enqueue ( & mut self , value : T ) {
16
23
self . elements . push_back ( value)
17
24
}
18
25
26
+ // Removes and returns the front element from the queue, or None if empty
19
27
pub fn dequeue ( & mut self ) -> Option < T > {
20
28
self . elements . pop_front ( )
21
29
}
22
30
31
+ // Returns a reference to the front element of the queue, or None if empty
23
32
pub fn peek_front ( & self ) -> Option < & T > {
24
33
self . elements . front ( )
25
34
}
26
35
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
27
42
pub fn len ( & self ) -> usize {
28
43
self . elements . len ( )
29
44
}
30
45
46
+ // Checks if the queue is empty
31
47
pub fn is_empty ( & self ) -> bool {
32
48
self . elements . is_empty ( )
33
49
}
50
+
51
+ // Clears all elements from the queue
52
+ pub fn drain ( & mut self ) {
53
+ self . elements . clear ( ) ;
54
+ }
34
55
}
35
56
57
+ // Implementing the Default trait for Queue
36
58
impl < T > Default for Queue < T > {
37
59
fn default ( ) -> Queue < T > {
38
60
Queue :: new ( )
@@ -44,55 +66,26 @@ mod tests {
44
66
use super :: Queue ;
45
67
46
68
#[ 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 ( ) ;
65
71
66
- #[ test]
67
- fn test_peek_front ( ) {
68
- let mut queue: Queue < u8 > = Queue :: new ( ) ;
72
+ assert ! ( queue. is_empty( ) ) ;
69
73
queue. enqueue ( 8 ) ;
70
74
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 ) ;
78
77
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 ) ) ;
90
80
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 ) ;
97
90
}
98
91
}
0 commit comments