/** * @file arrayintstack.cs * @author Fred Sullivan * * Implements the ArrayIntStack class. */ using System; /** * A class that implements a stack of integers using an array. */ public class ArrayIntStack { int[] items; /**< The array of items in the stack. */ int top; /**< The index of the top of the stack. */ /** * Creates a stack. * @param size : The size of the stack. */ public ArrayIntStack(int size) { items = new int[size]; top = -1; } /** * Pushes an item onto the stack. * @param item : The item to push. * @throws : InvalidOperationException if the stack is full. */ public void Push(int item) { if (top == items.Length - 1) { throw new InvalidOperationException("Stack overflow"); } else { items[++top] = item; } } /** * Pops an item from the stack. * @returns The top of the stack. * @throws InvalidOperationException if the stack is empty. */ public int Pop() { if (top == -1) { throw new InvalidOperationException("Empty stack"); } else { return items[top--]; } } /** * Indicates whether the stack is empty. */ public bool Empty { get => top == -1; } }