-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp_stack.c
More file actions
66 lines (58 loc) · 1.63 KB
/
Copy pathexp_stack.c
File metadata and controls
66 lines (58 loc) · 1.63 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
/**
* Projekt IFJ/IAL 2019 - Překladač imperativního jazyka IFJ19
*
* @file exp_stack.c
* @brief Pomocný stack pro práci s výrazy
*
* @author Daniel Čechák (xcecha06)
*/
#include "exp_stack.h"
unsigned int deletedItems = 0;
unsigned int stackSize = 0;
// Inicialization of stack
ERROR_CODE exp_stackInit (ptrStack* stack) {
stack->top_of_stack = NULL;
int type = TOKEN_UNDEF;
Exp_element *new_element = newElement(type,false);
return exp_stackPush(stack, new_element);
}
// Checking top of the stack if the stack is empty
// if it is returning true
bool exp_stackEmpty (ptrStack* stack ) {
if (stack->top_of_stack != NULL) {
return (stack->top_of_stack->value->type == TOKEN_UNDEF);
}
else {
return 1;
}
}
//Releases uppermost element on the stack
void exp_stackPop (ptrStack* stack ) {
ptStack *tmp = NULL;
if (stack->top_of_stack != NULL) {
tmp = stack->top_of_stack;
stack->top_of_stack = stack->top_of_stack->left;
free(tmp);
stackSize--;
}
}
// Creates new memory space on stack for new element and insert data into it
ERROR_CODE exp_stackPush (ptrStack* stack, void * value) {
ptStack *tmp = malloc(sizeof(struct ptstack_structure));
if(tmp != NULL) {
tmp->value = value;
tmp->left = stack->top_of_stack;
stack->top_of_stack = tmp;
stackSize++;
return ERROR_CODE_OK;
}
return ERROR_CODE_INTERNAL;
}
//Free the whole stack one by one
bool exp_stackClear(ptrStack *stack){
while(stack->top_of_stack != NULL){
exp_stackPop(stack);
deletedItems++;
}
return true;
}