-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
142 lines (122 loc) · 3.09 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
typedef struct Node *nodePtr;
typedef void *pointer;
struct List {
pointer identifier;
nodePtr start;
nodePtr current;
};
struct Node {
nodePtr right, left;
pointer data;
};
/***Private functions***/
int l_existsNode(nodePtr node) {
return node != NULL;
}
int l_isFirst(nodePtr target) {
return target->left == NULL;
}
int l_isEmpty(List list) {
return list->start == NULL;
}
nodePtr l_createNode() {
nodePtr node = (nodePtr) malloc(sizeof(struct Node));
if (node != NULL) {
node->right = NULL;
node->left = NULL;
return node;
} else
return NULL;
}
bool listExists(List *list) {
return (*list != NULL);
}
/***Public functions***/
void listCreate(List *list, pointer identifier) {
assert(*list == NULL);
*list = (List) malloc(sizeof(struct List));
if ((*list) != NULL) {
(*list)->identifier = identifier;
(*list)->start = NULL;
(*list)->current = NULL;
}
}
pointer listGetIdentifier(List list) {
return list->identifier;
}
bool listInsert(List list, pointer data) {
assert(list != NULL);
assert(data != NULL);
nodePtr newNode = l_createNode();
if (newNode != NULL) {
newNode->data = data;
newNode->right = list->start;
newNode->left = NULL;
if (!l_isEmpty(list))
list->start->left = newNode;
list->start = newNode;
list->current = newNode;
return true;
}
return false;
}
bool listRemove(List list, pointer data) {
assert(list != NULL);
list->current = list->start;
nodePtr next = NULL;
nodePtr point = list->start;
if (point != NULL) {
do {
next = point->right;
if (point->data == data) {
if (point->left == NULL) {
//First element
list->start = list->current = point->right;
if (point->right != NULL) {
point->right->left = NULL;
}
} else {
//Others
point->left->right = point->right;
if (point->right != NULL) {
point->right->left = point->left;
}
}
free(point);
return true;
}
} while ((point = next) != NULL);
}
return false;
}
void listSetCurrentToStart(List list) {
assert(list != NULL);
list->current = list->start;
}
pointer listNext(List list) {
assert(list != NULL);
nodePtr tmp = NULL;
if (list->current != NULL) {
tmp = list->current;
list->current = list->current->right;
return tmp->data;
}
list->current = list->start;
return NULL;
}
void listDestroy(List *list) {
assert(*list != NULL);
nodePtr next = NULL;
nodePtr point = (*list)->start;
if (point != NULL) {
do {
next = point->right;
free(point);
} while ((point = next) != NULL);
}
free(*list);
}