pub struct LinkedList<T> {
pub length: usize,
/* private fields */
}
Fields§
§length: usize
Get length of linked list
Implementations§
Source§impl<T> LinkedList<T>
impl<T> LinkedList<T>
Source§impl<T> LinkedList<T>
impl<T> LinkedList<T>
Sourcepub fn insert(&mut self, data: T, posn: usize) -> Result<(), Error>
pub fn insert(&mut self, data: T, posn: usize) -> Result<(), Error>
Insert elemet at posn in the linked list.
§Time Complexity
O(posn)
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.insert(44, 0);
linked_list.insert(22, 0);
linked_list.insert(33, 1);
assert_eq!(format!("{}", linked_list), "HEAD -> 22 -> 33 -> 44 -> None");
§Errors
The function returns Error::PositionOutOfBounds(posn, len) if position to be inserted is >= linked list length.
Source§impl<T> LinkedList<T>
impl<T> LinkedList<T>
Sourcepub fn delete_at_posn(&mut self, posn: usize) -> Result<(), Error>
pub fn delete_at_posn(&mut self, posn: usize) -> Result<(), Error>
Delete element at the given position.
§Time Complexity
O(posn)
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.append(1);
linked_list.append(2);
linked_list.append(3);
linked_list.append(4);
linked_list.delete_at_posn(2);
assert_eq!(format!("{}", linked_list), "HEAD -> 1 -> 2 -> 4 -> None");
§Errors
The function returns Error::PositionOutOfBounds(posn, len) if position to be deleted is >= linked list length.
Sourcepub fn delete_where<F: FnMut(&T) -> bool>(&mut self, f: F) -> Result<(), Error>
pub fn delete_where<F: FnMut(&T) -> bool>(&mut self, f: F) -> Result<(), Error>
Delete element at the given position.
§Time Complexity
O(n)
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.append(1);
linked_list.append(2);
linked_list.append(3);
linked_list.append(4);
// delete first 2 elements
let mut counter = 2;
linked_list.delete_where(move |element| {
counter -= 1;
counter >= 0
});
assert_eq!(format!("{}", linked_list), "HEAD -> 3 -> 4 -> None");
linked_list.delete_where(|element| element % 2 == 0);
assert_eq!(format!("{}", linked_list), "HEAD -> 3 -> None");
§Errors
The function returns Error::ElementDoesNotExist if no element of linked list returns true with closure f.
Source§impl<T> LinkedList<T>
impl<T> LinkedList<T>
Sourcepub fn reverse(self) -> LinkedList<T>
pub fn reverse(self) -> LinkedList<T>
Returns reversed linked list
§Time Complexity
O(n)
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.append(1);
linked_list.append(2);
linked_list.append(3);
linked_list.append(4);
linked_list = linked_list.reverse();
assert_eq!(format!("{}", linked_list), "HEAD -> 4 -> 3 -> 2 -> 1 -> None");
Source§impl<T> LinkedList<T>
impl<T> LinkedList<T>
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Produces mutable iterator
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.append(1);
linked_list.append(2);
linked_list.append(3);
linked_list.append(4);
linked_list.iter_mut().for_each(|element| *element += 1);
assert_eq!(format!("{}", linked_list), "HEAD -> 2 -> 3 -> 4 -> 5 -> None");
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Produces am immutable iterator
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.append(1);
linked_list.append(2);
linked_list.append(3);
let mut vector = Vec::new();
for element in linked_list.iter() {
vector.push(*element);
}
assert_eq!(vector, [1, 2, 3]);
Sourcepub fn into_iter(self) -> IntoIter<T> ⓘ
pub fn into_iter(self) -> IntoIter<T> ⓘ
Consumes the linked list and gives an iterator.
§Example
let mut linked_list = LinkedList::<i32>::new();
linked_list.append(1);
linked_list.append(2);
linked_list.append(3);
let mut vector: Vec<i32> = linked_list.into_iter().collect();
assert_eq!(vector, [1, 2, 3]);
Trait Implementations§
Source§impl<T: Debug> Debug for LinkedList<T>
impl<T: Debug> Debug 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>where
T: Send,
impl<T> Sync for LinkedList<T>where
T: Sync,
impl<T> Unpin for LinkedList<T>
impl<T> UnwindSafe for LinkedList<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more