-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stacks
133 lines (131 loc) · 2.55 KB
/
Stacks
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*Design, Develop and Implement 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;
void push();
void pop();
void display();
void pali();
int main()
{
int choice;
while(choice)
{
printf("\n\n--------STACK OPERATIONS-----------\n"); printf("1.Push\n");
printf("2.Pop\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
pali();
break;
case 4:
display();
break;
case 5:
exit(0);
break;
default:
printf("\nInvalid choice:\n"); break;
}
}
return 0;
}
void push() //Inserting element into the stack {
int item,n;
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;
}
}
void pop() //deleting an element from the stack {
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item); }
}
void pali()
{
int digit,j,k,len=top+1,flag=0,ind=0 ,length = 0; int num[len],rev[len],i=0;
while(top!=-1)
{
digit= stack[top];
num[i]=digit;
top--;
i++;
}
for(j=0;j<len;j++)
{
printf("Numbers= %d\n",num[j]); }
printf("reverse operation : \n");
for(k=len-1;k>=0;k--)
{
rev[k]=num[ind];
ind++;
}
printf("reverse array : ");
for(k=0;k<len;k++)
{
printf("%d\n",rev[k]);
}
printf("check for palindrome :\n");
for(i=0;i<len;i++)
{
if(num[i]==rev[i])
{
length = length+1;
}
}
if(length==len)
{
printf("It is palindrome number\n"); }
else
{
printf("It is not a palindrome number\n"); }
top = len-1;
}
void display()
{
int i;
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]); }
}
}