Design, Develop and Implement a menu driven Program in C for the following operations on STACK of Integers
Stack : Stack is a linear data structure that uses LIFO (last in first out) as its functionality . here the last element is first poped out and the first element will be poped out at last . stack is a list of elements in which an element may be inserted or deleted only at one place called as top of the stack . this means that elements are removed from a stack. this means that the elements are removed from a stack in the reverse order of that in which they were inserted into stack .example : Plates stacked over one another in the canteen , Conversion of infix to prefix / infix to postfix
there are two basic operations of stack :
- PUSH - Push is used to insert an element into a stack .
- POP - Pop is used to delete a element from a stack .
top here contains the location of top element of stack i.e the last element of stack . Maxstack gives the maximum number of element that can be held by the stack . there are two conditions of stack :
- If top=0 or top=null then it means that the stack is empty or we can say underflow.
- If top=maxstack then stack is full or overflow condition
Program
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int s[5],top=-1;
void push()
{
if(top==4)
printf("\nStack overflow!!!!");
else
{
printf("\nEnter element to insert:");
scanf("%d",&s[++top]);
}
}
void pop()
{
if(top==-1)
printf("\nStack underflow!!!");
else
printf("\nElement popped is: %d",s[top--]);
}
void disp()
{
int t=top;
if(t==-1)
printf("\nStack empty!!");
else
printf("\nStack elements are:\n");
while(t>=0)
printf("%d ",s[t--]);
}
void pali()
{
int num[5],rev[5],i,t;
for(i=0,t=top;t>=0;i++,t--)
num[i]=rev[t]=s[t];
for(i=0;i<=top;i++)
if(num[i]!=rev[i])
break;
if(i==top+1)
printf("\nIt is a palindrome");
else
printf("\nIt is not a palindrome");
}
int main()
{
int ch;
do
{
printf("\n...Stack operations.....\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n________________\n");
printf("Enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push();break;
case 2:pop();break;
case 3:pali();break;
case 4:disp();break;
case 5:exit(0);
default:printf("\nInvalid choice");
}
}
while(1);
return 0;
}
|
Output
For more Visvesvaraya Technological University(VTU) CSE-III Sem Data Structure Lab Experiments Click Here