Advertisement
AmbushedRaccoon

Untitled

Feb 19th, 2018
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Fixed_Stack<T>
  5. {
  6.     public Fixed_Stack(UInt32 capacity)
  7.     {
  8.         _container = new List<T>();
  9.         _capacity = capacity;
  10.     }
  11.  
  12.     public void Push(T item)
  13.     {
  14.         _container.Add(item);
  15.         if (_container.Count > _capacity)
  16.         {
  17.             _container.RemoveAt(0);
  18.         }
  19.     }
  20.  
  21.     public bool Any()
  22.     {
  23.         return _container.Count > 0;
  24.     }
  25.  
  26.     public T Pop()
  27.     {
  28.         if (!Any())
  29.         {
  30.             throw new InvalidOperationException("The Stack is empty.");
  31.         }
  32.         T item = _container[_container.Count - 1];
  33.         _container.RemoveAt(_container.Count - 1);
  34.         return item;
  35.     }
  36.  
  37.     private List<T> _container;
  38.     private UInt32 _capacity;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement