From c54401fdbbba355e134478b804930256921157e8 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Thu, 31 Mar 2022 21:55:17 +0100 Subject: [PATCH] Fixed Push and Pop methods on Stack --- C#/Datastructures/Stack.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/C#/Datastructures/Stack.cs b/C#/Datastructures/Stack.cs index 625319c..93f00fb 100644 --- a/C#/Datastructures/Stack.cs +++ b/C#/Datastructures/Stack.cs @@ -21,13 +21,15 @@ namespace C_.Datastructures } 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) { - Count++; Head = StackNode.Create(value, Head); return; } + Head = StackNode.Create(value, default); + return; } public T? Pop(){ @@ -38,6 +40,10 @@ namespace C_.Datastructures Count--; value = Head!.Value; Head = Head.Next; + if (Count == 0) + { + Head = default; + } } return value; }