Skip to content

Instantly share code, notes, and snippets.

@aknuds1
Last active December 28, 2018 13:24
Show Gist options
  • Save aknuds1/6836aa1c2969a203d5513ffa53a9d186 to your computer and use it in GitHub Desktop.
Save aknuds1/6836aa1c2969a203d5513ffa53a9d186 to your computer and use it in GitHub Desktop.
pub struct SimpleLinkedList<T> {
head: Option<Box<Element<T>>>,
}
pub struct Element<T> {
data: T,
next: Option<Box<Element<T>>>,
}
impl<T> SimpleLinkedList<T> {
pub fn push(&mut self, element: T) {
match &self.head {
Some(head) => {
// Loop until we find a node with None for next
let mut cur = head;
while let Some(next) = &cur.next {
cur = next;
}
cur.next = Some(Box::new(Element {
data: element,
next: None,
}))
}
None => {
self.head = Some(Box::new(Element {
data: element,
next: None,
}))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment