Commit 3bd9c05a authored by Jan Laszlo Alexander Heuer's avatar Jan Laszlo Alexander Heuer
Browse files

aufgaben

parent 535a603e
use crate::Stack;
//use std::borrow::Borrow;
// TODO Complete implementation
impl Stack for Vec<i32> {
fn init() -> Self {
todo!()
Vec::new()
}
fn push_val(&mut self, i: i32) {
todo!()
self.push(i)
}
fn top_val(&self) -> Option<&i32> {
todo!()
self.last()
}
fn pop_val(&mut self) -> Option<i32> {
todo!()
self.pop()
}
fn is_empty(&self) -> bool {
todo!()
self.is_empty()
}
}
......@@ -40,13 +40,16 @@ impl Stack for ListStack {
fn push_val(&mut self, i: i32) {
match self {
Val(value, other) => *self = todo!(),
Nil => *self = todo!(),
Val(value, other) => *self = Val(i, Some(Box::new(Val(*value, other.take())))),
Nil => *self = Val(i, None),
};
}
fn top_val(&self) -> Option<&i32> {
todo!()
match self {
Val(i, _) => Some(i),
Nil => None,
}
}
fn pop_val(&mut self) -> Option<i32> {
......@@ -55,16 +58,19 @@ impl Stack for ListStack {
let popped_value = *value;
match other.take() {
None => *self = Nil,
Some(other) => todo!(),
Some(other) => *self = *other,
};
todo!()
Some(popped_value)
}
Nil => None,
}
}
fn is_empty(&self) -> bool {
todo!()
match self {
Val(_, _) => false,
Nil => true,
}
}
}
......
......@@ -14,15 +14,31 @@ pub struct Syntree<T> {
// Hint: Start with seek_node_mut
impl<'a, T> Syntree<T> {
pub fn new(value: T, id: ID) -> Syntree<T> {
todo!()
Syntree {
id,
value,
children: Vec::new(),
}
}
pub fn push_node(&mut self, parent_id: ID, new_node: Syntree<T>) -> Result<(), String> {
todo!()
match self.seek_node_mut(&parent_id) {
Some(parent_node) => {
parent_node.children.push(new_node);
Ok(())
}
None => Err("Parent-ID not found".to_string()),
}
}
pub fn prepend_node(&mut self, parent_id: ID, new_node: Syntree<T>) -> Result<(), String> {
todo!()
match self.seek_node_mut(&parent_id) {
Some(parent_node) => {
parent_node.children.insert(0, new_node);
Ok(())
}
None => Err("Parent_ID not found".to_string()),
}
}
pub fn insert_node(
......@@ -31,7 +47,17 @@ impl<'a, T> Syntree<T> {
index: usize,
new_node: Syntree<T>,
) -> Result<(), String> {
todo!()
match self.seek_node_mut(&parent_id) {
None => Err("Parent_ID not found".to_string()),
Some(parent_node) => {
if parent_node.children.len() < index {
Err("Index out of bound".to_string())
} else {
parent_node.children.insert(index, new_node);
Ok(())
}
}
}
}
// Anmerkung: `'a` Is ein Lebenszeit angabe für die Referenzen
......@@ -50,7 +76,16 @@ impl<'a, T> Syntree<T> {
}
pub fn seek_node_mut(&'a mut self, id: &ID) -> Option<&'a mut Syntree<T>> {
todo!()
if self.id == *id {
Some(self)
} else {
for child in &mut self.children {
if let Some(result) = child.seek_node_mut(id) {
return Some(result);
}
}
None
}
}
}
......
Supports Markdown
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