BCSL305 Data Structures and Applications Lab
Program 3:
Develop a menu driven Program in C for the following operations on STACK of Integers (Array
Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit
Support the program with appropriate functions for each of the above operations
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size],top=-1,flag=1;
int i,temp,item,rev[max_size],num[max_size];
void push();
void pop();
void display();
void pali();
int main()
{
int choice;
printf("\n\n--------STACK
OPERATIONS--------\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf(" ");
while(1)
{
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: push();break;
case 2: pop();
if(flag)
printf("\nThe poped element: %d\t",item);
temp=top; break;
case 3: pali();
Dept. of AI & ML SMVITM, Bantakal Page 10
BCSL305 Data Structures and Applications Lab
top=temp; break;
case 4: display(); break;
case 5: exit(0); break;
default: printf("\nInvalid choice:\n"); break;
}
}
}
void push() //Inserting element into the stack
{
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
temp=top;
}
void pop() //deleting an element from the stack
{
if(top==-1)
{
printf("Stack Underflow:");
flag=0;
}
else
{
item=stack[top];
top=top-1;
}
}
void pali()
{ i=0;
if(top==-1)
{
printf("Push some elements into the stack first\n");
}
else
{
Dept. of AI & ML SMVITM, Bantakal Page 11
BCSL305 Data Structures and Applications Lab
while(top!=-1)
{
rev[top]=stack[top];
pop();
}
top=temp;
for(i=0;i<=temp;i++)
{
if(stack[top--]==rev[i])
{
if(i==temp)
{
printf("Palindrome\n");
return;
}}}
printf("Not Palindrome\n");
}}
void display()
{
int i; top=temp;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}
Output:
Case 1:
--------STACK OPERATIONS--------
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
Enter the element to be inserted: 10
Dept. of AI & ML SMVITM, Bantakal Page 12
BCSL305 Data Structures and Applications Lab
Enter your choice: 1
Enter the element to be inserted: 20
Enter your choice: 1
Enter the element to be inserted: 30
Enter your choice: 1
Enter the element to be inserted: 40
Enter your choice: 1
Enter the element to be inserted: 50
Enter your choice: 4
The stack elements are:
50
40
30
20
10
Enter your choice: 2
The poped element: 50
Enter your choice: 3
Not Palindrome
Enter your choice: 4
The stack elements are:
40
30
20
10
Enter your choice: 5
Case 2:
--------STACK OPERATIONS--------
[Link]
[Link]
[Link]
[Link]
[Link]
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 1
Dept. of AI & ML SMVITM, Bantakal Page 13
BCSL305 Data Structures and Applications Lab
Enter the element to be inserted: 2
Enter your choice: 1
Enter the element to be inserted: 1
Enter your choice: 4
The stack elements are:
1
2
1
Enter your choice: 3
Palindrome
Enter your choice: 5
Dept. of AI & ML SMVITM, Bantakal Page 14