Skip to content

Instantly share code, notes, and snippets.

@derekdreery
Created January 30, 2018 11:44
Show Gist options
  • Save derekdreery/251c9155673a185fc84e7b7dba339811 to your computer and use it in GitHub Desktop.
Save derekdreery/251c9155673a185fc84e7b7dba339811 to your computer and use it in GitHub Desktop.
Unfinished linked list
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