Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace GenericStackTask
- {
- /// <summary>
- /// Represents extendable last-in-first-out (LIFO) collection of the specified type T.
- /// </summary>
- /// <typeparam name="T">Specifies the type of elements in the stack.</typeparam>
- public class Stack<T> : IEnumerable<T>
- {
- private T[] items;
- private int count;
- private int version;
- /// <summary>
- /// Initializes a new instance of the <see cref="Stack{T}"/> class that is empty and has the default initial capacity.
- /// </summary>
- public Stack()
- : this(2)
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stack{T}"/> class that is empty and has.
- /// the specified initial capacity.
- /// </summary>
- /// <param name="capacity">The initial number of elements of stack.</param>
- public Stack(int capacity)
- {
- this.items = new T[capacity];
- this.count = 0;
- this.version = 0;
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="Stack{T}"/> class that contains elements copied.
- /// from the specified collection and has sufficient capacity to accommodate the
- /// number of elements copied.
- /// </summary>
- /// <param name="collection">The collection to copy elements from.</param>
- public Stack(IEnumerable<T>? collection)
- : this()
- {
- if (collection == null)
- {
- throw new ArgumentNullException(nameof(collection));
- }
- foreach (var item in collection)
- {
- this.Push(item);
- }
- this.version = 0;
- }
- /// <summary>
- /// Gets the number of elements contained in the stack.
- /// </summary>
- public int Count { get => this.count; }
- /// <summary>
- /// Removes and returns the object at the top of the stack.
- /// </summary>
- /// <returns>The object removed from the top of the stack.</returns>
- public T Pop()
- {
- if (this.count == 0)
- {
- throw new InvalidOperationException("Stack is empty.");
- }
- this.version++;
- return this.items[--this.count];
- }
- /// <summary>
- /// Returns the object at the top of the stack without removing it.
- /// </summary>
- /// <returns>The object at the top of the stack.</returns>
- public T Peek()
- {
- return this.items[this.count - 1];
- }
- /// <summary>
- /// Inserts an object at the top of the stack.
- /// </summary>
- /// <param name="item">The object to push onto the stack.
- /// The value can be null for reference types.</param>
- public void Push(T item)
- {
- if (this.count == this.items.Length)
- {
- Array.Resize(ref this.items, this.items.Length * 2);
- }
- this.items[this.count++] = item;
- this.version++;
- }
- /// <summary>
- /// Copies the elements of stack to a new array.
- /// </summary>
- /// <returns>A new array containing copies of the elements of the stack.</returns>
- public T[] ToArray()
- {
- T[] result = new T[this.count];
- for (int i = 0; i < this.count; i++)
- {
- result[this.count - i - 1] = this.items[i];
- }
- return result;
- }
- /// <summary>
- /// Determines whether an element is in the stack.
- /// </summary>
- /// <param name="item">The object to locate in the stack. The value can be null for reference types.</param>
- /// <returns>Return true if item is found in the stack; otherwise, false.</returns>
- public bool Contains(T item)
- {
- for (int i = 0; i < this.items.Length; i++)
- {
- if (this.items[i].Equals(item))
- {
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// Removes all objects from the stack.
- /// </summary>
- public void Clear()
- {
- this.count = 0;
- this.version++;
- }
- /// <summary>
- /// Returns an enumerator for the stack.
- /// </summary>
- /// <returns>Return Enumerator object for the stack.</returns>
- public IEnumerator<T> GetEnumerator()
- {
- return new Enumerator(this);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- private class Enumerator : IEnumerator<T>
- {
- private Stack<T> stack;
- private int index;
- private int version;
- public Enumerator(Stack<T> stack)
- {
- this.stack = stack;
- this.index = -1;
- this.version = stack.version;
- }
- public T Current
- {
- get
- {
- return this.stack.items[this.index];
- }
- }
- object IEnumerator.Current
- {
- get
- {
- return this.Current;
- }
- }
- public void Dispose()
- {
- }
- public bool MoveNext()
- {
- if (this.version != this.stack.version)
- {
- throw new InvalidOperationException();
- }
- if (this.index < this.stack.count - 1)
- {
- this.index++;
- return true;
- }
- return false;
- }
- public void Reset()
- {
- this.index = -1;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement