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 ESP<T>
- {
- public T Value { get; set; }
- public ESP<T> Next { get; set; }
- public ESP<T> Previous { get; set; }
- }
- public class LimitedSizeStack<T>
- {
- private ESP<T> Head;
- private ESP<T> Tail;
- private readonly int length;
- public LimitedSizeStack(int limit)
- {
- length = limit;
- }
- public void Push(T item)
- {
- ESP<T> Item = new ESP<T> { Value = item };
- if (Head == null) {
- Head = Tail = Item;
- } else {
- if (length == Count)
- {
- Head = Head.Next;
- --Count;
- }
- Item.Previous = Tail;
- if (Tail != null)
- {
- Tail.Next = Item;
- }
- }
- Tail = Item;
- ++Count;
- }
- public T Pop()
- {
- if (Count == 0) throw new InvalidOperationException();
- var res = Tail.Value;
- Tail = Tail.Previous;
- --Count;
- return res;
- }
- public int Count { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement