-
Notifications
You must be signed in to change notification settings - Fork 0
/
sym_linked_list.c
126 lines (103 loc) · 2.92 KB
/
sym_linked_list.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
/**
* Project: Compiler for IFJ21 language
*
* Brief: Linked list for symtables
*
* Author: Adam Kankovsky <[email protected]>
*
* Date: 12-11-2021
*/
#include "sym_linked_list.h"
#include "symtable.h"
void LL_Init( LList* list ) {
list->lastElement = NULL;
list->activeElement = NULL;
}
void LL_Dispose( LList* list ) {
struct LLElement *current = NULL;
struct LLElement *to_delete = NULL;
current = list->lastElement;
if(list->lastElement == NULL){
return;
}
while(current != NULL){
symTableDispose(&(current->root));
to_delete = current;
current = current->nextElement;
free(to_delete);
to_delete = NULL;
}
list->lastElement = NULL;
list->activeElement = NULL;
}
void LL_InsertLast( LList* list, symTree_t* root) {
struct LLElement* newElement = (struct LLElement*) malloc(sizeof(struct LLElement));
newElement->root = root;
newElement->nextElement = NULL;
if(list->lastElement == NULL){
list->lastElement = newElement;
} else {
newElement->nextElement = list->lastElement;
list->lastElement = newElement;
}
}
void LL_DeleteLast( LList* list ){
if(list->lastElement == NULL){
return;
}
if(list->lastElement == list->activeElement){
list->activeElement = NULL;
}
if(list->lastElement->nextElement == NULL){
list->lastElement = NULL;
}else{
struct LLElement *delElement = list->lastElement;
symTableDispose(&delElement->root);
list->lastElement = list->lastElement->nextElement;
free(delElement);
delElement = NULL;
}
}
symTree_t * LL_GetFirst( LList* list){
if(list->lastElement == NULL){
return NULL;
}
struct LLElement* current;
current = list->lastElement;
while(current->nextElement != NULL){
current = current->nextElement;
}
return current->root;
}
symTree_t * LL_GetLast( LList* list){
return list->lastElement->root;
}
void LL_InsertAfter( LList* list, symTree_t* root){
if(list->activeElement == NULL){
return;
}
struct LLElement* newElement = NULL;
newElement->root = root;
if(list->activeElement == list->lastElement) {
list->lastElement = newElement;
newElement->nextElement = NULL;
}else{
newElement->nextElement = list->activeElement->nextElement;
list->activeElement->nextElement = newElement;
}
}
void LL_GetValue( LList* list, symTree_t* root ){
if(list->activeElement == NULL){
return;
}
*root = *list->activeElement->root;
}
void LL_Next( LList* list){
if(list->activeElement == NULL){
return;
}
list->activeElement = list->activeElement->nextElement;
}
int LL_IsActive( LList* list){
return (list->activeElement != NULL) ? 0 : -1;
}