forked from tmsm1999/Rust-Compiler-Project-Compilers-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
37 lines (31 loc) · 727 Bytes
/
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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
// Create a new list
NODE newList(char* var, NODE l) {
NODE new = malloc(sizeof(struct node));
new->variable = var;
new->next = l;
return new;
}
// Add an instruction to the end of the list
void addLast(char* var, NODE *l) {
if (*l == NULL) *l = newList(var, NULL);
else {
NODE cur = *l;
while (cur != NULL && cur->next != NULL) cur = cur->next;
NODE end = malloc(sizeof(struct node));
end->variable = var;
end->next = NULL;
cur->next = end;
}
}
// Print the list
void printList(NODE l) {
if (l == NULL) {
printf("\n"); return;
}
printf("%s", l->variable);
if (l->next != NULL) printf(" ");
printList(l->next);
}