Stack Data Structure

A stack is a linear data structure that follows the principle of Last In First Out (LIFO). This means the last element inserted inside the stack is removed first.

You can think of the stack data structure as the pile of plates on top of another.

elements on stack are added on top and removed from top just like a pile of plate

And, if you want the plate at the bottom, you must first remove all the plates on top. This is exactly how the stack data structure works.

LIFO Principle of Stack

In programming terms, putting an item on top of the stack is called push and removing an item is called pop.

represent the LIFO principle by using push and pop operation

In the above image, although item 3 was kept last, it was removed first. This is exactly how the LIFO (Last In First Out) Principle works.

We can implement a stack in any programming language like C, C++, Java, Python or C#, but the specification is pretty much the same.

Basic Operations of Stack

There are some basic operations that allow us to perform different actions on a stack.

Working of Stack Data Structure

The operations work as follows:

Adding elements to the top of stack and removing elements from the top of stack

  1. A pointer called TOP is used to keep track of the top element in the stack.
  2. When initializing the stack, we set its value to -1 so that we can check if the stack is empty by comparing TOP == -1 .
  3. On pushing an element, we increase the value of TOP and place the new element in the position pointed to by TOP .
  4. On popping an element, we return the element pointed to by TOP and reduce its value.
  5. Before pushing, we check if the stack is already full
  6. Before popping, we check if the stack is already empty

Stack Implementations in Python, Java, C, and C++

The most common stack implementation is using arrays, but it can also be implemented using lists.

# Stack implementation in python # Creating a stack def create_stack(): stack = [] return stack # Creating an empty stack def check_empty(stack): return len(stack) == 0 # Adding items into the stack def push(stack, item): stack.append(item) print("pushed item: " + item) # Removing an element from the stack def pop(stack): if (check_empty(stack)): return "stack is empty" return stack.pop() stack = create_stack() push(stack, str(1)) push(stack, str(2)) push(stack, str(3)) push(stack, str(4)) print("popped item: " + pop(stack)) print("stack after popping an element: " + str(stack)) 
// Stack implementation in Java class Stack < private int arr[]; private int top; private int capacity; // Creating a stack Stack(int size) < arr = new int[size]; capacity = size; top = -1; >// Add elements into stack public void push(int x) < if (isFull()) < System.out.println("OverFlow\nProgram Terminated\n"); System.exit(1); >System.out.println("Inserting " + x); arr[++top] = x; > // Remove element from stack public int pop() < if (isEmpty()) < System.out.println("STACK EMPTY"); System.exit(1); >return arr[top--]; > // Utility function to return the size of the stack public int size() < return top + 1; >// Check if the stack is empty public Boolean isEmpty() < return top == -1; >// Check if the stack is full public Boolean isFull() < return top == capacity - 1; >public void printStack() < for (int i = 0; i > public static void main(String[] args) < Stack stack = new Stack(5); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.pop(); System.out.println("\nAfter popping out"); stack.printStack(); >>
// Stack implementation in C #include #include #define MAX 10 int count = 0; // Creating a stack struct stack < int items[MAX]; int top; >; typedef struct stack st; void createEmptyStack(st *s) < s->top = -1; > // Check if the stack is full int isfull(st *s) < if (s->top == MAX - 1) return 1; else return 0; > // Check if the stack is empty int isempty(st *s) < if (s->top == -1) return 1; else return 0; > // Add elements into stack void push(st *s, int newitem) < if (isfull(s)) < printf("STACK FULL"); >else < s->top++; s->items[s->top] = newitem; > count++; > // Remove element from stack void pop(st *s) < if (isempty(s)) < printf("\n STACK EMPTY \n"); >else < printf("Item popped= %d", s->items[s->top]); s->top--; > count--; printf("\n"); > // Print elements of stack void printStack(st *s) < printf("Stack: "); for (int i = 0; i < count; i++) < printf("%d ", s->items[i]); > printf("\n"); > // Driver code int main()
// Stack implementation in C++ #include #include using namespace std; #define MAX 10 int size = 0; // Creating a stack struct stack < int items[MAX]; int top; >; typedef struct stack st; void createEmptyStack(st *s) < s->top = -1; > // Check if the stack is full int isfull(st *s) < if (s->top == MAX - 1) return 1; else return 0; > // Check if the stack is empty int isempty(st *s) < if (s->top == -1) return 1; else return 0; > // Add elements into stack void push(st *s, int newitem) < if (isfull(s)) < cout else < s->top++; s->items[s->top] = newitem; > size++; > // Remove element from stack void pop(st *s) < if (isempty(s)) < cout else < cout items[s->top]; s->top--; > size--; cout // Print elements of stack void printStack(st *s) < printf("Stack: "); for (int i = 0; i < size; i++) < cout items[i] cout // Driver code int main()

Stack Time Complexity

For the array-based implementation of a stack, the push and pop operations take constant time, i.e. O(1) .

Applications of Stack Data Structure

Although stack is a simple data structure to implement, it is very powerful. The most common uses of a stack are:

Table of Contents