cs101-notes
  • CS 101 Notes
  • Big O Notation
  • Big O Cheat Sheet
  • Sorting
    • Summary
    • Bubble Sort
    • Selection Sort
    • Insertion Sort
    • Merge Sort
    • Quick Sort
  • Abstract Data Types
    • Linked List
    • Stack
    • Queue
    • Hash Maps
    • Minimum Ordered Heap
Powered by GitBook
On this page

Was this helpful?

  1. Abstract Data Types

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; }
};

PreviousLinked ListNextQueue

Last updated 5 years ago

Was this helpful?