[][src]Struct data_structures::LinkedList

pub struct LinkedList<T> {
    pub length: usize,
    // some fields omitted
}

Fields

length: usize

Get length of linked list

Implementations

impl<T> LinkedList<T>[src]

pub fn new() -> Self[src]

Get new LinkedList

impl<T> LinkedList<T>[src]

pub fn append(&mut self, data: T)[src]

Append to the end of the 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);

 assert_eq!(format!("{}", linked_list), "HEAD -> 1 -> 2 -> 3 -> None");

pub fn insert(&mut self, data: T, posn: usize) -> Result<(), Error>[src]

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.

pub fn prepend(&mut self, data: T)[src]

Add element to the beginning of the linked list.

Time Complexity

O(1)

Example

 let mut linked_list = LinkedList::<i32>::new();
 linked_list.prepend(3);
 linked_list.prepend(2);
 linked_list.prepend(1);

 assert_eq!(format!("{}", linked_list), "HEAD -> 1 -> 2 -> 3 -> None");

impl<T> LinkedList<T>[src]

pub fn delete_at_posn(&mut self, posn: usize) -> Result<(), Error>[src]

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.

pub fn delete_where<F: FnMut(&T) -> bool>(&mut self, f: F) -> Result<(), Error>[src]

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.

impl<T> LinkedList<T>[src]

pub fn reverse(self) -> LinkedList<T>[src]

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");

impl<T> LinkedList<T>[src]

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Notable traits for IterMut<'a, T>

impl<'a, T> Iterator for IterMut<'a, T> type Item = &'a mut T;
[src]

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");

pub fn iter(&self) -> Iter<'_, T>

Notable traits for Iter<'a, T>

impl<'a, T> Iterator for Iter<'a, T> type Item = &'a T;
[src]

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]);

pub fn into_iter(self) -> IntoIter<T>

Notable traits for IntoIter<T>

impl<T> Iterator for IntoIter<T> type Item = T;
[src]

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

impl<T: Debug> Debug for LinkedList<T>[src]

impl<T: Display> Display for LinkedList<T>[src]

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.