Stack in Data Structure

What is Stack?

  • Stack is an ordered list of the same type of elements.
  • It is a linear list where all insertions and deletions are permitted only at one end of the list.
  • Stack is a LIFO (Last In First Out) structure.
  • In a stack, when an element is added, it goes to the top of the stack.
“Stack is a collection of similar data items in which both insertion and deletion operations are performed based on LIFO principle”.

There are two basic operations performed in a Stack:

1. Push()
2. Pop()

1. Push() function is used to add or insert new elements into the stack.

2. Pop() function is used to delete or remove an element from the stack.

  • When a stack is completely full, it is said to be Overflow state and if stack is completely empty, it is said to be Underflow state.
  • Stack allows operations at one end only. Stack behaves like a real life stack, for example, in a real life, we can remove a plate or dish from the top of the stack only or while playing a deck of cards, we can place or remove a card from top of the stack only.
    Similarly, here also, we can only access the top element of a stack.
  • According to its LIFO structure, the element which is inserted last, is accessed first.

Implementation of Stack

stack push

The above diagram represents a stack insertion operation. In a stack, inserting and deleting of elements are performed at a single position which is known as, Top.

Insertion operation can be performed using Push() function. New element is added at top of the stack and removed from top of the stack, as shown in the diagram below:

stack pop

An element is removed from top of the stack. Delete operation is based on LIFO principle. This operation is performed using a Pop() function. It means that the insertion and deletion operations are performed at one end i.e at Top.

Following table shows the Position of Top which indicates the status of stack:

Position of TopStatus of Stack
-1Stack is empty.
0Only one element in a stack.
N - 1Stack is full.
NStack is overflow. (Overflow state)

Stack using Array

Stack can be implemented using one-dimensional array. One-dimensional array is used to hold elements of a stack. Implementing a stack using array can store fixed number of data values. In a stack, initially top is set to -1. Top is used to keep track of the index of the top most element.

Stack can be defined using array as follows:

typedef struct stack
          int element [MAX];   
          int top;

In the above code snippet, MAX is a constant and can store defined number of elements. When the value of top becomes MAX – 1 after a series of insertion, it means that stack is full.

stack using array

Example: Program to implement a stack using Array

#include <stdio.h>
#include <conio.h>
#define MAX 50
void insert();
void delet();
void display();
int stack[MAX], top=-1, element;
void main()
    int ch;
        printf("\n\n\n\n 1. Insert\n 2. Delete\n 3. Display\n 4. Exit\n");
        printf("\n Enter Your Choice: ");
        scanf("%d", &ch);
            case 1:
            case 2:
            case 3:
            case 4:
            printf("\n\n Invalid entry. Please try again...\n");
void insert()
    if(top == MAX-1)
        printf("\n\n Stack is Full.");
        printf("\n\n Enter Element: ");
        scanf("%d", &element);
        stack[top] = element;
        printf("\n\n Element Inserted = %d", element);
void delet()
    if(top == -1)
        printf("\n\n Stack is Empty.");
        element = stack[top];
        printf("\n\n Element Deleted = %d", element);
void display()
    int i;
    if(top == -1)
    printf("\n\n Stack is Empty.");
        for(i=top; i>=0; i--)
            printf("\n%d", stack[i]);


1. Insert

stack array insert

2. Display

stack array display

3. Delete

stack array delete

Stack Operations

As we studied, there are two important operations used for adding the elements and removing the elements. They are Push() and Pop().

Following are the other operations used in Stack:

Peek()The peek() function gets the top element of the stack, without deleting it.
isEmpty()The isEmpty() function checks whether the stack is empty or not.
isFull()The isFull() function is used to check whether the stack is full or not.