Struct LinkedList

Source
pub struct LinkedList<T> {
    pub val: Option<T>,
    pub next: Option<Box<LinkedList<T>>>,
}

Fields§

§val: Option<T>§next: Option<Box<LinkedList<T>>>

Implementations§

Source§

impl LinkedList<i32>

Source

pub fn new() -> LinkedList<i32>

§Creates an empty LinkedList that may hold i32 values
§Example
let list = mini_linked_list::LinkedList::<i32>::new();
Source

pub fn push_right(&mut self, x: i32)

§Adds an element to the right side of the list

This method uses O(N) operations, as it needs to traverse the entire list before appending the new value to the end of the list.

§Example
let mut list = mini_linked_list::LinkedList::<i32>::new();
list.push_right(1);
list.push_right(2);
list.push_right(3);
list.push_right(4);
assert_eq!(list.collect(), vec![1,2,3,4]);
Source

pub fn push_left(self, x: i32) -> LinkedList<i32>

§Adds an element to the left side of the list

This method works in O(1) operation, as it replaces the head of the list with a new one and no traversal thus is required.

The method returns the new memory address of the list that must be handled by the caller (typically reassigning the variable).

§Example
use mini_linked_list::LinkedList;
let mut list: LinkedList<i32> = LinkedList::<i32>::new();
list = list.push_left(1);
list = list.push_left(2);
list = list.push_left(3);
list = list.push_left(4);
assert_eq!(list.collect(), vec![4,3,2,1]);
Source

pub fn pop_left(self) -> PopLeftResult<i32>

§Pops the List head on the left side, returning a PopLeftResult

This operation works in O(1), as it only pops the head and no traversal is required.

It’s usage is not so straightforward, however, as it requires the caller to replace the reference to the list head with the address returned by this method, inside PopLeftResult.list

It’s advised to not call unwrap directly in PopLeftResult.list`` directly, but rather rely on safer Option` methods.

§Example
use mini_linked_list::{LinkedList, PopLeftResult};
let mut list: LinkedList<i32> = LinkedList::<i32>::new();
list.push_right(1);
list.push_right(2);
 
let result: PopLeftResult<i32> = list.pop_left();
let list = result.list.unwrap();
 
assert_eq!(list.collect(), vec![2]);
assert_eq!(result.val.unwrap(), 1);
 
let result: PopLeftResult<i32> = list.pop_left();
let list = result.list;
 
assert_eq!(list.is_none(), true);
assert_eq!(result.val.unwrap(), 2);
Source

pub fn pop_right(&mut self) -> Option<i32>

§Pops the List head on the right side.

This operation works in O(N), as it requires a full traversal of the list.

Whenever possible, prefer relying on the pop_left method, as it is more efficient.

§Example
use mini_linked_list::LinkedList;
let mut list: LinkedList<i32> = LinkedList::<i32>::new();
list.push_right(1);
list.push_right(2);
assert_eq!(list.pop_right().unwrap(), 2);
assert_eq!(list.pop_right().unwrap(), 1);
assert_eq!(list.pop_right().is_none(), true);
Source

pub fn collect(&self) -> Vec<i32>

§Collects the list into an Array

This method is used mostly for debugging and testing, but can also be used to iterate over the list without popping it’s values.

It’s not memory efficient, however, as it copies the entire data.

§Example
use mini_linked_list::LinkedList;
let mut list: LinkedList<i32> = LinkedList::<i32>::new();
list.push_right(1);
list.push_right(2);
assert_eq!(list.collect(), vec![1,2]);

Auto Trait Implementations§

§

impl<T> Freeze for LinkedList<T>
where T: Freeze,

§

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>
where T: Unpin,

§

impl<T> UnwindSafe for LinkedList<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.