Created
January 30, 2018 11:44
-
-
Save richard-uk1/251c9155673a185fc84e7b7dba339811 to your computer and use it in GitHub Desktop.
Unfinished linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem; | |
pub struct SimpleLinkedList<T> { | |
head: Link<T> | |
} | |
impl<T> SimpleLinkedList<T> { | |
pub fn new() -> Self { | |
SimpleLinkedList { | |
head: Link::Empty | |
} | |
} | |
pub fn len(&self) -> usize { | |
unimplemented!() | |
} | |
pub fn push(&mut self, element: T) { | |
let new_node = Node { | |
value: element, | |
cons: mem::replace(&mut self.head, Link::Empty) | |
}; | |
self.head = Link::More(Box::new(new_node)); | |
} | |
pub fn pop(&mut self) -> Option<T> { | |
match mem::replace(&mut self.head, Link::Empty) { | |
Link::Empty => None, | |
Link::More(node) => { | |
let {value, cons } = *node; | |
self.head = cons; | |
Some(value) | |
} | |
} | |
} | |
pub fn peek(&self) -> Option<&T> { | |
unimplemented!() | |
} | |
} | |
impl<T: Clone> SimpleLinkedList<T> { | |
pub fn rev(&self) -> SimpleLinkedList<T> { | |
unimplemented!() | |
} | |
} | |
impl<'a, T: Clone> From<&'a [T]> for SimpleLinkedList<T> { | |
fn from(item: &[T]) -> Self { | |
unimplemented!() | |
} | |
} | |
impl<T> Into<Vec<T>> for SimpleLinkedList<T> { | |
fn into(mut self) -> Vec<T> { | |
unimplemented!() | |
} | |
} | |
enum Link<T> { | |
Empty, | |
More(Box<Node<T>>) | |
} | |
struct Node<T> { | |
value: T, | |
cons: Link<T> | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment