Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 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.         private int size;
  12.         private LinkedList<T> linkList = new LinkedList<T>();
  13.         public LimitedSizeStack(int size)
  14.         {
  15.             if (size <= 0)
  16.                 throw new ArgumentException();
  17.             this.size = size;
  18.         }
  19.        
  20.         public void Push(T item)
  21.         {
  22.             linkList.AddLast(item);
  23.             if(linkList.Count > size)
  24.                 linkList.RemoveFirst();
  25.         }
  26.  
  27.         public T Pop()
  28.         {
  29.             if (linkList.Count == 0)
  30.                 throw new InvalidOperationException();
  31.             var result = linkList.Last.Value;  
  32.             linkList.RemoveLast();
  33.             return result;
  34.         }
  35.  
  36.         public int Count
  37.         {
  38.             get
  39.             {
  40.                 return linkList.Count;
  41.             }
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement