Store the node in the list.

This commit is contained in:
Tom Alexander 2022-11-24 15:08:43 -05:00
parent 8dcb1318d6
commit 7976e017fd
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
1 changed files with 7 additions and 5 deletions

View File

@ -10,13 +10,13 @@ use nom::Parser;
type Link<'r, T> = Option<&'r Node<'r, T>>; type Link<'r, T> = Option<&'r Node<'r, T>>;
type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>; type Matcher = dyn for<'s> Fn(&'s str) -> IResult<&'s str, &'s str, VerboseError<&'s str>>;
pub struct Node<'r, T> { struct Node<'r, T> {
elem: T, elem: T,
next: Link<'r, T>, next: Link<'r, T>,
} }
pub struct List<'r, T> { pub struct List<'r, T> {
head: Link<'r, T>, head: Option<Node<'r, T>>,
} }
impl<'r, T> List<'r, T> { impl<'r, T> List<'r, T> {
@ -24,13 +24,15 @@ impl<'r, T> List<'r, T> {
List { head: None } List { head: None }
} }
pub fn with_additional_node(&self, element: T) -> Node<'r, T> { pub fn with_additional_node(&'r self, element: T) -> List<'r, T> {
let new_node = Node { let new_node = Node {
elem: element, elem: element,
next: self.head, next: self.head.as_ref(),
}; };
new_node List {
head: Some(new_node),
}
} }
} }