2022-12-04 01:38:56 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
pub struct List<T> {
|
|
|
|
head: Option<Rc<Node<T>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Node<T> {
|
|
|
|
data: T,
|
|
|
|
parent: Option<Rc<Node<T>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> List<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
List { head: None }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_front(&self, data: T) -> List<T> {
|
|
|
|
List {
|
|
|
|
head: Some(Rc::new(Node {
|
|
|
|
data: data,
|
|
|
|
parent: self.head.clone(),
|
|
|
|
})),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 02:06:04 +00:00
|
|
|
pub fn get_data(&self) -> Option<&T> {
|
|
|
|
self.head.map(|rc_node| &rc_node.as_ref().data)
|
2022-12-04 01:38:56 +00:00
|
|
|
}
|
|
|
|
}
|