Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class StackItem<T>
- {
- public T Value;
- public StackItem<T> Previous;
- }
- public class Stack<T>
- {
- public StackItem<T> Tail;
- private int Count;
- public void Push(T item)
- {
- var stackItem = new StackItem<T> { Value = item };
- if (Count == 0)
- {
- stackItem.Previous = null;
- Tail = stackItem;
- Count++;
- return;
- }
- stackItem.Previous = Tail;
- Tail = stackItem;
- Count++;
- }
- public T Pop()
- {
- if (Count == 0) throw new InvalidOperationException();
- var item = Tail;
- Tail = Tail.Previous;
- Count--;
- return item.Value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement