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>
- {
- protected int limit;
- protected int counter = 0;
- protected StackItem<T> head;
- protected StackItem<T> tail;
- public LimitedSizeStack(int limit)
- {
- this.limit = limit;
- }
- public void Push(T item)
- {
- if (Overcame)
- {
- head = head.Next;
- Push(item);
- }
- else
- {
- if (head == null && tail == null)
- {
- head = tail = new StackItem<T> { Value = item };
- counter += 1;
- }
- else
- {
- if (Count == 1)
- {
- if (tail == null)
- {
- tail = new StackItem<T> { Value = item };
- head.Next = tail;
- counter += 1;
- }
- else
- {
- tail.Next = new StackItem<T> { Value = item };
- tail = tail.Next;
- counter += 1;
- }
- }
- else
- {
- if (tail == null)
- {
- counter += 1;
- tail = new StackItem<T> { Value = item };
- }
- else
- {
- tail.Next = new StackItem<T> { Value = item };
- tail = tail.Next;
- counter += 1;
- }
- }
- }
- }
- }
- public T Pop()
- {
- T crutch = tail.Value;
- tail = null;
- counter -= 1;
- return crutch;
- }
- protected bool Overcame
- {
- get
- {
- if (Count == limit) return true;
- else return false;
- }
- }
- public int Count
- {
- get
- {
- return counter;
- }
- }
- }
- public class StackItem<T>
- {
- public T Value
- {
- get;
- set;
- }
- public StackItem<T> Next
- {
- get;
- set;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement