//! Singly linked list. //! //! Consult . use std::fmt::Debug; /// Node of the list. #[derive(Debug)] pub struct Node { /// Value of current node. pub value: T, /// Pointer to the next node. If it is `None`, there is no next node. pub next: Option>>, } impl Node { /// Creates a new node. pub fn new(value: T) -> Self { Self { value, next: None } } } /// A singly-linked list. #[derive(Debug)] pub struct SinglyLinkedList { /// Head node of the list. If it is `None`, the list is empty. head: Option>, } impl Default for SinglyLinkedList { fn default() -> Self { Self::new() } } impl SinglyLinkedList { /// Creates a new list. pub fn new() -> Self { Self { head: None } } /// Adds the given node to the front of the list. pub fn push_front(&mut self, value: T) { todo!() } /// Adds the given node to the back of the list. pub fn push_back(&mut self, value: T) { todo!() } /// Removes and returns the node at the front of the list. pub fn pop_front(&mut self) -> Option { todo!() } /// Removes and returns the node at the back of the list. pub fn pop_back(&mut self) -> Option { todo!() } /// Create a new list from the given vector `vec`. pub fn from_vec(vec: Vec) -> Self { todo!() } /// Convert the current list into a vector. pub fn into_vec(self) -> Vec { todo!() } /// Return the length (i.e., number of nodes) of the list. pub fn length(&self) -> usize { todo!() } /// Apply function `f` on every element of the list. /// /// # Examples /// /// `self`: `[1, 2]`, `f`: `|x| x + 1` ==> `[2, 3]` pub fn map T>(self, f: F) -> Self { todo!() } /// Apply given function `f` for each adjacent pair of elements in the list. /// If `self.length() < 2`, do nothing. /// /// # Examples /// /// `self`: `[1, 2, 3, 4]`, `f`: `|x, y| x + y` /// // each adjacent pair of elements: `(1, 2)`, `(2, 3)`, `(3, 4)` /// // apply `f` to each pair: `f(1, 2) == 3`, `f(2, 3) == 5`, `f(3, 4) == 7` /// ==> `[3, 5, 7]` pub fn pair_map T>(self, f: F) -> Self where T: Clone, { todo!() } } // A list of lists. impl SinglyLinkedList> { /// Flatten the list of lists into a single list. /// /// # Examples /// `self`: `[[1, 2, 3], [4, 5, 6], [7, 8]]` /// ==> `[1, 2, 3, 4, 5, 6, 7, 8]` pub fn flatten(self) -> SinglyLinkedList { todo!() } }