organic/src/parser/list.rs

32 lines
587 B
Rust
Raw Normal View History

2022-12-04 01:38:56 +00:00
use std::rc::Rc;
2022-12-04 02:35:30 +00:00
#[derive(Debug, Clone)]
2022-12-04 01:38:56 +00:00
pub struct List<T> {
head: Option<Rc<Node<T>>>,
}
2022-12-04 02:35:30 +00:00
#[derive(Debug, Clone)]
2022-12-04 01:38:56 +00:00
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(),
})),
}
}
pub fn get_data(&self) -> Option<&T> {
2022-12-04 02:50:06 +00:00
self.head.as_ref().map(|rc_node| &rc_node.data)
2022-12-04 01:38:56 +00:00
}
}