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>
- {
- LimitedSizeStackItem<T> head;
- LimitedSizeStackItem<T> tail;
- int count;
- int stackLimit;
- public LimitedSizeStack(int limit)
- {
- stackLimit = limit;
- }
- public void Push(T item)
- {
- if (!isLimitZero())
- {
- if (head == null)
- {
- tail = head = new LimitedSizeStackItem<T> { Item = item, Next = null, Previous = null };
- count++;
- }
- else
- {
- if (isStackFull())
- {
- head = head.Next;
- head.Previous = null;
- }
- else
- count++;
- var stackItem = new LimitedSizeStackItem<T> { Item = item, Next = null, Previous = tail };
- tail.Next = stackItem;
- tail = stackItem;
- }
- }
- }
- public T Pop()
- {
- if (!isLimitZero() && count != 0)
- {
- var result = tail.Item;
- count--;
- if (tail != head)
- tail = tail.Previous;
- if (head == null)
- tail = null;
- return result;
- }
- return default;
- }
- public int Count
- {
- get
- {
- return count;
- }
- }
- private bool isStackFull()
- {
- return count == stackLimit;
- }
- private bool isLimitZero()
- {
- return stackLimit == 0;
- }
- }
- public class LimitedSizeStackItem<T>
- {
- public T Item { get; set; }
- public LimitedSizeStackItem<T> Next { get; set; }
- public LimitedSizeStackItem<T> Previous { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement