Last active
December 28, 2018 13:24
-
-
Save aknuds1/6836aa1c2969a203d5513ffa53a9d186 to your computer and use it in GitHub Desktop.
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
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