Stack

Last in, First out.

const int MAX = 9999;
template <typename eType>
struct Stack {
  eType array[MAX];
  int top;
  Stack() { top = -1; }
  void push(eType x) {
    if (isFull()) throw "Stack is full";
    top += 1;
    array[top] = x;
  }
  eType pop() {
    if (isEmpty()) throw "Stack is empty";
    eType x = array[top];
    top -= 1;
    return x;
  }
  eType peek() {
    if (isEmpty()) throw "Stack is empty";
    return array[top];
  }
  bool isEmpty() { return top == -1; }
  bool isFull() { return top == MAX - 1; }
  int size() { return top + 1; }
};

Last updated

Was this helpful?