Array Based Stack Algorithm
Considered as a linear data structure, or more abstractly a sequential collection, the push and pop operations happen only at one end of the structure, referred to as the top of the stack. In computer science, a stack is an abstract data type that serves as a collection of components, with two principal operations:
1. push, which adds an component to the collection, and
2. pop, which removes the most recently added component that was not yet removed. Like concepts were developed, independently, by Charles Leonard Hamblin in the first half of 1954 and by Wilhelm Kämmerer in 1958.Stacks are often described by analogy to, a spring-loaded stack of plates in a cafeteria. Klaus Samelson and Friedrich L. Bauer of Technical University Munich proposed the idea of a stack in 1955 and filed a patent in 1957.In March 1988, at a time when Samelson was already dead, Bauer received the IEEE computer pioneer prize for the invention of the stack principle.
using System;
using System.Collections.Generic;
namespace DataStructures.ArrayBasedStack
{
/// <summary>
/// Implementation of an array based stack. FIFO style.
/// </summary>
/// <typeparam name="T">Generic Type.</typeparam>
public class ArrayBasedStack<T>
{
/// <summary>
/// <see cref="Array"/> based stack.
/// </summary>
private T[] stack;
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}"/> class.
/// </summary>
public ArrayBasedStack() => stack = Array.Empty<T>();
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}"/> class.
/// </summary>
/// <param name="item">Item to push onto the <see cref="ArrayBasedStack{T}"/>.</param>
public ArrayBasedStack(T item)
: this() => Push(item);
/// <summary>
/// Initializes a new instance of the <see cref="ArrayBasedStack{T}"/> class.
/// </summary>
/// <param name="items">Items to push onto the <see cref="ArrayBasedStack{T}"/>.</param>
public ArrayBasedStack(IEnumerable<T> items)
: this()
{
foreach (var item in items)
{
Push(item);
}
}
/// <summary>
/// Gets the number of elements on the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
public int Count => stack.Length;
/// <summary>
/// Removes all items from the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
public void Clear() => Array.Resize(ref stack, 0);
/// <summary>
/// Determines whether an element is in the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
/// <param name="item">The item to locate in the <see cref="ArrayBasedStack{T}"/>.</param>
/// <returns>True, if the item is in the stack.</returns>
public bool Contains(T item) => Array.IndexOf(stack, item) > -1;
/// <summary>
/// Returns the item at the top of the <see cref="ArrayBasedStack{T}"/> without removing it.
/// </summary>
/// <returns>The item at the top of the <see cref="ArrayBasedStack{T}"/>.</returns>
public T Peek() => stack[^1];
/// <summary>
/// Removes and returns the item at the top of the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
/// <returns>The item removed from the top of the <see cref="ArrayBasedStack{T}"/>.</returns>
public T Pop()
{
var item = stack[^1];
Array.Resize(ref stack, stack.Length - 1);
return item;
}
/// <summary>
/// Inserts an item at the top of the <see cref="ArrayBasedStack{T}"/>.
/// </summary>
/// <param name="item">The item to push onto the <see cref="ArrayBasedStack{T}"/>.</param>
public void Push(T item)
{
Array.Resize(ref stack, stack.Length + 1);
stack[^1] = item;
}
}
}