Add list.remove(T)
This commit is contained in:
parent
4c79f86b89
commit
62b60186e9
@ -69,6 +69,33 @@ impl<T: PartialEq> List<T> {
|
||||
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<T> = 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<T> {
|
||||
IntoIter(self)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user