Skip to content
Snippets Groups Projects
Commit 129c0af0 authored by Julian Komaromy's avatar Julian Komaromy
Browse files

pass tests

parent 0e36fb0b
Branches
No related merge requests found
use crate::Stack;
// TODO Complete implementation
impl Stack for Vec<i32> {
fn init() -> Self {
return vec![]
Vec::new()
}
fn push_val(&mut self, i: i32) {
self.push(i)
self.push(i);
}
fn top_val(&self) -> Option<&i32> {
return self.last()
self.last()
}
fn pop_val(&mut self) -> Option<i32> {
return self.pop()
self.pop()
}
fn is_empty(&self) -> bool {
return self.is_empty()
self.is_empty()
}
}
// Nil is only used for truly empty lists, nonempty lists end in Val(x, None)
#[derive(Debug)]
pub enum ListStack {
Val(i32, Option<Box<ListStack>>),
......@@ -48,28 +48,27 @@ impl Stack for ListStack {
fn top_val(&self) -> Option<&i32> {
match self {
Val(val,_o) => Some(val),
Nil => None,
Nil => None
}
}
fn pop_val(&mut self) -> Option<i32> {
match self {
Val(value, other) => {
let popped_value = *value;
match other.take() {
None => *self = Nil,
Some(other) => *self = *other,
};
Some(popped_value)
}
Nil => None,
Nil => None
}
}
fn is_empty(&self) -> bool {
match self {
Val(_v,_o) => false,
Nil => true,
_ => false
}
}
}
......
......@@ -23,12 +23,12 @@ fn next_id() -> ID {
impl<T> SyntaxTree<T> {
/// Create a SyntaxTree with a root node that carries the given value
pub fn new(value: T) -> SyntaxTree<T> {
todo!()
SyntaxTree { id: next_id(), value, children: Vec::new() }
}
/// Add another SyntaxTree as last child of this tree
pub fn push_node(&mut self, new_node: SyntaxTree<T>) {
todo!()
self.children.push(new_node);
}
/// Create a new SyntaxTree with a root node that carries the given value. Add the created tree
......@@ -39,7 +39,7 @@ impl<T> SyntaxTree<T> {
/// Add another SyntaxTree as first child of this tree
pub fn prepend_node(&mut self, new_node: SyntaxTree<T>) {
todo!()
self.children.insert(0, new_node);
}
/// Create a new SyntaxTree with a root node that carries the given value. Add the created tree
......@@ -66,7 +66,15 @@ impl<T> SyntaxTree<T> {
if predicate(self) {
Some(self)
} else {
todo!()
for child in self.children.iter() {
match child.find_node(predicate) {
None => continue,
some => return some
}
}
// nothing was found in any of the children
None
}
}
......@@ -77,7 +85,7 @@ impl<T> SyntaxTree<T> {
&mut self,
predicate: fn(&SyntaxTree<T>) -> bool,
) -> Option<&SyntaxTree<T>> {
todo!()
self.find_node(predicate)
}
/// Return a reference to the value carried by the root of this tree
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment