Advertisement
elena1234

Create Custom Stack

Feb 14th, 2021
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace CreateCustomStack
  6. {
  7.     class CustomStack : IEnumerable<int>
  8.     {
  9.         private const int InitialCapacity = 4;
  10.         private int currentCapacity = InitialCapacity;      
  11.         private int[] elementsInTheStack;
  12.  
  13.         public CustomStack()
  14.         {
  15.            this.elementsInTheStack = new int[InitialCapacity];
  16.            this.currentCapacity = InitialCapacity;
  17.         }
  18.  
  19.         public CustomStack(int currentCapacity)          
  20.         {
  21.             this.elementsInTheStack = new int[currentCapacity];
  22.             this.currentCapacity = currentCapacity;
  23.         }
  24.  
  25.         public int Count { get; private set; }
  26.  
  27.         private void ResizeTheArray()
  28.         {
  29.             int[] newArray = new int[currentCapacity * 2];
  30.             for (int i = 0; i < this.Count; i++)
  31.             {
  32.                 newArray[i] = this.elementsInTheStack[i];
  33.             }
  34.             this.elementsInTheStack = newArray;
  35.             this.currentCapacity = newArray.Length;
  36.             this.Count++;
  37.         }
  38.         private void Shrink()
  39.         {
  40.             int[] newArray = new int[currentCapacity / 2];
  41.             for (int i = 0; i < this.Count - 1; i++)
  42.             {
  43.                 newArray[i] = this.elementsInTheStack[i];
  44.             }
  45.             this.elementsInTheStack = newArray;
  46.             this.currentCapacity = newArray.Length;
  47.             this.Count--;
  48.         }
  49.  
  50.         public void ValidateTheStack()
  51.         {
  52.             if (this.Count == 0)
  53.             {
  54.                 throw new Exception("The stack is empty!");
  55.             }
  56.         }
  57.  
  58.         public void Push (int element)
  59.         {
  60.             if(this.Count + 1 > currentCapacity)
  61.             {
  62.                 ResizeTheArray();
  63.                 this.elementsInTheStack[this.Count-1] = element;
  64.             }
  65.             else
  66.             {
  67.                 this.Count++;
  68.                 this.elementsInTheStack[this.Count-1] = element;                                  
  69.             }          
  70.         }
  71.  
  72.         public int Pop()
  73.         {
  74.             ValidateTheStack();
  75.             int lastElement = this.elementsInTheStack[this.Count - 1];
  76.             if (this.Count-1 <= currentCapacity / 4)
  77.             {
  78.                 Shrink();
  79.                 return lastElement;
  80.             }
  81.             else
  82.             {
  83.                 this.Count--;
  84.                 this.elementsInTheStack[this.Count] = default;              
  85.                 return lastElement;
  86.             }          
  87.         }
  88.  
  89.         public int Peek()
  90.         {
  91.             ValidateTheStack();
  92.             int lastElement = this.elementsInTheStack[this.Count - 1];
  93.             return lastElement;
  94.         }
  95.        
  96.         public IEnumerator<int> GetEnumerator()
  97.         {
  98.             for (int i = 0; i < this.Count; i++)
  99.             {
  100.                 yield return this.elementsInTheStack[i];
  101.             }          
  102.         }
  103.  
  104.         IEnumerator IEnumerable.GetEnumerator()
  105.         {
  106.             return GetEnumerator();
  107.         }
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement