Struct rust_algorithm_club::collections::SinglyLinkedList
source · [−]pub struct SinglyLinkedList<T> { /* private fields */ }
Expand description
A singly-linked list with owned nodes.
This implementation is a simplified version of std::forward_list
in C++.
References:
Implementations
sourceimpl<T> SinglyLinkedList<T>
impl<T> SinglyLinkedList<T>
sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new, empty SinglyLinkedList<T>
.
The list will not allocate until elements are pushed onto it.
sourcepub fn push_front(&mut self, elem: T)
pub fn push_front(&mut self, elem: T)
Prepends the given element value to the beginning of the container.
Parameters
elem
- The element to prepend.
Complexity
Constant.
sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes and returns the first element of the container.
If there are no elements in the container, return None
.
Complexity
Constant.
sourcepub fn insert_after(&mut self, pos: usize, elem: T) -> Result<(), usize>
pub fn insert_after(&mut self, pos: usize, elem: T) -> Result<(), usize>
Inserts an element after the specified position in the container.
If the position is out of bound, returns an Result:Err
with the
size of the list.
Parameters
pos
- The index after which the element will be inserted.elem
- The element to be inserted.
Complexity
Search time O(n) + O(1).
sourcepub fn remove(&mut self, pos: usize) -> Option<T>
pub fn remove(&mut self, pos: usize) -> Option<T>
Removes and returns an element at specified position from the container.
Parameters
pos
- The index at which the element will be moved.
Complexity
Search time O(n) + constant.
sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of the elements in the container.
Complexity
Linear in the size of the container.
Trait Implementations
sourceimpl<T: Debug> Debug for SinglyLinkedList<T>
impl<T: Debug> Debug for SinglyLinkedList<T>
sourceimpl<T> Drop for SinglyLinkedList<T>
impl<T> Drop for SinglyLinkedList<T>
sourceimpl<T> IntoIterator for SinglyLinkedList<T>
impl<T> IntoIterator for SinglyLinkedList<T>
sourcefn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates a consuming iterator, that is, one that moves each value out of the list (from start to end). The list cannot be used after calling this.
type Item = T
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
Auto Trait Implementations
impl<T> RefUnwindSafe for SinglyLinkedList<T> where
T: RefUnwindSafe,
impl<T> Send for SinglyLinkedList<T> where
T: Send,
impl<T> Sync for SinglyLinkedList<T> where
T: Sync,
impl<T> Unpin for SinglyLinkedList<T>
impl<T> UnwindSafe for SinglyLinkedList<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more