-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSTACK.c
75 lines (72 loc) · 1.51 KB
/
STACK.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.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;
/*printf(" num rev\n");
for(t=0;t<=top;t++)
printf("%4d %4d\n",num[t],rev[t]);*///remove /* */ to display num and rev
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;
}