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:
- Different Types Of AWS Products (Amazon Web Service).
- C Language Interview Questions On Programs With Output.
- 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
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
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:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs
- AWS Lambda, Working, Cost, Advantages, Disadvantages
- AWS Technical Interview Questions, Top 200+ AWS Questions
- Battery Management Systems Using Artificial Intelligence
- Best Engineering Branch For Future
- Best Programming Languages For Electrical and Electronics Engineers
- Big Data, Evolution Of Big Data, Benefits Of Big Data, Opportunities
- Bit Operation In C Programming With Example & Applications
- Blockchain Projects For Computer Science Engineers
- Blockchain Technology, History, Working, Applications, Advantages
- Brain Computer Interfaces Technology, Beyond AI, ML, IoT, Blockchain
- C Language Interview Questions On Programs With Output
- C Program On Arrays With Output For Placement Exams