1+ #![ allow( dead_code) ]
2+
13//! A queue implementation using a singly linked list
24//! The queue follows FIFO (First-In First-Out) principle
35//! The [enqueue] method's time complexity is O(1)
@@ -167,7 +169,7 @@ impl<T> Queue<T> {
167169 // SAFETY: this will prevent the length from going below usize::MIN, because
168170 // a user may try to dequeue an empty queue, which would lead to negatives
169171 // which are not supported by the usize
170- if result. is_some ( ) {
172+ if result. is_some ( ) && ! self . is_empty ( ) {
171173 self . length -= 1 ;
172174 }
173175
@@ -176,19 +178,18 @@ impl<T> Queue<T> {
176178
177179 /// Reference to the first element in the queue
178180 pub fn peek_front ( & self ) -> Option < & T > {
179- self . head . as_ref ( ) . map ( |head | & head . element )
181+ self . tail . as_ref ( ) . map ( |tail | & tail . element )
180182 }
181183
182184 // Reference to value at the end of the queue
183185 pub fn peek_back ( & self ) -> Option < & T > {
184- self . tail . as_ref ( ) . map ( |tail | & tail . element )
186+ self . head . as_ref ( ) . map ( |head | & head . element )
185187 }
186188
187189 // Get element by index from the queue
188190 pub fn get ( & self , index : usize ) -> Option < & T > {
189191 let mut counter = 0 ;
190- let mut current = & self . head ;
191-
192+
192193 // If index == 0, it returns the first element from the queue, using the peek_front
193194 if index == 0 {
194195 return self . peek_front ( ) ;
@@ -197,22 +198,24 @@ impl<T> Queue<T> {
197198 } else if index == ( self . len ( ) - 1 ) {
198199 return self . peek_back ( ) ;
199200
200- // Else, returns none, if index is out of bounds
201+ // Else, returns none, if index is out of bounds
201202 } else if index > ( self . len ( ) - 1 ) {
202203 return None ;
203204 }
204205
206+ let mut _current = & self . head ;
207+
205208 let mut get_node: Option < & T > = None ;
206-
209+
207210 // If the node was not got we also index through the tail
208211 if get_node. is_none ( ) {
209212 // Setting current to now be the tail
210- current = & self . tail ;
213+ _current = & self . tail ;
211214 // And also reset counter to 0, because the head will have atmost 1 element
212215 counter += 1 ;
213216
214217 // We traverse to the node to get from the queue
215- while let Some ( node) = & current {
218+ while let Some ( node) = & _current {
216219 // If the selected index matches the pointer, then set get_node
217220 // to node at that index
218221 if counter == index {
@@ -224,7 +227,7 @@ impl<T> Queue<T> {
224227 // Increment counter
225228 counter += 1 ;
226229
227- current = & node. next ;
230+ _current = & node. next ;
228231 }
229232 }
230233
@@ -333,6 +336,9 @@ mod tests {
333336 queue. enqueue ( 2 ) ;
334337 queue. enqueue ( 3 ) ;
335338
339+ println ! ( "{:?}" , queue) ;
340+ println ! ( "{:?}" , queue. len( ) ) ;
341+
336342 assert_eq ! ( queue. len( ) , 3 ) ;
337343 }
338344
@@ -438,9 +444,7 @@ mod tests {
438444
439445 queue. delete ( 1 ) ;
440446
441- assert ! ( false ) ;
442- assert_eq ! ( queue. len( ) , 1 ) ;
443-
447+ assert_eq ! ( queue. len( ) , 2 ) ;
444448 // Whether to see whether an option of variant Some is returned
445449 assert ! ( queue. delete( 1 ) . is_some( ) ) ;
446450 }
0 commit comments