Advertisement
themaleem

MyStack

Aug 27th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace CP
  6. {
  7.     internal class MyStack
  8.     {
  9.         #region(Fields)
  10.  
  11.         private string[] items;
  12.         private int top = -1;
  13.  
  14.         #endregion
  15.  
  16.         #region(Properties)
  17.  
  18.         public int Capacity => items.Length;
  19.  
  20.         public int Count => top + 1;
  21.  
  22.         #endregion
  23.  
  24.         #region(Constructors)
  25.  
  26.         public MyStack()
  27.         {
  28.             items = new string[4];
  29.         }
  30.  
  31.         public MyStack(IEnumerable<string> collection)
  32.         {
  33.             items = collection.Reverse().ToArray();
  34.             top = items.Length - 1;
  35.         }
  36.  
  37.         #endregion
  38.  
  39.         #region(Methods)
  40.  
  41.         public void Push(string item)
  42.         {
  43.             if (Count == Capacity)
  44.             {
  45.                 Resize(Capacity * 2);
  46.             }
  47.             items[++top] = item;
  48.         }
  49.  
  50.         public string Pop()
  51.         {
  52.             if (Count == 0)
  53.             {
  54.                 throw new InvalidOperationException("Stack is empty.");
  55.             }
  56.  
  57.             string item = items[top];
  58.             items[top--] = default;
  59.             return item;
  60.         }
  61.  
  62.         public string Peek()
  63.         {
  64.             if (Count == 0)
  65.             {
  66.                 throw new InvalidOperationException("Stack is empty.");
  67.             }
  68.             return items[top];
  69.         }
  70.  
  71.         public string[] ToArray()
  72.         {
  73.             string[] arr = new string[Count];
  74.             for (int i = 0; i <= top; i++)
  75.             {
  76.                 arr[Count - 1 - i] = items[i];
  77.             }
  78.             return arr;
  79.         }
  80.  
  81.         public void Clear()
  82.         {
  83.             for (int i = 0; i <= top; i++)
  84.             {
  85.                 items[i] = default;
  86.             }
  87.             top = -1;
  88.         }
  89.  
  90.         public bool Contains(string item)
  91.         {
  92.             for (int i = 0; i <= top; i++)
  93.             {
  94.                 if (items[i] == item)
  95.                 {
  96.                     return true;
  97.                 }
  98.             }
  99.             return false;
  100.         }
  101.  
  102.         public IEnumerable<string> Concat(IEnumerable<string> collection)
  103.         {
  104.             return this.ToArray().Concat(collection);
  105.         }
  106.  
  107.         public IEnumerable<string> Append(string item)
  108.         {
  109.             return this.ToArray().Append(item);
  110.         }
  111.  
  112.         public string ElementAt(int index)
  113.         {
  114.             if (index < 0 || index >= Count)
  115.             {
  116.                 throw new ArgumentOutOfRangeException(nameof(index));
  117.             }
  118.             return items[top - index];
  119.         }
  120.  
  121.         public void CopyTo(string[] array, int arrayIndex)
  122.         {
  123.             if (array == null)
  124.             {
  125.                 throw new ArgumentNullException(nameof(array));
  126.             }
  127.             if (arrayIndex < 0 || arrayIndex + Count > array.Length)
  128.             {
  129.                 throw new ArgumentOutOfRangeException(nameof(arrayIndex));
  130.             }
  131.             for (int i = 0; i < Count; i++)
  132.             {
  133.                 array[arrayIndex + i] = items[Count - 1 - i];
  134.             }
  135.         }
  136.  
  137.         private void Resize(int newSize)
  138.         {
  139.             string[] newItems = new string[newSize];
  140.             Array.Copy(items, 0, newItems, 0, Count);
  141.             items = newItems;
  142.         }
  143.  
  144.         #endregion
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement