Struct LinkedList

Source
pub struct LinkedList<T> { /* private fields */ }
Expand description

A list with links in both directions. See also: Cursor for bi-directional iteration and traversal.

§Time Complexity

For this analysis of time complexity, variables are defined as follows:

  • n: The number of items in the LinkedList.
  • i: The index of the item in question.
MethodComplexity
lenO(1)
front/backO(1)
push_front/backO(1)
pop_front/backO(1)
getO(min(i, n-i))
insertO(min(i, n-i))
removeO(min(i, n-i))
replaceO(min(i, n-i))
appendO(1)
containsO(n)

As a general note, modern computer architecture isn’t kind to linked lists, (or more importantly, favours contiguous collections) because all O(i) or O(n) operations will consist primarily of cache misses. For this reason, Vector should be preferred for most applications unless LinkedList and the accompanying Cursor type’s O(1) methods are being heavily utilized.

Implementations§

Source§

impl<T> LinkedList<T>

Source

pub const fn new() -> LinkedList<T>

Creates a new LinkedList with no elements.

Source

pub const fn len(&self) -> usize

Returns the length of the LinkedList.

Source

pub const fn is_empty(&self) -> bool

Returns true if the LinkedList contains no elements.

Source

pub const fn front(&self) -> Option<&T>

Returns a reference to the first element in the list, if it exists.

Source

pub const fn front_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the first element in the list, if it exists.

Source

pub const fn back(&self) -> Option<&T>

Returns a reference to the last element in the list, if it exists.

Source

pub const fn back_mut(&mut self) -> Option<&mut T>

Returns a mutable reference to the last element in the list, if it exists.

Source

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

Add the provided element to the front of the LinkedList.

Source

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

Add the provided element to the back of the LinkedList.

Source

pub fn pop_front(&mut self) -> Option<T>

Removes the first element from the list and returns it, if the list isn’t empty.

Source

pub fn pop_back(&mut self) -> Option<T>

Removes the last element from the list and returns it, if the list isn’t empty.

Source

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

Returns a reference to the element at the provided index, panicking on a failure.

The same functionality can be achieved using the Index operator.

§Panics

Panics if index is out of bounds of the LinkedList.

Source

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

Returns a reference to the element at the provided index, returning an Err on a failure rather than panicking.

The same functionality can be achieved using the Index operator.

Source

pub fn get_mut(&mut self, index: usize) -> &mut T

Returns a mutable reference to the element at the provided index, panicking on a failure.

The same functionality can be achieved using the IndexMut operator.

§Panics

Panics if index is out of bounds of the LinkedList.

Source

pub fn try_get_mut(&mut self, index: usize) -> Result<&mut T, IndexOutOfBounds>

Returns a mutable reference to the element at the provided index, returning an Err on a failure rather than panicking.

The same functionality can be achieved using the IndexMut operator.

Source

pub fn insert(&mut self, index: usize, value: T)

Source

pub fn try_insert( &mut self, index: usize, value: T, ) -> Result<(), IndexOutOfBounds>

Source

pub fn remove(&mut self, index: usize) -> T

Source

pub fn try_remove(&mut self, index: usize) -> Result<T, IndexOutOfBounds>

Source

pub fn replace(&mut self, index: usize, new_value: T) -> T

Source

pub fn try_replace( &mut self, index: usize, new_value: T, ) -> Result<T, IndexOutOfBounds>

Source

pub fn append(&mut self, other: LinkedList<T>)

Source

pub fn cursor_head(self) -> Cursor<T>

Source

pub fn cursor_tail(self) -> Cursor<T>

Source

pub fn cursor_front(self) -> Cursor<T>

Source

pub fn cursor_back(self) -> Cursor<T>

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Source

pub fn iter(&self) -> Iter<'_, T>

Source§

impl<T: Eq> LinkedList<T>

Source

pub fn index_of(&self, item: &T) -> Option<usize>

Source

pub fn contains(&self, item: &T) -> bool

Trait Implementations§

Source§

impl<T: Debug> Debug for LinkedList<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for LinkedList<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Debug> Display for LinkedList<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for LinkedList<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> FromIterator<T> for LinkedList<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for LinkedList<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<usize> for LinkedList<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for LinkedList<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a LinkedList<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut LinkedList<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for LinkedList<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for LinkedList<T>

Source§

fn eq(&self, other: &LinkedList<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for LinkedList<T>

Source§

impl<T> StructuralPartialEq for LinkedList<T>

Auto Trait Implementations§

§

impl<T> Freeze for LinkedList<T>

§

impl<T> RefUnwindSafe for LinkedList<T>
where T: RefUnwindSafe,

§

impl<T> !Send for LinkedList<T>

§

impl<T> !Sync for LinkedList<T>

§

impl<T> Unpin for LinkedList<T>
where T: Unpin,

§

impl<T> UnwindSafe for LinkedList<T>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.