Fixed Push and Pop methods on Stack

This commit is contained in:
Luke Else 2022-03-31 21:55:17 +01:00
parent 1738ef0e5a
commit c54401fdbb

View File

@ -21,13 +21,15 @@ namespace C_.Datastructures
} }
public void Push(T value){ public void Push(T value){
//Add an Item to the top of the stacks //Add an Item to the top of the stack
Count++;
if (Count > 0) if (Count > 0)
{ {
Count++;
Head = StackNode<T>.Create(value, Head); Head = StackNode<T>.Create(value, Head);
return; return;
} }
Head = StackNode<T>.Create(value, default);
return;
} }
public T? Pop(){ public T? Pop(){
@ -38,6 +40,10 @@ namespace C_.Datastructures
Count--; Count--;
value = Head!.Value; value = Head!.Value;
Head = Head.Next; Head = Head.Next;
if (Count == 0)
{
Head = default;
}
} }
return value; return value;
} }