Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TodoApplication
- {
- public class LimitedSizeStack<T>
- {
- class StackItem
- {
- public StackItem(T item)
- {
- this.item = item;
- }
- public T item;
- public StackItem NextItem;
- public StackItem LastItem;
- }
- public LimitedSizeStack(int limit)
- {
- stackLimit = limit;
- }
- int stackLimit;
- StackItem head;
- StackItem tail;
- int stackCount;
- public void Push(T item)
- {
- StackItem stackItem = new StackItem(item);
- if (head == null)
- {
- head = tail = stackItem;
- stackCount++;
- }
- else
- {
- tail.NextItem = stackItem;
- stackItem.LastItem = tail;
- tail = stackItem;
- stackCount++;
- }
- if (stackCount > stackLimit)
- {
- head = head.NextItem;
- head.LastItem = null;
- stackCount--;
- }
- }
- public T Pop()
- {
- if (head == null) throw new InvalidOperationException();
- var result = tail.item;
- tail = tail.LastItem;
- tail.NextItem = null;
- stackCount--;
- return result;
- }
- public int Count
- {
- get
- {
- return stackCount;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement