pub struct LinkedList<T> { /* private fields */ }
Expand description
A doubly-linked list in which the liftime of iterator is independent from self.
The LinkedList
allows pushing and popping elements at either end
in constant time.
Implementations§
Source§impl<T> LinkedList<T>
impl<T> LinkedList<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty LinkedList
.
§Examples
use atlist_rs::LinkedList;
let list: LinkedList<u32> = LinkedList::new();
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the LinkedList
.
This operation should compute in O(1) time.
§Examples
use atlist_rs::LinkedList;
let mut dl = LinkedList::new();
let _ = dl.push_front(2);
assert_eq!(dl.len(), 1);
let _ = dl.push_front(1);
assert_eq!(dl.len(), 2);
let _ = dl.push_back(3);
assert_eq!(dl.len(), 3);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the LinkedList
is empty.
This operation should compute in O(1) time.
§Examples
use atlist_rs::LinkedList;
let mut dl = LinkedList::new();
assert!(dl.is_empty());
let _ = dl.push_front("foo");
assert!(!dl.is_empty());
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all elements from the LinkedList
.
This operation should compute in O(n) time.
§Examples
use atlist_rs::{LinkedList, LinkedListError};
let mut dl = LinkedList::new();
dl.push_front(2);
dl.push_front(1);
assert_eq!(dl.len(), 2);
assert_eq!(*dl.front().unwrap(), 1);
dl.clear();
assert_eq!(dl.len(), 0);
assert_eq!(dl.front(), Err(LinkedListError::Empty));
Sourcepub fn iter(&self) -> Iter<T> ⓘ
pub fn iter(&self) -> Iter<T> ⓘ
Provides a forward iterator.
§Examples
use atlist_rs::LinkedList;
let mut list: LinkedList<u32> = LinkedList::new();
let _ = list.push_back(0);
let _ = list.push_back(1);
let _ = list.push_back(2);
let mut iter = list.iter();
assert_eq!(*iter.next().unwrap(), 0);
assert_eq!(*iter.next().unwrap(), 1);
assert_eq!(*iter.next().unwrap(), 2);
assert_eq!(iter.next(), None);
Sourcepub fn iter_mut(&mut self) -> IterMut<T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<T> ⓘ
Provides a forward iterator.
§Examples
use atlist_rs::LinkedList;
use std::cell::RefCell;
let mut list: LinkedList<RefCell<u32>> = LinkedList::new();
let _ = list.push_back(RefCell::new(0));
let _ = list.push_back(RefCell::new(1));
let _ = list.push_back(RefCell::new(2));
for element in list.iter_mut() {
*(*element).borrow_mut() += 10;
}
let mut iter = list.iter_mut();
assert_eq!(*(*iter.next().unwrap()).borrow(), 10);
assert_eq!(*(*iter.next().unwrap()).borrow(), 11);
assert_eq!(*(*iter.next().unwrap()).borrow(), 12);
assert_eq!(iter.next(), None);
Sourcepub fn iter_front(&self) -> Iter<T> ⓘ
pub fn iter_front(&self) -> Iter<T> ⓘ
Provides a iterator at the front element.
The iterator is pointing to the “ghost” non-element if the list is empty.
Sourcepub fn iter_back(&self) -> Iter<T> ⓘ
pub fn iter_back(&self) -> Iter<T> ⓘ
Provides a iterator at the back element.
The iterator is pointing to the “ghost” non-element if the list is empty.
Sourcepub fn iter_mut_front(&mut self) -> IterMut<T> ⓘ
pub fn iter_mut_front(&mut self) -> IterMut<T> ⓘ
Provides a iterator with editing operations at the front element.
The iterator is pointing to the “ghost” non-element if the list is empty.
Sourcepub fn iter_mut_back(&mut self) -> IterMut<T> ⓘ
pub fn iter_mut_back(&mut self) -> IterMut<T> ⓘ
Provides a iterator with editing operations at the back element.
The iterator is pointing to the “ghost” non-element if the list is empty.
Sourcepub fn push_front(&mut self, elt: T) -> LinkedListResult<()>
pub fn push_front(&mut self, elt: T) -> LinkedListResult<()>
Adds an element first in the list.
This operation should compute in O(1) time.
Sourcepub fn pop_front(&mut self) -> LinkedListResult<LinkedListItem<T>>
pub fn pop_front(&mut self) -> LinkedListResult<LinkedListItem<T>>
Removes the first element and returns it, or None
if the list is
empty.
This operation should compute in O(1) time.
Sourcepub fn push_back(&mut self, elt: T) -> LinkedListResult<()>
pub fn push_back(&mut self, elt: T) -> LinkedListResult<()>
Appends an element to the back of a list.
This operation should compute in O(1) time.
Sourcepub fn pop_back(&mut self) -> LinkedListResult<LinkedListItem<T>>
pub fn pop_back(&mut self) -> LinkedListResult<LinkedListItem<T>>
Removes the last element from a list and returns it, or None
if
it is empty.
This operation should compute in O(1) time.
Sourcepub fn front(&self) -> LinkedListResult<LinkedListItem<T>>
pub fn front(&self) -> LinkedListResult<LinkedListItem<T>>
Provides a reference to the front element, or None
if the list is
empty.
§Examples
use atlist_rs::{LinkedList, LinkedListError};
let mut dl = LinkedList::new();
assert_eq!(dl.front(), Err(LinkedListError::Empty));
let _ = dl.push_front(1);
assert_eq!(*dl.front().unwrap(), 1);
Sourcepub fn back(&self) -> LinkedListResult<LinkedListItem<T>>
pub fn back(&self) -> LinkedListResult<LinkedListItem<T>>
Provides a reference to the back element, or None
if the list is
empty.
§Examples
use atlist_rs::{LinkedList, LinkedListError};
let mut dl = LinkedList::new();
assert_eq!(dl.back(), Err(LinkedListError::Empty));
let _ = dl.push_back(1);
assert_eq!(*dl.back().unwrap(), 1);
Sourcepub fn contains_iter(&self, x: &Iter<T>) -> LinkedListResult<()>
pub fn contains_iter(&self, x: &Iter<T>) -> LinkedListResult<()>
Returns true
if the LinkedList
contains an element equal to the
given value.
§Examples
use atlist_rs::{LinkedList, LinkedListError};
let mut list: LinkedList<u32> = LinkedList::new();
let mut another_list: LinkedList<u32> = LinkedList::new();
let _ = list.push_back(0);
let _ = list.push_back(1);
let _ = another_list.push_back(2);
assert_eq!(list.contains_iter(&list.iter()), Ok(()));
assert_eq!(list.contains_iter(&another_list.iter()), Err(LinkedListError::IteratorNotInList));
Sourcepub fn contains_iter_mut(&self, x: &IterMut<T>) -> LinkedListResult<()>
pub fn contains_iter_mut(&self, x: &IterMut<T>) -> LinkedListResult<()>
Returns true
if the LinkedList
contains an element equal to the
given value.
§Examples
use atlist_rs::{LinkedList, LinkedListError};
let mut list: LinkedList<u32> = LinkedList::new();
let mut another_list: LinkedList<u32> = LinkedList::new();
let _ = list.push_back(0);
let _ = list.push_back(1);
let _ = another_list.push_back(2);
let iter = list.iter_mut();
let another_iter = another_list.iter_mut();
assert_eq!(list.contains_iter_mut(&iter), Ok(()));
assert_eq!(list.contains_iter_mut(&another_iter), Err(LinkedListError::IteratorNotInList));
Sourcepub fn insert_before(
&mut self,
iter: &IterMut<T>,
elt: T,
) -> LinkedListResult<IterMut<T>>
pub fn insert_before( &mut self, iter: &IterMut<T>, elt: T, ) -> LinkedListResult<IterMut<T>>
Inserts a new element into the LinkedList
before the current one.
If the cursor is pointing at the “ghost” non-element then the new element is
inserted at the end of the LinkedList
.
Returns the iterator of inserted value if success, or error if failed
Sourcepub fn insert_after(
&mut self,
iter: &IterMut<T>,
elt: T,
) -> LinkedListResult<IterMut<T>>
pub fn insert_after( &mut self, iter: &IterMut<T>, elt: T, ) -> LinkedListResult<IterMut<T>>
Inserts a new element into the LinkedList
after the current one.
If the cursor is pointing at the “ghost” non-element then the new element is
inserted at the front of the LinkedList
.
Returns the iterator of inserted value if success, or error if failed
Sourcepub fn remove_iter_mut(
&mut self,
iter: &mut IterMut<T>,
) -> LinkedListResult<LinkedListItem<T>>
pub fn remove_iter_mut( &mut self, iter: &mut IterMut<T>, ) -> LinkedListResult<LinkedListItem<T>>
Removes the current iterator from the LinkedList
.
The element that was removed is returned, the iterator will point to the “ghost” non-element.
If the iterator is currently pointing to the “ghost” non-element then
no element is removed and Err(LinkedListError::IteratorNotInList)
is
returned.
Trait Implementations§
Source§impl<T: Clone> Clone for LinkedList<T>
impl<T: Clone> Clone for LinkedList<T>
Source§impl<T: Debug> Debug for LinkedList<T>
impl<T: Debug> Debug for LinkedList<T>
Source§impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T>
impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList<T>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T> Extend<T> for LinkedList<T>
impl<T> Extend<T> for LinkedList<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)