algo/csharp/08-stack/algo08_stack/LinkedStack.cs
robin 9b540eb9a8 rename project names;
add stack implementation with csharp
2019-01-21 13:38:55 +08:00

51 lines
959 B
C#

using System;
namespace algo08_stack
{
public class LinkedStack<T>
{
private StackListNode<T> _top;
public int Count { get; private set; }
public void Push(T val)
{
var newNode = new StackListNode<T>(val);
newNode.Next = _top;
_top = newNode;
Count++;
}
public T Pop()
{
if (_top == null) throw new InvalidOperationException("Stack empty");
T val = _top.Value;
_top = _top.Next;
Count--;
return val;
}
public void Clear()
{
while (Count > 0)
{
Pop();
}
}
}
public class StackListNode<T>
{
public StackListNode(T nodeValue)
{
Value = nodeValue;
}
public T Value { get; set; }
public StackListNode<T> Next { get; set; }
}
}