-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack_array.c
241 lines (189 loc) · 6.23 KB
/
Stack_array.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <stdlib.h>
struct stack{
int* array;
int size;
int capacity;
};
struct stack* stack_initializer(int new_stack_size)
{
struct stack* stack = (struct stack*) malloc(sizeof(struct stack));
if (stack == NULL)
{
printf("**memory allocation error**\n");
exit;
}
stack->array = (int*) malloc(new_stack_size * sizeof(int));
if (stack->array == NULL)
{
printf("**array memory allocation failure**\n");
exit;
}
stack->size = 0;
stack->capacity = new_stack_size;
printf("\nsize of stack = %d\n", stack->size);
printf("stack MAX capacity = %d\n", stack->capacity);
printf("**stack initialization successful**\n");
return stack;
}
void stack_push(struct stack* stack, int new_top_data)
{
int extra_space = stack->capacity - stack->size;
if (stack->size == stack->capacity)
{
printf("\n**stack is full! Please increase stack size before element addition**\n");
return;
}
printf("\nsize before element addition = %d\n", stack->size);
stack->array[stack->size] = new_top_data;
stack->size = stack->size + 1;
printf("size after element addition = %d\n", stack->size);
printf("**element push operation successful**\n");
}
void stack_pop(struct stack* stack)
{
int extra_space = stack->capacity - stack->size;
if (stack->size > 0) {
stack->size = stack->size - 1;
} else {
printf("\n**Error: Stack is empty. Cannot pop.**\n");
return;
}
printf("\n**Element pop operation successful**\n");
// auto shrinker
if (extra_space >= 6)
{
stack->array = (int*) realloc(stack->array, (stack->capacity - 5) * sizeof(int));
stack->capacity = stack->capacity - 5;
}
}
void stack_resizer(struct stack** stack, int new_stack_size)
{
int old_stack_size = (*stack)->capacity;
printf("\ncurrent stack MAX capacity = %d\n", (*stack)->capacity);
(*stack)->array = (int*) realloc((*stack)->array, (new_stack_size*sizeof(int)));
(*stack)->capacity = new_stack_size;
printf("new stack MAX capacity = %d\n", (*stack)->capacity);
if ((*stack)->capacity != 0 && (*stack)->capacity > old_stack_size)
{
printf("**stack resizing successful**\n");
}
}
int stack_peek(struct stack* stack)
{
if (stack->size == 0 || stack->capacity == 0)
{
printf("\n**stack is empty**\n");
return -1;
}
int org_top = stack->size - 1;
printf("\nelement at the top of the stack = %d\n", stack->array[org_top]);
return stack->array[org_top];
}
int stack_isempty_or_full_checker(struct stack* stack)
{
if (stack->size == 0)
{
printf("\n**stack is empty**\n");
return -1;
}
else if (stack->size != stack->capacity)
{
printf("\n**stack is not empty**\n");
return 0;
}
else
{
printf("\n**stack is full**\n");
return 1;
}
}
void stack_printer(struct stack* stack)
{
if (stack_isempty_or_full_checker(stack) == -1)
{
return;
}
printf("\ncurrent stack structure\n");
for (int i = 0; i < stack->size; i++)
{
printf("value at %d = %d\n", i, stack->array[i]);
}
}
int main()
{
int mode;
int new_top_data, new_stack_size, peeked_element;
struct stack* stack;
printf("This program implements the data structure stack using Dynamic arrays.\n");
printf("following are the Options avaliable for list manipulation:\n1.Initialize stack.\n2.push a new element to the stack.\n3.pop the top element from the stack.\n4.resize stack (incremental only).\n5.peek top of stack.\n6.check if stack is empty or not.\n7.Re-print operations panel.\n8.EXIT.\n");
while(mode != 10)
{
printf("please enter the operation you want to perform\n");
scanf("%d", &mode);
switch (mode)
{
case 1:
printf("please enter the MAX capacity of the stack you want: \n");
scanf("%d", &new_stack_size);
stack = stack_initializer(new_stack_size);
break;
case 2:
printf("please enter the data of the elment you would like to push in stack: \n");
scanf("%d", &new_top_data);
stack_push(stack, new_top_data);
break;
case 3:
printf("poping the top-most element in stack :\n");
stack_pop(stack);
break;
case 4:
printf("please enter the new MAX capacity of the stack for resizing: \n");
scanf("%d", &new_stack_size);
stack_resizer(&stack, new_stack_size);
break;
case 5:
peeked_element = stack_peek(stack);
break;
case 6:
stack_isempty_or_full_checker(stack);
break;
case 7:
printf("following are the Options avaliable for list manipulation:\n1.Re-Initialize stack.\n2.push a new element to the stack.\n3.pop the top element from the stack.\n4.resize stack (incremental only).\n5.peek top of stack.\n6.check if stack is empty or not.\n7.Re-print operations panel.\n8.EXIT.\n");
break;
case 8:
goto exit;
break;
case 9:
stack_printer(stack);
break;
default:
printf("operation not found!!! please check the operations list and try again.\n");
break;
}
}
exit:
return 0;
}
/*
FOR TESTING ONLY
stack = stack_initializer(5);
stack_isempty_or_full_checker(stack);
stack_push(stack, 100);
stack_push(stack, 200);
stack_push(stack, 300);
stack_push(stack, 400);
stack_push(stack, 500);
stack_push(stack, 200);
stack_isempty_or_full_checker(stack);
stack_printer(stack);
stack_pop(stack);
stack_pop(stack);
stack_pop(stack);
stack_pop(stack);
stack_pop(stack);
stack_isempty_or_full_checker(stack);
stack_printer(stack);
stack_peek(stack);
stack_resizer(&stack, 10);
*/