algorithms

Algorithms Handbook

View on GitHub

Stack

Java examples

Overview

Stack is a linear data structure which follows a LIFO/FILO order for operations.

In a stack, the element deleted from the set is the one most recently inserted.

Applications

Common Procedures

STACK-EMPTY(S)
  if S.top == 0
    return TRUE
  else return FALSE

PUSH(S,x)
  S.top = S.top + 1
  S[S.top] = x

POP(S)
  if STACK-EMPTY(S)
    error "underflow"
  else S.top = S.top - 1
    return S[S.top + 1]

Design & Implementation

The two most common ways to implement a stack are using array or using linked list.