pop_front implemented.

This commit is contained in:
Tom Alexander 2022-12-10 21:27:33 -05:00
parent 5899bde66e
commit bb4b045aa8
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 19 additions and 1 deletions

View File

@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::rc::Rc;
#[derive(Debug)]
@ -19,7 +20,8 @@ pub struct Node<T> {
parent: Option<Rc<Node<T>>>,
}
impl<T> List<T> {
// TODO: This Debug is only needed because of the try_unwrap+expect
impl<T: Debug> List<T> {
pub fn new() -> Self {
List { head: None }
}
@ -33,6 +35,22 @@ impl<T> List<T> {
}
}
pub fn pop_front(self) -> (Option<T>, List<T>) {
match self.head {
None => (None, List::new()),
Some(popped_node) => {
let extracted_node =
Rc::try_unwrap(popped_node).expect("TODO I should handle this better");
(
Some(extracted_node.data),
List {
head: extracted_node.parent,
},
)
}
}
}
pub fn without_front(&self) -> List<T> {
List {
head: self.head.as_ref().map(|node| node.parent.clone()).flatten(),