Advertisement
tane_superior

Untitled

Mar 6th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 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 LimitedSizeStack<T>
  10.     {
  11.         protected int limit;
  12.         protected int counter = 0;
  13.         protected StackItem<T> head;
  14.         protected StackItem<T> tail;
  15.  
  16.         public LimitedSizeStack(int limit)
  17.         {
  18.             this.limit = limit;
  19.         }
  20.  
  21.         public void Push(T item)
  22.         {
  23.             if (Overcame)
  24.             {
  25.                 head = head.Next;
  26.                 Push(item);
  27.             }
  28.             else
  29.             {
  30.                 if (head == null && tail == null)
  31.                 {
  32.                     head = tail = new StackItem<T> { Value = item };
  33.                     counter += 1;
  34.                 }
  35.                 else
  36.                 {
  37.                     if (Count == 1)
  38.                     {
  39.                         if (tail == null)
  40.                         {
  41.                             tail = new StackItem<T> { Value = item };
  42.                             head.Next = tail;
  43.                             counter += 1;
  44.                         }
  45.                         else
  46.                         {
  47.                             tail.Next = new StackItem<T> { Value = item };
  48.                             tail = tail.Next;
  49.                             counter += 1;
  50.                         }
  51.                     }
  52.                     else
  53.                     {
  54.                         if (tail == null)
  55.                         {
  56.                             counter += 1;
  57.                             tail = new StackItem<T> { Value = item };
  58.                         }
  59.                         else
  60.                         {
  61.                             tail.Next = new StackItem<T> { Value = item };
  62.                             tail = tail.Next;
  63.                             counter += 1;
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.  
  70.         public T Pop()
  71.         {
  72.             T crutch = tail.Value;
  73.             tail = null;
  74.             counter -= 1;
  75.             return crutch;
  76.         }
  77.  
  78.         protected bool Overcame
  79.         {
  80.             get
  81.             {
  82.                 if (Count == limit) return true;
  83.                 else return false;
  84.             }
  85.         }
  86.  
  87.         public int Count
  88.         {
  89.             get
  90.             {
  91.                 return counter;
  92.             }
  93.         }
  94.     }
  95.  
  96.     public class StackItem<T>
  97.     {
  98.         public T Value
  99.         {
  100.             get;
  101.             set;
  102.         }
  103.  
  104.         public StackItem<T> Next
  105.         {
  106.             get;
  107.             set;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement