Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TodoApplication
  8. {
  9.     public class ESP<T>
  10.     {
  11.         public T Value { get; set; }
  12.         public ESP<T> Next { get; set; }
  13.         public ESP<T> Previous { get; set; }
  14.     }
  15.  
  16.     public class LimitedSizeStack<T>
  17.     {
  18.         private ESP<T> Head;
  19.         private ESP<T> Tail;
  20.         private readonly int length;
  21.  
  22.         public LimitedSizeStack(int limit)
  23.         {
  24.             length = limit;
  25.         }
  26.  
  27.         public void Push(T item)
  28.         {
  29.             ESP<T> Item = new ESP<T> { Value = item };
  30.  
  31.             if (Head == null) {
  32.                 Head = Tail = Item;
  33.             } else {
  34.                 if (length == Count)
  35.                 {
  36.                     Head = Head.Next;
  37.                     --Count;
  38.                 }
  39.                     Item.Previous = Tail;
  40.                     if (Tail != null)
  41.                     {
  42.                         Tail.Next = Item;
  43.                     }
  44.             }
  45.             Tail = Item;
  46.             ++Count;
  47.         }
  48.  
  49.         public T Pop()
  50.         {
  51.             if (Count == 0) throw new InvalidOperationException();
  52.             var res = Tail.Value;
  53.             Tail = Tail.Previous;
  54.             --Count;
  55.             return res;
  56.         }
  57.  
  58.         public int Count { get; private set; }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement