diff --git a/src/data_structures.rs b/src/data_structures.rs index 0911971..e37edaf 100644 --- a/src/data_structures.rs +++ b/src/data_structures.rs @@ -232,3 +232,42 @@ impl Stack { self.stack.last() } } + +use std::mem; + +struct Node { + value: T, + next: Option>>, +} + +type Link = Option>>; + +pub struct List { + head: Link, +} + +impl List { + pub fn new() -> Self { + List { head: None } + } + + pub fn push(&mut self, item: T) { + let next_node = Box::new(Node { + value: item, + next: self.head.take(), + }); + + self.head = Some(next_node) + } + + pub fn pop(&mut self) -> Option { + self.head.take().map(|node| { + self.head = node.next; + node.value + }) + } + + pub fn peek(&self) -> Option<&T> { + self.head.as_ref().map(|node| &node.value) + } +}