#Implementation of a LinkedList
10 messages · Page 1 of 1 (latest)
pub mod Node {
#[derive(Debug, PartialEq)]
pub struct Node<T> {
data: T,
next: Option<Box<Node<T>>>
}
impl<T> Node<T> {
pub fn new(data: T, next: Option<Box<Node<T>>>) -> Self {
Self {
data: data,
next: next
}
}
pub fn get_next(&mut self) -> Option<Box<Node<T>>> {
self.next.take()
}
pub fn get_next_ref(&self) -> Option<&Node<T>> {
self.next.as_deref()
}
pub fn get_next_mut(&mut self) -> Option<&mut Node<T>> {
self.next.as_deref_mut()
}
pub fn set_next(&mut self, next: Option<Box<Node<T>>>) {
self.next = next;
}
pub fn get_value(&self) -> T
where T: Copy {
self.data
}
}
}
It exceeded the message limit
this is a valid implementation of a singly linked list, but mainly linked lists are used in low level programming are doubly linked lists or circular linked lists
linked lists as an idiom are rarely useful, the most notable use imo is lock free concurrency, which will probably use a custom data structure anyway
you just created the Ok Stack! https://rust-unofficial.github.io/too-many-lists/second.html
Learning Rust With Entirely Too Many Linked Lists
the fun stuff happens when you do doubly linked lists
Thank you for the comments. Next thing, I will try to implement lock free concurrent linked lists 
Thanks again. I am aware that it is not a good data structure and it was mostly for experiment purposes. Even though it is experimental, I wanted to make sure it idiomatic as much as possible to have good foundations overall. Also, read the link sent. I will also look into it more since it is also related with doubly linkedlists
