forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedStack.cs
51 lines (40 loc) · 959 Bytes
/
LinkedStack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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; }
}
}