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