-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLab_prog03.c
209 lines (200 loc) · 5.13 KB
/
Lab_prog03.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#define SUCCESS 0
#define ERROR INT_MIN
typedef enum Menu {ch_exit, ch_create, ch_push, ch_pop, ch_palindrome, ch_display, ch_end} menu_t;
typedef struct {
size_t capacity; // max capacity of stack
int top; // identify top element in the stack
char* elem; // actual storage of elements.
}stack_t;
//-----------------------------------------------------------------
menu_t menu_options() {
menu_t choice = ch_end;
do {
printf("Enter your choice:\n");
printf("\t%u to create stack\n", ch_create);
printf("\t%u to push an element to stack\n", ch_push);
printf("\t%u to pop an element from stack\n", ch_pop);
printf("\t%u to check palindrome using stack\n", ch_palindrome);
printf("\t%u to display stack elements\n", ch_display);
printf("\t%u to exit\n", ch_exit);
printf("\tchoice: ");
scanf("%u", & choice);
} while (choice >=ch_end);
getchar();
return choice;
}
//-----------------------------------------------------------------
stack_t* create(size_t size){
stack_t* s = malloc(sizeof(stack_t));
if(s != NULL){
s->capacity = size;
s->elem = calloc(size, sizeof(char));
if(s->elem != NULL){
s->top = -1;
}
else{
free(s);
s = NULL;
}
}
return s;
}
//-----------------------------------------------------------------
bool isfull(stack_t* s){
if(s->top == s->capacity-1){
return true;
}
return false;
}
//-----------------------------------------------------------------
bool isempty(stack_t* s){
if(s->top == -1){
return true;
}
return false;
}
//-----------------------------------------------------------------
int push(stack_t* s, char ch){
if(isfull(s))
return ERROR;
s->elem[++s->top] = ch;
return SUCCESS;
}
//-----------------------------------------------------------------
int pop(stack_t* s){
if(isempty(s))
return ERROR;
return s->elem[s->top--];
}
//-----------------------------------------------------------------
void display(stack_t* s) {
if(isempty(s)) {
printf("No Elementsin stack\n");
return;
}
printf("Elements in stack are: ");
for(size_t i=0; i<=s->top; i++) {
printf(" %d ", s->elem[i]);
}
printf("\n");
}
//-----------------------------------------------------------------
int palindrome_check(char* string) {
stack_t* pal = NULL;
size_t length, i=0;
//length = strlen(string);
while(string[i]!='\0'){
i++;
length++;
}
size_t mid = length/2;
bool result;
pal = create(mid);
for(size_t i=0; i<mid; i++) {
push(pal, string[i]);
}
if(length%2 != 0) {
mid++;
}
while(pal->top>=0) {
if(pop(pal)!=string[mid]) {
free(pal);
return 0;
}
else {
mid++;
}
}
free(pal);
return 1;
}
//-----------------------------------------------------------------
void fn_exit(stack_t* s) {
free(s->elem);
free(s);
}
//-----------------------------------------------------------------
int main() {
stack_t* stack = NULL;
char* string;
unsigned int value, size, choice, result;
while(true) {
choice = menu_options();
switch(choice) {
case ch_create:
if(stack == NULL){
printf("Enter the size of the stack to be created: ");
scanf("%u", &size);
stack = create(size);
printf("Stack created of size %u\n", size);
continue;
}
printf("Stack already created\n");
case ch_push:
if(stack == NULL) {
printf("Stack not created\n");
continue;
}
else if(isfull(stack)) {
printf("Stack overflow\n");
continue;
}
else{
printf("\nEnter the element to be pushed to stack: ");
scanf("%u", &value);
push(stack, value);
printf("Element %u has been pushed to the stack\n", value);
break;
}
case ch_pop:
if(stack == NULL) {
printf("Stack not created\n");
continue;
}
else if(isempty(stack)) {
printf("Stack underflow\n");
continue;
}
else {
if(pop(stack)){
printf ("Element has been popped from the stack\n");
break;
}
}
case ch_palindrome:
printf("Enter the string: ");
scanf("%s", string);
result = palindrome_check(string);
if(result == 1) {
printf("The entered string is palindrome\n");
}
if(result == 0) {
printf("The entered string is not palindrome\n");
}
break;
case ch_display:
if(stack == NULL) {
printf("Stack not created\n");
continue;
}
display(stack);
break;
case ch_exit:
default:
if(stack == NULL) {
fn_exit(stack);
exit(0);
continue;
}
fn_exit(stack);
exit(0);
break;
}
}
return 0;
}