pop_front implemented.
This commit is contained in:
		
							parent
							
								
									5899bde66e
								
							
						
					
					
						commit
						bb4b045aa8
					
				| @ -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(), | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Tom Alexander
						Tom Alexander