1- #![ allow( dead_code) ]
2-
31//! A queue implementation using a singly linked list
42//! The queue follows FIFO (First-In First-Out) principle
53//! The [enqueue] method's time complexity is O(1)
@@ -50,7 +48,7 @@ impl<T> Default for LinkedListQueue<T> {
5048}
5149
5250// Implement iterator for the queue
53- pub struct LinkedListQueueIterator < ' a , T > {
51+ pub struct LinkedListQueueIter < ' a , T > {
5452 current : & ' a Option < Box < Node < T > > > ,
5553 _marker : PhantomData < & ' a T > ,
5654}
@@ -69,22 +67,22 @@ where
6967 T : Debug ,
7068{
7169 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
72- use std:: fmt:: Write as _;
70+ use std:: fmt:: Write as _;
7371
7472 let mut output = String :: from ( "LinkedListQueue ( elements: [" ) ;
75-
73+
7674 for elem in self . iter ( ) {
7775 let _ = write ! ( output, " {:?} " , elem) ;
7876 }
79-
77+
8078 let _ = write ! ( output, "], length: {} )" , self . len( ) ) ;
8179
82- write ! ( f, "{}" , output)
80+ write ! ( f, "{}" , output)
8381 }
8482}
8583
8684// LinkedListQueueIterator implementation
87- impl < ' a , T > Iterator for LinkedListQueueIterator < ' a , T > {
85+ impl < ' a , T > Iterator for LinkedListQueueIter < ' a , T > {
8886 type Item = & ' a T ;
8987
9088 fn next ( & mut self ) -> Option < Self :: Item > {
@@ -110,8 +108,8 @@ impl<T> LinkedListQueue<T> {
110108 }
111109
112110 // Iter method, will enably us to iterate through our queue
113- pub fn iter ( & self ) -> LinkedListQueueIterator < ' _ , T > {
114- LinkedListQueueIterator {
111+ pub fn iter ( & self ) -> LinkedListQueueIter < ' _ , T > {
112+ LinkedListQueueIter {
115113 current : & self . tail ,
116114 _marker : PhantomData ,
117115 }
@@ -312,6 +310,11 @@ impl<T> LinkedListQueue<T> {
312310 }
313311 }
314312
313+ /// Empty the queue
314+ pub fn drain ( & mut self ) {
315+ while self . dequeue ( ) . is_some ( ) { }
316+ }
317+
315318 /// Gets the length of the queue
316319 pub fn len ( & self ) -> usize {
317320 self . length
@@ -326,7 +329,7 @@ impl<T> LinkedListQueue<T> {
326329/// The queue implementation tests
327330#[ cfg( test) ]
328331mod tests {
329- use super :: * ;
332+ use super :: LinkedListQueue ;
330333
331334 #[ test]
332335 fn test_enqueue ( ) {
0 commit comments