Advertisement
yahorrr

Untitled

Nov 28th, 2022
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace GenericStackTask
  6. {
  7.     /// <summary>
  8.     /// Represents extendable last-in-first-out (LIFO) collection of the specified type T.
  9.     /// </summary>
  10.     /// <typeparam name="T">Specifies the type of elements in the stack.</typeparam>
  11.     public class Stack<T> : IEnumerable<T>
  12.     {
  13.         private T[] items;
  14.         private int count;
  15.         private int version;
  16.  
  17.         /// <summary>
  18.         /// Initializes a new instance of the <see cref="Stack{T}"/> class that is empty and has the default initial capacity.
  19.         /// </summary>
  20.         public Stack()
  21.             : this(2)
  22.         {
  23.         }
  24.  
  25.         /// <summary>
  26.         /// Initializes a new instance of the <see cref="Stack{T}"/> class that is empty and has.
  27.         /// the specified initial capacity.
  28.         /// </summary>
  29.         /// <param name="capacity">The initial number of elements of stack.</param>
  30.         public Stack(int capacity)
  31.         {
  32.             this.items = new T[capacity];
  33.             this.count = 0;
  34.             this.version = 0;
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Initializes a new instance of the <see cref="Stack{T}"/> class that contains elements copied.
  39.         /// from the specified collection and has sufficient capacity to accommodate the
  40.         /// number of elements copied.
  41.         /// </summary>
  42.         /// <param name="collection">The collection to copy elements from.</param>
  43.         public Stack(IEnumerable<T>? collection)
  44.             : this()
  45.         {
  46.             if (collection == null)
  47.             {
  48.                 throw new ArgumentNullException(nameof(collection));
  49.             }
  50.  
  51.             foreach (var item in collection)
  52.             {
  53.                 this.Push(item);
  54.             }
  55.  
  56.             this.version = 0;
  57.         }
  58.  
  59.         /// <summary>
  60.         /// Gets the number of elements contained in the stack.
  61.         /// </summary>
  62.         public int Count { get => this.count; }
  63.  
  64.         /// <summary>
  65.         /// Removes and returns the object at the top of the stack.
  66.         /// </summary>
  67.         /// <returns>The object removed from the top of the stack.</returns>
  68.         public T Pop()
  69.         {
  70.             if (this.count == 0)
  71.             {
  72.                 throw new InvalidOperationException("Stack is empty.");
  73.             }
  74.  
  75.             this.version++;
  76.             return this.items[--this.count];
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Returns the object at the top of the stack without removing it.
  81.         /// </summary>
  82.         /// <returns>The object at the top of the stack.</returns>
  83.         public T Peek()
  84.         {
  85.             return this.items[this.count - 1];
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Inserts an object at the top of the stack.
  90.         /// </summary>
  91.         /// <param name="item">The object to push onto the stack.
  92.         /// The value can be null for reference types.</param>
  93.         public void Push(T item)
  94.         {
  95.             if (this.count == this.items.Length)
  96.             {
  97.                 Array.Resize(ref this.items, this.items.Length * 2);
  98.             }
  99.  
  100.             this.items[this.count++] = item;
  101.             this.version++;
  102.         }
  103.  
  104.         /// <summary>
  105.         /// Copies the elements of stack to a new array.
  106.         /// </summary>
  107.         /// <returns>A new array containing copies of the elements of the stack.</returns>
  108.         public T[] ToArray()
  109.         {
  110.             T[] result = new T[this.count];
  111.             for (int i = 0; i < this.count; i++)
  112.             {
  113.                 result[this.count - i - 1] = this.items[i];
  114.             }
  115.  
  116.             return result;
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Determines whether an element is in the stack.
  121.         /// </summary>
  122.         /// <param name="item">The object to locate in the stack. The value can be null for reference types.</param>
  123.         /// <returns>Return true if item is found in the stack; otherwise, false.</returns>
  124.         public bool Contains(T item)
  125.         {
  126.             for (int i = 0; i < this.items.Length; i++)
  127.             {
  128.                 if (this.items[i].Equals(item))
  129.                 {
  130.                     return true;
  131.                 }
  132.             }
  133.  
  134.             return false;
  135.         }
  136.  
  137.         /// <summary>
  138.         /// Removes all objects from the stack.
  139.         /// </summary>
  140.         public void Clear()
  141.         {
  142.             this.count = 0;
  143.             this.version++;
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Returns an enumerator for the stack.
  148.         /// </summary>
  149.         /// <returns>Return Enumerator object for the stack.</returns>
  150.         public IEnumerator<T> GetEnumerator()
  151.         {
  152.             return new Enumerator(this);
  153.         }
  154.  
  155.         IEnumerator IEnumerable.GetEnumerator()
  156.         {
  157.             return this.GetEnumerator();
  158.         }
  159.  
  160.         private class Enumerator : IEnumerator<T>
  161.         {
  162.             private Stack<T> stack;
  163.             private int index;
  164.             private int version;
  165.  
  166.             public Enumerator(Stack<T> stack)
  167.             {
  168.                 this.stack = stack;
  169.                 this.index = -1;
  170.                 this.version = stack.version;
  171.             }
  172.  
  173.             public T Current
  174.             {
  175.                 get
  176.                 {
  177.                     return this.stack.items[this.index];
  178.                 }
  179.             }
  180.  
  181.             object IEnumerator.Current
  182.             {
  183.                 get
  184.                 {
  185.                     return this.Current;
  186.                 }
  187.             }
  188.  
  189.             public void Dispose()
  190.             {
  191.             }
  192.  
  193.             public bool MoveNext()
  194.             {
  195.                 if (this.version != this.stack.version)
  196.                 {
  197.                     throw new InvalidOperationException();
  198.                 }
  199.  
  200.                 if (this.index < this.stack.count - 1)
  201.                 {
  202.                     this.index++;
  203.                     return true;
  204.                 }
  205.  
  206.                 return false;
  207.             }
  208.  
  209.             public void Reset()
  210.             {
  211.                 this.index = -1;
  212.             }
  213.         }
  214.     }
  215. }
  216.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement