Skip to main content

SingleLinkedList

Struct SingleLinkedList 

Source
pub struct SingleLinkedList<T> { /* private fields */ }

Implementations§

Source§

impl<T> SingleLinkedList<T>

Source

pub fn new() -> Self

Creates empty list.

Source

pub fn from_slice(slice: &[T]) -> Self
where T: Clone,

Creates list from slice.

Source

pub fn to_vec(&self) -> Vec<T>
where T: Clone,

Collect list values into a vector. Efficiency: O(n)

Source

pub fn len(&self) -> usize

Returns list size. Efficiency: O(1)

Source

pub fn is_empty(&self) -> bool

Checks if the list is empty. Efficiency: O(1)

Source

pub fn head(&self) -> Option<&T>

Returns the payload value of the first node in the list. Efficiency: O(1)

Source

pub fn last(&self) -> Option<&T>

Returns the payload value of the last node in the list. Efficiency: O(1)

Source

pub fn get(&self, index: usize) -> Result<&T>

Returns a list item by index, or error if index out of bounds. Efficiency: O(n)

Source

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)

Source

pub fn iter(&self) -> impl Iterator<Item = &T>

Returns an iterator over the immutable items of the list.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>

Returns an iterator over the mutable items of the list.

Source

pub fn into_iter(self) -> impl Iterator<Item = T>

Returns an iterator that consumes the list.

Source

pub fn push_back(&mut self, payload: T)

Adds a new node to the end of the list. Efficiency: O(1)

Source

pub fn push_front(&mut self, payload: T)

Adds a new node to the front of the list. Efficiency: O(1)

Source

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)

Source

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)

Source

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)

Source

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)

Source

pub fn clear(&mut self)

Removes all items from the list. Efficiency: O(n)

Source

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)

Source

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)

Source

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<T> Drop for SingleLinkedList<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.