-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
265 lines (219 loc) · 5.96 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* This file declares a linked list object, used for dynamically
* growing lists of objects.
*/
#include "list.h"
#include "network.h"
#include "logic.h"
#include "structs.h"
struct llist * createList()
{
struct llist * l = malloc(sizeof(struct llist)*1);
l->first = NULL;
return l;
}
struct lnode * getLastNode(struct llist * l, struct lnode * n)
{
if (n!=NULL){
if (n->next==NULL){
return n; //this is the last node
} else {
return getLastNode(l, n->next); //return last of the list
}
} else {
if (l->first!=NULL){
return getLastNode(l, l->first); //return last of the list
} else {
return NULL; //list is empty
}
}
}
void deleteUserNode(struct llist * l, char * key)
{
deleteUserNodeEngine(key, l->first, NULL, l); //key, current, previous, linked list
}
void deleteUserNodeEngine(char * key, struct lnode * n, struct lnode * previous, struct llist * list)
{
if(areEqual(n->d.u->key, key)){ // if this is the key to be deleted, delete it
if(previous==NULL){ // this node is the first of the list
if(n->next==NULL){ //there's no next node
list->first = NULL; //first node of linked list does not exist
free(n->d.u); //free user structure
free(n); //free entire node
} else {
list->first = n->next; //first node of linked list does not exist
free(n->d.u); //free user structure
free(n); //free entire node
//there is next node. This next node will become the new first node
}
} else {
if(n->next==NULL){
//this node is alone. Just free it and remove reference.
previous->next = NULL;
free(n->d.u); //free user structure
free(n); //free entire node
} else {
previous->next = n->next;
free(n->d.u); //free user structure
free(n); //free entire node
}
}
} else { // if this is not the right node, go ahead
if (n->next!=NULL) {
deleteUserNodeEngine(key, n->next, n, list);
}
}
}
struct user * getUserFromKey(struct llist * l, char * key)
{
if (l->first==NULL){
return NULL;
} else {
return getUserFromKeyEngine(key, l->first);
}
}
struct user * getUserFromKeyEngine(char * key, struct lnode * n)
{
if (n->d.u!=NULL){
//perform search
if(areEqual(n->d.u->key,key)){
return n->d.u; //this is the right node
} else {
if (n->next!=NULL){
return getUserFromKeyEngine(key, n->next); //search in the next
} else {
return NULL; //not found here
}
}
} else {
return NULL;
}
}
void appendUserNode(struct llist * l, struct user * u1)
{
//create node
struct lnode * node = malloc(sizeof(struct lnode)*1);
node->d.u = u1;
node->next = NULL;
//append node to last node of the list
struct lnode * last = getLastNode(l, NULL);
if (last==NULL){ //if list is empty
l->first = node;
} else {
last->next = node;
}
}
//--------------------------------------GAME LIST
void appendGameNode(struct llist * l, struct game * g1)
{
//setting the first move always to white
g1->status = 1;
/* TODO: INTERNAL BOARD?
//loading the white empty board
g1->b = malloc(sizeof(struct board)*1);
setAllBoardEmpty(g1->b); //everything 0
prepareBoard(g1->b); // preparing with right pieces
*/
struct board * currentboard = malloc(sizeof(struct board)*1); //used for server side validation
setAllBoardEmpty(currentboard);
prepareBoard(currentboard);
g1->currentboard = currentboard;
g1->lastmove = NULL;
//create node
struct lnode * node = malloc(sizeof(struct lnode)*1);
node->d.g = g1;
node->next = NULL;
//append node to last node of the list
struct lnode * last = getLastNode(l, NULL);
if (last==NULL){ //if list is empty
l->first = node;
} else {
last->next = node;
}
}
struct game * getGameFromPlayer(struct llist * l, char * key)
{
if (l->first==NULL){
return NULL;
} else {
return getGameFromPlayerEngine(key, l->first);
}
}
struct game * getGameFromPlayerEngine(char * key, struct lnode * n)
{
if (n->d.g!=NULL){
//perform search
if(areEqual(n->d.g->white,key) || areEqual(n->d.g->black,key)){
return n->d.g; //this is the right node
} else {
if (n->next!=NULL){
return getGameFromPlayerEngine(key, n->next); //search in the next
} else {
return NULL; //not found here
}
}
} else {
return NULL;
}
}
struct game * getGameFromKey(struct llist * l, char * key)
{
if (l->first==NULL){
return NULL;
} else {
return getGameFromKeyEngine(key, l->first);
}
}
struct game * getGameFromKeyEngine(char * key, struct lnode * n)
{
if (n->d.g!=NULL){
//perform search
if(areEqual(n->d.g->key,key)){
return n->d.g; //this is the right node
} else {
if (n->next!=NULL){
return getGameFromKeyEngine(key, n->next); //search in the next
} else {
return NULL; //not found here
}
}
} else {
return NULL;
}
}
void deleteGameNode(struct llist * l, char * key)
{
deleteGameNodeEngine(key, l->first, NULL, l); //key, current, previous, linked list
}
void deleteGameNodeEngine(char * key, struct lnode * n, struct lnode * previous, struct llist * list)
{
if(areEqual(n->d.g->key, key)){ // if this is the key to be deleted, delete it
if(previous==NULL){ // this node is the first of the list
if(n->next==NULL){ //there's no next node
list->first = NULL; //first node of linked list does not exist
free(n->d.g); //free user structure
free(n); //free entire node
} else {
list->first = n->next; //first node of linked list does not exist
free(n->d.g); //free user structure
free(n); //free entire node
//there is next node. This next node will become the new first node
}
} else {
if(n->next==NULL){
//this node is alone. Just free it and remove reference.
previous->next = NULL;
free(n->d.g); //free user structure
free(n); //free entire node
} else {
previous->next = n->next;
free(n->d.g); //free user structure
free(n); //free entire node
}
}
} else { // if this is not the right node, go ahead
if (n->next!=NULL) {
deleteGameNodeEngine(key, n->next, n, list);
}
}
}