Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace TodoApplication
- {
- public class LimitedSizeStack<T>
- {
- private readonly T[] array;
- private int offset = -1;
- private int count;
- public LimitedSizeStack(int limit)
- {
- array = new T[limit];
- }
- public void Push(T item)
- {
- offset++;
- count++;
- if (count > array.Length)
- count = array.Length;
- array[offset % array.Length] = item;
- }
- public T Pop()
- {
- count--;
- if (count < 0)
- throw new InvalidOperationException("stack is empty");
- var value = array[offset % array.Length];
- offset--;
- return value;
- }
- public int Count => count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement