pub struct SingleLinkedList<T> { /* private fields */ }Implementations§
Source§impl<T> SingleLinkedList<T>
impl<T> SingleLinkedList<T>
Sourcepub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone,
pub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone,
Creates list from slice.
Sourcepub fn to_vec(&self) -> Vec<T>where
T: Clone,
pub fn to_vec(&self) -> Vec<T>where
T: Clone,
Collect list values into a vector. Efficiency: O(n)
Sourcepub fn head(&self) -> Option<&T>
pub fn head(&self) -> Option<&T>
Returns the payload value of the first node in the list. Efficiency: O(1)
Sourcepub fn last(&self) -> Option<&T>
pub fn last(&self) -> Option<&T>
Returns the payload value of the last node in the list. Efficiency: O(1)
Sourcepub fn get(&self, index: usize) -> Result<&T>
pub fn get(&self, index: usize) -> Result<&T>
Returns a list item by index, or error if index out of bounds. Efficiency: O(n)
Sourcepub fn get_mut(&mut self, index: usize) -> Result<&mut T>
pub fn get_mut(&mut self, index: usize) -> Result<&mut T>
Returns a mutable list item by index, or error if index out of bounds. Efficiency: O(n)
Sourcepub fn iter(&self) -> impl Iterator<Item = &T>
pub fn iter(&self) -> impl Iterator<Item = &T>
Returns an iterator over the immutable items of the list.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Returns an iterator over the mutable items of the list.
Sourcepub fn push_back(&mut self, payload: T)
pub fn push_back(&mut self, payload: T)
Adds a new node to the end of the list. Efficiency: O(1)
Sourcepub fn push_front(&mut self, payload: T)
pub fn push_front(&mut self, payload: T)
Adds a new node to the front of the list. Efficiency: O(1)
Sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes a node from the end of the list and returns its payload value. Efficiency: O(n)
Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes a node from the front of the list and returns its payload value. Efficiency: O(1)
Sourcepub fn insert(&mut self, index: usize, payload: T) -> Result<()>
pub fn insert(&mut self, index: usize, payload: T) -> Result<()>
Insert a new node at the specified location in the list. Error returns, if the index out of bounds. Efficiency: O(n)
Sourcepub fn remove(&mut self, index: usize) -> Result<T>
pub fn remove(&mut self, index: usize) -> Result<T>
Removes a node from the specified location in the list. Error returns, if the index out of bounds. Efficiency: O(n)
Sourcepub fn find(&self, value: &T) -> Option<usize>where
T: PartialEq,
pub fn find(&self, value: &T) -> Option<usize>where
T: PartialEq,
Finds the first node whose payload is equal to the given one and returns its index.
Returns None if there is no such node.
Efficiency: O(n)
Sourcepub fn find_if(&self, predicate: impl Fn(&T) -> bool) -> Option<usize>where
T: PartialEq,
pub fn find_if(&self, predicate: impl Fn(&T) -> bool) -> Option<usize>where
T: PartialEq,
Finds the first node whose payload satisfies the predicate and returns its index.
Returns None if there is no such node.
Efficiency: O(n)
Sourcepub fn sort(&mut self)where
T: PartialOrd + Default,
pub fn sort(&mut self)where
T: PartialOrd + Default,
Sorts the list in ascending order using merge sort algorithm. Efficiency: O(n log n) Space complexity: O(log n) due to recursion stack