-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03.StackOperations.c
More file actions
135 lines (126 loc) · 2.43 KB
/
03.StackOperations.c
File metadata and controls
135 lines (126 loc) · 2.43 KB
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
134
135
/*
3 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<stdio.h>
#include<stdlib.h>
#define MAX 20
int stack_full(int top);
int stack_empty(int top);
void push(int stack[], int *top, int ele);
int pop(int stack[], int *top);
int is_palindrome(int stack[], int top);
void display(int stack[], int top);
void main()
{
int stack[MAX], top = -1, ele, ch;
for(;;)
{
printf("\nMenu\n");
printf("1. Push\n2. Pop\n3. Check Palindrome\n4. Stack Status\n5. Display\n6. Exit\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(stack_full(top))
printf("Stack Full\n");
else
{
printf("Enter an Element\n");
scanf("%d", &ele);
push(stack, &top, ele);
}
break;
case 2:
if(stack_empty(top))
printf("Stack Empty\n");
else
{
ele = pop(stack, &top);
printf("Deleted Element is %d", ele);
}
break;
case 3:
if(stack_empty(top))
printf("Stack Empty\n");
else if(is_palindrome(stack, top))
{
printf("Stack is Palindrome\n");
display(stack, top);
}
else
printf("Stack is not Palindrome\n");
break;
case 4:
if(stack_empty(top))
printf("Stack Empty\n");
else if(stack_full(top))
printf("Stack Full\n");
else
printf("Stack contains %d elements\n", top+1);
break;
case 5:
if(stack_empty(top))
printf("Stack Empty\n");
else
display(stack, top);
break;
case 6:
exit(0);
}
}
}
int stack_full(int top)
{
if(top == MAX-1)
return 1;
return 0;
}
int stack_empty(int top)
{
if(top == -1)
return 1;
return 0;
}
void push(int stack[], int *top, int ele)
{
stack[++(*top)] = ele;
}
int pop(int stack[], int *top)
{
return stack[(*top)--];
}
int is_palindrome(int stack[], int top)
{
int i, not_palindrome=0;
for(i=0; i<=top/2; i++)
{
if(stack[i] != stack[top-i])
{
not_palindrome = 1;
break;
}
}
if(not_palindrome)
return 0;
else
return 1;
}
void display(int stack[], int top)
{
int i;
for(i=0; i <= top; i++)
{
printf("%d ", stack[i]);
}
printf("\n");
}