-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
executable file
·361 lines (287 loc) · 8.42 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Data {
char *record;
int type;
};
struct Stack {
struct Data *data;
struct Stack *next;
struct Stack *prev;
};
/*Return data structure (especially for Pop() function)*/
struct PopResult {
struct Stack *pointer;
struct Data *data;
};
/*Pseudo-constructor (Creates a new stack with one item)*/
struct Stack *CreateStack(char *_firstElData, int _firstElType) {
struct Stack *stack = (struct Stack*)malloc(sizeof(struct Stack*));
struct Data *d = (struct Data*)malloc(sizeof(struct Data*));
d->record = _firstElData;
d->type = _firstElType;
stack->data = d;
stack->prev = NULL;
stack->next = NULL;
return stack;
}
/*Adds a new element to stack*/
struct Stack *Push(struct Stack *_stack, char *_inputData, int _inputType) {
/*in case when the stack would has a one member*/
if(_stack != NULL) {
struct Stack *newItem = (struct Stack*)malloc(sizeof(struct Stack*));
struct Data *d = (struct Data*)malloc(sizeof(struct Data*));
//d->data = (char*)calloc(sizeof(char), 256);
d->record = _inputData;
d->type = _inputType;
newItem->data = d; /*set our data*/
newItem->prev = _stack;
newItem->next = NULL;
_stack->next = newItem;
return newItem;
}
/*in case when the stack is empty*/
else {
_stack = CreateStack(_inputData, _inputType);
return _stack;
}
}
/*Gets last input element from Stack without its removing*/
struct Stack *Peek(struct Stack *_stack) {
if(_stack != NULL) {
while(_stack->next != NULL) {
_stack = _stack->next;
printf("!\n");
}
return _stack;
}
else
return NULL;
}
/*Gets top stack's member and removes it*/
struct PopResult *Pop(struct Stack *_stack) {
if(_stack == NULL)
return NULL;
if(_stack->prev != NULL) {
_stack = Peek(_stack); /*get the first stack's item*/
struct PopResult *result = (struct PopResult*)malloc(sizeof(struct PopResult*));
result = NULL;
printf("%s\n", _stack->data->record);
result->data = _stack->data;
/*result->data->record = _stack->data->record;
result->data->type = _stack->data->type;
*/
/*delete a connection and free memory*/
_stack = _stack->prev;
struct Stack *tmp = _stack->next;
_stack->next = NULL;
//free(tmp->data);
//free(tmp);
//tmp = NULL;
_stack->next = NULL;
result->pointer = Peek(_stack);
//_stack = NULL;
return result;
}
else {
struct PopResult *result = (struct PopResult*)malloc(sizeof(struct PopResult*));
result = NULL;
result->data = _stack->data;
/*
result->data->record = _stack->data->record;
result->data->type = _stack->data->type;
*/
result->pointer = NULL;
//free(_stack);
//_stack = NULL;
return result;
}
}
/*
struct PopResult *Pop(struct Stack *_stack) {
if(_stack == NULL)
return NULL;
if(_stack->prev != NULL) {
_stack = Peek(_stack); /*get the first stack's item
struct PopResult *result = (struct PopResult*)malloc(sizeof(struct PopResult*));
result->data = _stack->data;
/*delete a connection and free memory
_stack = _stack->prev;
struct Stack *tmp = _stack->next;
_stack->next = NULL;
free(tmp);
result->pointer = Peek(_stack);
return result;
}
else {
struct PopResult *result = (struct PopResult*)malloc(sizeof(struct PopResult*));
result->data = _stack->data;
result->pointer = NULL;
free(_stack);
return result;
}
}
*/
/*Deletes all items and frees memory*/
struct Stack* Clear(struct Stack *_stack) {
if(_stack != NULL) {
_stack = Peek(_stack);
struct Stack *tmp;
while(_stack->prev != NULL) {
tmp = _stack;
_stack = _stack->prev;
free(tmp);
}
/*frees last item*/
tmp = _stack;
free(tmp);
}
else
free(_stack);
_stack = NULL;
return _stack;
}
/*Prints elements of stack(Order: from top to low item)*/
void PrintStack(struct Stack *_stack) {
_stack = Peek(_stack); //get the first stack's element
if (_stack != NULL) {
while(_stack->prev != NULL) {
printf("%s --- %d, type is %d\n", _stack->data->record, strlen(_stack->data->record), _stack->data->type);
_stack = _stack->prev;
}
printf("%s --- %d, type is %d\n", _stack->data->record, strlen(_stack->data->record), _stack->data->type);
}
else
printf("List is empty!\n");
}
/*Gets a count of stack's items*/
int GetItemsCount(struct Stack *_stack) {
_stack = Peek(_stack); /*get the first stack's element*/
int count = 0;
if (_stack != NULL) {
while(_stack->prev != NULL) {
_stack = _stack->prev;
count++;
}
count++;
}
_stack = Peek(_stack);
return count;
}
/*Gets the last element of regarded stack*/
struct Stack *Bottom(struct Stack *_stack) {
if(_stack != NULL) {
while(_stack->prev != NULL)
_stack = _stack->prev;
return _stack;
}
else
return NULL;
}
/*Provides movements among stack's items*/
struct Stack *MoveTo(struct Stack *_stack, unsigned int _pos) {
/*in case when the _stack equals NULL*/
if(_stack == NULL)
return NULL;
/*in case when the parameter _pos more than length of the _stack*/
if(GetItemsCount(_stack) < _pos)
return _stack;
/*in case when the _pos is a negative*/
if(_pos < 0)
return _stack;
_stack = Bottom(_stack);
int counter = 0;
while(counter != _pos) {
_stack = _stack->next;
counter++;
}
return _stack;
}
/*Pushes a new element on concrete position*/
struct Stack* PushInside(struct Stack *_stack, unsigned int _pos, char *_inputData, int _inputType) {
/*in case when the _stack equals NULL*/
if(_stack == NULL)
return NULL;
/*in case when the parameter _pos more than length of the _stack*/
if(GetItemsCount(_stack) < _pos)
return _stack;
_stack = MoveTo(_stack, _pos);
struct Stack *newItem = (struct Stack*)malloc(sizeof(struct Stack*));
struct Data *d = (struct Data*)malloc(sizeof(struct Data*));
d->record = _inputData;
d->type = _inputType;
newItem->data = d;
/*if required position belongs to the last element*/
if(_pos == 0) {
newItem->next = _stack;
newItem->prev = NULL;
_stack->prev = newItem;
return newItem;
}
/*if required position belongs to the first element*/
if(_pos == GetItemsCount(_stack) - 1) {
newItem->prev = _stack;
newItem->next = NULL;
_stack->next = newItem;
return newItem;
}
/*in other cases (1 --- 2(between) --- 3)*/
struct Stack *tmp = _stack->next;
//1---2
_stack->next = newItem;
newItem->prev = _stack;
//2---3
tmp->prev = newItem;
newItem->next = tmp;
return newItem;
}
/*Gets the first position of required data*/
int FindValue(struct Stack *_stack, char *_searchData) {
_stack = Bottom(_stack);
/*in case when the _stack is empty*/
if(_stack == NULL)
return -1;
int pos = 0;
while(_stack != NULL) {
if(strcmp(_stack->data->record,_searchData) == 0)
return pos;
pos++;
_stack = _stack->next;
}
/*in case when there is no any matches*/
return -2;
}
/*Removes a concrete item via its position number*/
struct Stack* RemoveAt(struct Stack *_stack, int _pos) {
/*in case when the parameter _pos more than length of the _stack*/
if(GetItemsCount(_stack) < _pos)
return _stack;
_stack = MoveTo(_stack, _pos);
struct Stack* tmp;
tmp = _stack;
/*Consider a case when we'd like to remove the bottom item of the _stack*/
if(_pos == 0) {
_stack = _stack->next;
_stack->prev = NULL;
tmp->next = NULL;
free(tmp);
return _stack;
}
/*Consider a case when we'd like to remove a top item of the _stack*/
if(_pos == GetItemsCount(_stack) - 1) {
_stack = _stack->prev;
_stack->next = NULL;
tmp->prev = NULL;
free(tmp);
return _stack;
}
/*in other cases*/
_stack = _stack->prev;
_stack->next = tmp->next;
tmp->next->prev = _stack;
tmp->next = NULL;
tmp->prev = NULL;
free(tmp);
return _stack;
}