Stack Implementation

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Write a program to implement the operations on stacks

Stack Algorithm:

Algorithm to push an item into stack:

 Step 1 - Check whether stack is FULL. (top == SIZE-1)


 Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not
possible!!!" and terminate the function.
 Step 3 - If it is NOT FULL, then increment top value by one (top++) and set
stack[top] to value (stack[top] = value).

Algorithm to pop an element from stack:


 Step 1 - Check whether stack is EMPTY. (top == -1)
 Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
 Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one
(top--).

Algorithm to display Elements from Stack:


 Step 1 - Check whether stack is EMPTY. (top == -1)
 Step 2 - If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
 Step 3 - If it is NOT EMPTY, then define a variable 'i' and initialize with top.
Display stack[i] value and decrement i value by one (i--).
 Step 3 - Repeat above step until i value becomes '0'.
Program:

/*Program To Implement The Operations On Stacks*/


#include <stdio.h>
#include <conio.h>
#define max 100
int s[max],top=-1,x,i;
int push();
int pop();
int display();
int main()
{
int c; //c=choice
printf("Welcome To Stack Operations:\n");
printf("\t1.Push\n");
printf("\t2.Pop\n");
printf("\t3.Display\n");
printf("Enter Your Choice:");
scanf("%d",&c);
while(c<=3)
{
switch(c)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;

}
printf("Enter Your Choice:");
scanf("%d",&c);
}
return 0;
}
int push( )
{
int o;
if(top==max-1)printf("Stack Is Full\n");
else
printf("Enter The Number Of Elements To Insert In Stack:");
scanf("%d",&o);
printf("Enter %d Elements:\n",o);
for(i=0;i<o;i++)
{
scanf("%d",&x);
s[++top]=x;
}
}
int pop()
{
if(top==-1)printf("Stack Is Underflow\n");
else
top--;
}
int display()
{
if(top==-1) printf("No Elements in Stack To Display");
else
printf("The Elements In Stack Are As Follows:\n");
for(i=top;i>=0;i--)
printf("\n%d",s[i]);
printf("\n");
}
Output:

Eg 1:
Welcome To Stack Operations:
1.Push
2.Pop
3.Display
Enter Your Choice:1
Enter The Number Of Elements To Insert In Stack:5
Enter 5 Elements:
441
484
529
576
625
Enter Your Choice:2
Enter Your Choice:3
The Elements In Stack Are As Follows:

576
529
484
441
Enter Your Choice:

Eg 2:

Welcome To Stack Operations:


1.Push
2.Pop
3.Display
Enter Your Choice:1
Enter The Number Of Elements To Insert In Stack:3
Enter 3 Elements:
600
700
800
Enter Your Choice:2
Enter Your Choice:2
Enter Your Choice:2
Enter Your Choice:2
Stack Is Underflow
Enter Your Choice:3
No Elements in Stack To Display
Enter Your Choice:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy