Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TodoApplication
  4. {
  5.     public class LimitedSizeStack<T>
  6.     {
  7.         private T[] array;
  8.         private int limit;
  9.         private int headPointer;
  10.         private int tailPointer;
  11.  
  12.         public LimitedSizeStack(int limit)
  13.         {
  14.             this.limit = limit;
  15.             array = new T[limit];
  16.             headPointer = 0;
  17.             tailPointer = 0;
  18.         }
  19.  
  20.         public void Push(T item)
  21.         {
  22.             if (headPointer - tailPointer >= limit) {
  23.                 tailPointer++;
  24.             }
  25.             array[headPointer++ % limit] = item;
  26.         }
  27.  
  28.         public T Pop()
  29.         {
  30.             if (headPointer == tailPointer)
  31.                 return default(T);
  32.             return array[--headPointer % limit];
  33.         }
  34.  
  35.         public int Count
  36.         {
  37.             get
  38.             {
  39.                 return headPointer - tailPointer;
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement