-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMystack.c
103 lines (81 loc) · 2.26 KB
/
Mystack.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
/**
* Projekt : Implementace prekladace imperativniho jazyka IFJ21
* @file Mystack.c
* @brief Zpracovanie vyrazov - pomocny Stack
* @author Jakub Skunda (xskund02)
*
*/
#include "MyStackTree.h"
void Stack_init(TStack *Stack){
Stack->top = NULL;
}
int Stack_push(TStack *Stack, int newItem, Token *token){
TElement *new = (TElement*)malloc(sizeof(TElement));
if (new == NULL){
return INTERNAL_ERROR;
}
new->tree = (Tree*)malloc(sizeof(Tree));
if (new->tree == NULL){
return INTERNAL_ERROR;
}
if(token != NULL){
new->tree->Data = (Token*)malloc(sizeof(Token));
if (new->tree->Data == NULL){
return INTERNAL_ERROR;
}
*new->tree->Data = *token;
}
else{
new->tree->Data = NULL;
}
TElement *tmp = Stack->top;
new->Item = newItem;
new->next = tmp;
Stack->top = new;
return 0;
}
void Stack_Push_Element(TStack *Stack, TElement *Element){
TElement *tmp = Stack->top;
Stack->top = Element;
Stack->top->next = tmp;
}
TElement *stack_pop_nofree(TStack *stack){
// kontrola ci je stack prazdny
if(stack->top == NULL) return NULL;
TElement *tmp = stack->top; // do tmp ulozim top
stack->top = tmp->next; // novy top bude tmp->next
//tmp->next = NULL; // z tmp zrusis odkaz na dalsi prvok
return tmp; // vratis ukazatel na tmp
}
void Stack_pop(TStack *Stack){
TElement *tmp = Stack->top;
Stack->top = Stack->top->next;
//tmp->next = NULL;
}
int Stack_first_term(TStack *stack){
for (TElement *tmp = stack->top; tmp != NULL; tmp = tmp->next){
if (tmp->Item < NOTERM){
return tmp->Item;
}
}
return -1;
}
void treeInit(Tree **tree){
*tree = NULL;
}
TElement Stack_top(TStack *stack){
return *stack->top;
}
void freeExprTree(Tree *exprTree){
if (exprTree->Data->type == Integer || exprTree->Data->type == String || exprTree->Data->type == Number){
free(exprTree->Data);
free(exprTree);
}
else if (exprTree->Data->type == Plus){
freeExprTree(exprTree->attr.binary.right);
freeExprTree(exprTree->attr.binary.left);
}
else if (exprTree->Data->type == Sizeof){
freeExprTree(exprTree->attr.unary.child);
}
}