-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
84 lines (74 loc) · 1.93 KB
/
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
/**
* School project to subject IFJ (Formal Languages and Compilers)
* Compiler implementation of imperative language IFJ18
*
* Module for list data structure
*
* Author: Julius Marko Jan Zauska
* Login: xmarko17 xzausk00
*/
#include <string.h>
#include "list.h"
void InitList(tList *L) {
L->Act = NULL;
L->First = NULL;
}
void DisposeList(tList *L) {
tElemPtr actual = L->First;
tElemPtr next = NULL;
while (actual != NULL) { // iterate over list and delete elements (clean up memory)
next = actual->ptr;
free(actual);
actual = next;
}
L->Act = NULL; // there can be InitList() instead, but I rather do it this way
L->First = NULL;
}
char *AppendToList(tList *L) {
tElemPtr tElemToAddPtr = (tElemPtr) malloc(sizeof(struct tElem));
if (tElemToAddPtr == NULL) return NULL;
tElemToAddPtr->ptr = NULL;
if (L->First == NULL) {
L->First = tElemToAddPtr;
} else {
tElemPtr tmp = L->First;
while (tmp->ptr != NULL) {
tmp = tmp->ptr;
}
tmp->ptr = tElemToAddPtr;
}
return tElemToAddPtr->instruction;
}
char *PostInsert(tList *L) {
if (Active(L)) {
tElemPtr toInsert = (tElemPtr) malloc(sizeof(struct tElem));
if (toInsert == NULL) return NULL;
toInsert->ptr = L->Act->ptr;
L->Act->ptr = toInsert; // insert it after active elem.
return toInsert->instruction;
}
return NULL;
}
int Active(tList *L) {
return (L->Act == NULL) ? 0 : 1;
}
void printList(tList *L) {
tElemPtr temp = L->First;
while (temp != NULL) {
fprintf(stdout, "%s", temp->instruction);
temp = temp->ptr;
}
}
int find(tList *L, char *key) {
if (L == NULL)
return 0;
tElemPtr temp = L->First;
while (temp) {
if (strcmp(temp->instruction, key) == 0) {
L->Act = temp;
return 1;
}
temp = temp->ptr;
}
return 0;
}