Stack Operation In Data Structure

Stack Operation In Data Structure, Definition, Code, Push, Pop, Full

Hello guys, welcome back to my blog, In this article, I will discuss stack operation in the data structure, performing stack operations such as push, pop, full, empty, and I will also share stack operation code with you.

If you need an article on some other topics then comment us below in the comment box. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. Different Types Of AWS Products (Amazon Web Service).
  2. C Language Interview Questions On Programs With Output.
  3. What Is The Internet And Who Is The Owner Of The Internet, ICANN.

Stack Operation In Data Structure

The stack is an essential data structure that stores its components in an arranged manner. A stack is a linear data structure that uses the principle, i.e., the components in a stack are added and removed only from one end. Therefore, a stack is called a LIFO (Last-In-First-Out) data structure, as the data or element that was inserted last is the first one to be brought out.

A stack holds three basic services: push, pop, and peek. The push method adds a component to the top of the stack and the pop process removes the component from the top of the stack. The peek method returns the value of the topmost component of the stack.

Push Operation

Push operation in data structure

The push operation is done to insert a component into the stack. The new component is added at the topmost position of the stack. However, before inserting the value, we need to first check if TOP=MAX–1, because if this is the case, then this stack is full and no longer insertions can be made. If an attempt is done to insert a value in a stack that is now full, then an OVERFLOW message should be printed.

Algorithm for push operation

Step 1: IF TOP = MAX-1
           PRINT "OVERFLOW"
           Go to Step 4
[END OF IF]
Step 2: SET TOP = TOP+1
Step 3: SET STACK[TOP] = VALUE
Step 4: END

Pop Operation

pop operation in data structure

The pop operation is done to delete the topmost component from the stack. But, before deleting the value, we need to first check if TOP=NULL because if this is the situation, then it indicates the stack is empty and no more further deletions can be done. If an attempt is done to delete a value from a stack that is now empty, then an UNDERFLOW message should be printed.

Algorithm for pop operation

Step 1: IF TOP = NULL
           PRINT "UNDERFLOW"
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP-1
Step 4: END

Peek Operation

Peek is an operation to find top value or that returns the value of the topmost component of the stack without removing it from the stack. But, the Peek operation first verifies if the stack is empty, i.e., if TOP = NULL, then a relevant message is printed, else the value is returned.

Algorithm for peek operation

Step 1: IF TOP = NULL
            PRINT STACK IS EMPTY
            Go to Step 3
Step 2: RETURN STACK[TOP]
Step 3: END

Program or code to perform push, pop, peek operation on a stacks in data structure?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 3

int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
int peek(int st[]);
void display(int st[]);

int main(int argc, char *argv[]){
    int val, option;
    do
    {
        printf("\n ~~~~~Main Menu~~~~~");
        printf("\n 1. Push");
        printf("\n 2. Pop");
        printf("\n 3. Peek");
        printf("\n 4. Display");
        printf("\n 5. Exit");
        printf("\n Please, enter your option :");
        scanf("%d", &option);
        switch(option)
        {
        case 1:
            printf("\n Enter the number to be pushed on stack: ");
            scanf("%d", &val);
            push(st, val);
            break;
        case 2:
            val = pop(st);
            if(val != -1)
                printf("\n The value deleted from stack is: %d", val);
            break;
        case 3:
            val = peek(st);
            if(val != -1)
                printf("\n The value stored at top of stack is: %d", val);
            break;
        case 4:
            display(st);
            break;
        }
    }while(option != 5);
    return 0;
}
void push(int st[], int val)
{
    if(top == MAX-1)
    {
        printf("\n Stack Overflow");
    }
    else
    {
        top++;
        st[top] = val;
    }
}
int pop(int st[])
{
    int val;
    if(top == -1)
    {
        printf("\n Stack Underflow");
        return -1;
    }
    else
    {
        val = st[top];
        top--;
        return val;
    }
}
void display(int st[])
{
    int i;
    if(top == -1)
        printf("\n Stack is empty");
    else
    {
        for(i=top;i>=0;i--)
            printf("\n %d", st[i]);
        printf("\n");
    }
}
int peek(int st[])
{
    if(top == -1)
    {
        printf("\n Stack is empty");
        return -1;
    }
    else
    return (st[top]);
}

Output:


 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :1

 Enter the number to be pushed on stack: 23

 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :4

 23

 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :1

 Enter the number to be pushed on stack: 34

 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :2

 The value deleted from stack is: 34
 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :1

 Enter the number to be pushed on stack: 54

 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :4

 54
 23

 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :3

 The value stored at top of stack is: 54
 ~~~~~Main Menu~~~~~
 1. Push
 2. Pop
 3. Peek
 4. Display
 5. Exit
 Please, enter your option :

I hope this article may help you all a lot. Thank you for reading. If you have any doubts related to this article “stack in data structure”, then comment below.

Also, read:

About The Author

Share Now