pub struct LinkedList<T> { /* private fields */ }collections and linked only.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.
| Method | Complexity |
|---|---|
len | O(1) |
front/back | O(1) |
push_front/back | O(1) |
pop_front/back | O(1) |
get | O(min(i, n-i)) |
insert | O(min(i, n-i)) |
remove | O(min(i, n-i)) |
replace | O(min(i, n-i)) |
append | O(1) |
contains | O(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>
impl<T> LinkedList<T>
Sourcepub const fn new() -> LinkedList<T>
pub const fn new() -> LinkedList<T>
Creates a new LinkedList with no elements.
Sourcepub const fn front(&self) -> Option<&T>
pub const fn front(&self) -> Option<&T>
Returns a reference to the first element in the list, if it exists.
Sourcepub const fn front_mut(&mut self) -> Option<&mut T>
pub const fn front_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the first element in the list, if it exists.
Sourcepub const fn back(&self) -> Option<&T>
pub const fn back(&self) -> Option<&T>
Returns a reference to the last element in the list, if it exists.
Sourcepub const fn back_mut(&mut self) -> Option<&mut T>
pub const fn back_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the last element in the list, if it exists.
Sourcepub fn push_front(&mut self, value: T)
pub fn push_front(&mut self, value: T)
Add the provided element to the front of the LinkedList.
Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes the first element from the list and returns it, if the list isn’t empty.
Sourcepub fn pop_back(&mut self) -> Option<T>
pub fn pop_back(&mut self) -> Option<T>
Removes the last element from the list and returns it, if the list isn’t empty.
Sourcepub fn try_get_mut(&mut self, index: usize) -> Result<&mut T, IndexOutOfBounds>
pub fn try_get_mut(&mut self, index: usize) -> Result<&mut T, IndexOutOfBounds>
pub fn insert(&mut self, index: usize, value: T)
pub fn try_insert( &mut self, index: usize, value: T, ) -> Result<(), IndexOutOfBounds>
pub fn remove(&mut self, index: usize) -> T
pub fn try_remove(&mut self, index: usize) -> Result<T, IndexOutOfBounds>
pub fn replace(&mut self, index: usize, new_value: T) -> T
pub fn try_replace( &mut self, index: usize, new_value: T, ) -> Result<T, IndexOutOfBounds>
pub fn append(&mut self, other: LinkedList<T>)
pub fn cursor_head(self) -> Cursor<T>
pub fn cursor_tail(self) -> Cursor<T>
pub fn cursor_front(self) -> Cursor<T>
pub fn cursor_back(self) -> Cursor<T>
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Trait Implementations§
Source§impl<T: Debug> Debug for LinkedList<T>
impl<T: Debug> Debug for LinkedList<T>
Source§impl<T> Default for LinkedList<T>
impl<T> Default for LinkedList<T>
Source§impl<T: Debug> Display for LinkedList<T>
impl<T: Debug> Display for LinkedList<T>
Source§impl<T> Drop for LinkedList<T>
impl<T> Drop for LinkedList<T>
Source§impl<T> FromIterator<T> for LinkedList<T>
impl<T> FromIterator<T> for LinkedList<T>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<T: Hash> Hash for LinkedList<T>
impl<T: Hash> Hash for LinkedList<T>
Source§impl<T> Index<usize> for LinkedList<T>
impl<T> Index<usize> for LinkedList<T>
Source§impl<T> IndexMut<usize> for LinkedList<T>
impl<T> IndexMut<usize> for LinkedList<T>
Source§impl<'a, T> IntoIterator for &'a LinkedList<T>
impl<'a, T> IntoIterator for &'a LinkedList<T>
Source§impl<'a, T> IntoIterator for &'a mut LinkedList<T>
impl<'a, T> IntoIterator for &'a mut LinkedList<T>
Source§impl<T> IntoIterator for LinkedList<T>
impl<T> IntoIterator for LinkedList<T>
Source§impl<T: PartialEq> PartialEq for LinkedList<T>
impl<T: PartialEq> PartialEq for LinkedList<T>
Source§fn eq(&self, other: &LinkedList<T>) -> bool
fn eq(&self, other: &LinkedList<T>) -> bool
self and other values to be equal, and is used by ==.