pub struct SinglyLinkedList<T> { /* private fields */ }Expand description
A singly-linked list implementation with efficient insertion at the front and back.
The SinglyLinkedList stores elements in a linear sequence where each element
points to the next one. It provides O(1) push and pop_front operations.
§Type Parameters
T: The type of elements stored in the list.
§Examples
use plain_ds::SinglyLinkedList;
let mut list = SinglyLinkedList::new();
list.push(1);
list.push(2);
list.push(3);
assert_eq!(list.pop(), Some(3));
assert_eq!(list.len(), 2);Implementations§
Source§impl<T> SinglyLinkedList<T>
impl<T> SinglyLinkedList<T>
Sourcepub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone,
pub fn from_slice(slice: &[T]) -> Selfwhere
T: Clone,
Creates list from slice.
Efficiency: O(n)
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 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 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 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
Trait Implementations§
Source§impl<'a, T: 'a> List<'a, T> for SinglyLinkedList<T>
impl<'a, T: 'a> List<'a, T> for SinglyLinkedList<T>
Source§fn head(&self) -> Option<&T>
fn head(&self) -> Option<&T>
Returns the payload value of the first node in the list.
Efficiency: O(1)
Source§fn last(&self) -> Option<&T>
fn last(&self) -> Option<&T>
Returns the payload value of the last node in the list.
Efficiency: O(1)
Source§fn iter(&self) -> impl Iterator<Item = &'a T>
fn iter(&self) -> impl Iterator<Item = &'a T>
Returns an iterator over the immutable items of the list.
Source§fn iter_mut(&mut self) -> impl Iterator<Item = &'a mut T>
fn iter_mut(&mut self) -> impl Iterator<Item = &'a mut T>
Returns an iterator over the mutable items of the list.
Source§fn pop_back(&mut self) -> Option<T>
fn pop_back(&mut self) -> Option<T>
Removes a node from the end of the list and returns its payload value.
Efficiency: O(n)
Source§fn pop_front(&mut self) -> Option<T>
fn pop_front(&mut self) -> Option<T>
Removes a node from the front of the list and returns its payload value.
Efficiency: O(1)
Source§fn remove(&mut self, index: usize) -> Result<T>
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)