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

reverse liststack direction

parent 129c0af0
Branches main
No related merge requests found
......@@ -40,14 +40,18 @@ impl Stack for ListStack {
fn push_val(&mut self, i: i32) {
match self {
Val(v, o) => *self = Val(i, Some(Box::new(Val(*v,o.take())))),
Nil => *self = Val(i,None),
Val(value, other) => {
// create a new smart pointer to the current node
let new_boxed = Box::new(Val(*value, other.take()));
*self = Val(i, Some(new_boxed));
},
Nil => *self = Val(i, None)
};
}
fn top_val(&self) -> Option<&i32> {
match self {
Val(val,_o) => Some(val),
Val(value, _) => Some(value),
Nil => None
}
}
......@@ -55,10 +59,12 @@ impl Stack for ListStack {
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
......
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