-
Notifications
You must be signed in to change notification settings - Fork 0
/
dll.c
76 lines (59 loc) · 1.59 KB
/
dll.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
/**
* Project: IFJ21 imperative language compiler
*
* Brief: Double Linked List used in Code Generator for IFJ21 compiler
*
* Author: Stepan Bakaj <xbakaj00>
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "dll.h"
#include "error.h"
void DLL_Init( DLList *list ) {
list->firstElement = NULL;
list->lastElement = NULL;
}
void DLL_Dispose( DLList *list ) {
DLLElementPtr tmp = list->firstElement;
while (list->firstElement != NULL){
list->firstElement = list->firstElement->nextElement;
free(tmp->data);
tmp->data = NULL;
free(tmp);
tmp = NULL;
tmp = list->firstElement;
}
list->lastElement = NULL;
}
void DLL_InsertLast( DLList *list, char *data , unsigned size) {
DLLElementPtr tmp = (DLLElementPtr)malloc(sizeof(struct DLLElement));
if (tmp != NULL){
tmp->data = (char *) malloc(size);
if (tmp->data != NULL) {
strcpy(tmp->data, data);
} else {
free(tmp);
err = E_INTERNAL;
return;
}
tmp->nextElement = NULL;
tmp->previousElement = list->lastElement;
if (list->firstElement == NULL){
list->firstElement = tmp;
} else {
list->lastElement->nextElement = tmp;
}
list->lastElement = tmp;
} else {
err = E_INTERNAL;
}
}
void DLL_PrintAll( DLList *list ){
DLLElementPtr tmp = list->firstElement;
while (tmp != NULL){
printf("%s", tmp->data);
tmp = tmp->nextElement;
}
}