From bf4be4467608cd70d5bfd3f5f9cfcccd81501011 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 8 Mar 2023 15:46:53 +0100 Subject: [PATCH] Add list.remove(T) --- src/utility/list.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/utility/list.rs b/src/utility/list.rs index 88962c4..024606c 100644 --- a/src/utility/list.rs +++ b/src/utility/list.rs @@ -70,6 +70,33 @@ impl List { false } + /// Remove the item from the list + /// + /// Return true if the item has been found, otherwise return false + /// + /// Worst-case complexity is O(n) + pub fn remove(&mut self, item: T)-> bool { + let mut found = false; + let mut tmp_list: List = List::new(); + while !self.is_empty() { + let current = self.pop().unwrap(); + if current != item { + tmp_list.push(current); + } else { + found = true; + break; + } + } + while !tmp_list.is_empty() { + self.push(tmp_list.pop().unwrap()); + } + found + } + + pub fn is_empty(&self) -> bool { + self.head.is_none() + } + pub fn into_iter(self) -> IntoIter { IntoIter(self) }