In this article, we will be implementing C language program to implement stack to perform push, pop & display operation in it. But before we proceed to that we must have knowledge of Stack.
Stack data structure
Stack is a linear data structure which follows a particular order in which the operations are performed. It can be represented by a real physical stack or pile, a structure where insertion and deletion of items take place at one end called top of the stack.
The basic implementation of a stack is also called a LIFO (Last In First Out) to demonstrate the way it accesses data, since as we will see there are various variations of stack implementations.
Common example: Suppose at your home you have multiple chairs then you put them together to form a vertical pile. From that vertical pile the chair which is placed last is always removed first.
Operations performned on stack
Mainly two basic operations(PUSH, POP ) are performed in the stack, here are the list of operations with brief explanation:
- Push: Adds an item on the top of the stack. If the stack is full, then it is said to be an Overflow condition.
- Pop: Removes an item from the top of the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
- Peek or Top: Returns the top element of the stack.
- isEmpty: True if no more items can be popped and there is no top item
- is-full: True if no more items can be pushed
- get-size: Returns the number of elements on the stack.
Declaration of Stack
typedef struct stack
{
int data[MAX];
int top;
}stack;
Algorithm for stack program in C
We will be focusing on two main operations of Stack, i.e, Push, & POP, so I will be creating Algorithm for PUSH, POP & Check if Stack is full or empty
Algorithm for PUSH
Step 1: If TOP >= SIZE – 1 then
Print "Stack Overflow"
Step 2: Else TOP = TOP + 1
Step 3: STACK [TOP] = X
Step 4: Exit
Algorithm for POP
Step 1: If TOP = -1 then
Print "Stack is Underflow"
Step 2: Return STACK [TOP]
Step 3: TOP = TOP - 1
Step 4: Exit
Algorithm to check if Stack is FULL
Step 1: IF TOP = MAX then
STATUS:=true;
Step 2: Otherwise
STATUS:=false;
Step 3: End of IF
Step 4: Exit
Algorithm to check if Stack is EMPTY
Step 1: IF TOP = 0 then
STATUS:=true;
Step 2: Otherwise
STATUS:=false;
Step 3: End of IF
Step 4: Exit
C program to implement Stack PUSP, POP operation
#include<stdio.h>
//Stack size
#define SIZE 5
//Declare stack functions
void push(int *STACK,int x);
void pop(int *a);
void display(int *a);
//Decalre TOP and STACK variables
int TOP=-1;
int STACK[SIZE];
void main()
{
int x,ch;
//implement continues while loop using 1
while(1)
{
printf("Select Your Choice:\n");
printf("(1) PUSH\n");
printf("(2) POP\n");
printf("(3) Display\n");
printf("(4) Enter 4 to exit \n");
printf("Enter Your Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter Element:");
scanf("%d",&x);
push(STACK,x);
break;
case 2:
pop(STACK);
break;
case 3:
display(STACK);
break;
case 4:
//break loop using exit
exit(0);
}
}
}
void push(int *STACK,int x)
{
//check if TOP value (TOP=current stack value) is greater than or equal SIZE
// if yes Stack Overlfowed cannot enter new data in STACK
if(TOP>=SIZE-1)
{
printf(" Stack Overflowed ");
}
//if there is more space in STACK eneter data
else
{
TOP=TOP+1;
STACK[TOP]=x;
}
}
void pop(int *STACK)
{
int x;
//check is TOP (TOP=current stack value) of stack is equal to -1
//then is it already empty
if(TOP==-1){
printf("Stack is Underflow");
}
//remove top element from Stack
else
{
printf("Deleted Element is %d\n",STACK[TOP]);
//decrement TOP value
TOP=TOP-1;
}
}
void display(int *STACK)
{
int i;
//check is TOP (TOP=current stack value) of stack is equal to -1
//then is it already empty
if(TOP==-1){
printf("Stack is Empty\n");
}
else
{
printf ("\n Stack element are \n");
//loop through each value of stack and print it
for(i=TOP;i>=0;i--)
{
printf("%d\t",STACK[i]);
}
}
}
I have commented out code for you to understand important lines of the code for you to understand it, here is the output of the code which I ran on online compiler here
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
1
Enter Element:11
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
1
Enter Element:22
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
1
Enter Element:33
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
1
Enter Element:44
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
3
Stack element are
44 33 22 11 Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
2
Deleted Element is 44
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
2
Deleted Element is 33
Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
3
Stack element are
22 11 Select Your Choice:
(1) PUSH
(2) POP
(3) Display
(4) Enter 4 to exit
Enter Your Choice
4
You may also like :
Program for insertion sorting in C (With explanation)
Program for Matrix multiplication in C (With & Without pointers)
Feel free to post your questions related to this post, below in the comment section.