Skip to main content

SinglyLinkedList

Struct SinglyLinkedList 

Source
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>

Source

pub fn new() -> Self

Creates empty singly-linked list.

Source

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

Creates list from slice.

Efficiency: O(n)

Source

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

Collect list values into a vector.

Efficiency: O(n)

Trait Implementations§

Source§

impl<'a, T: 'a> List<'a, T> for SinglyLinkedList<T>

Source§

fn len(&self) -> usize

Returns list size.

Efficiency: O(1)

Source§

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>

Returns the payload value of the last node in the list.

Efficiency: O(1)

Source§

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>

Returns an iterator over the mutable items of the list.

Source§

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

Returns an iterator that consumes the list.

Source§

fn push(&mut self, payload: T)

Adds a new node to the end of the list.

Efficiency: O(1)

Source§

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>

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>

Removes a node from the specified location in the list. Error returns, if the index out of bounds.

Efficiency: O(n)

Source§

fn is_empty(&self) -> bool

Checks if the list is empty. Read more
Source§

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

Returns a list item by index, or error if index out of bounds. Read more
Source§

fn get_mut(&mut self, index: usize) -> Result<&'a mut T>

Returns a mutable list item by index, or error if index out of bounds. Read more
Source§

fn clear(&mut self)

Removes all items from the list. Read more
Source§

fn find(&self, value: &T) -> Option<usize>
where T: PartialEq<T>,

Finds the first node whose payload is equal to the given value and returns its index. Returns None if there is no such node. 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.