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§
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)