-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.c
68 lines (52 loc) · 1.59 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
#include "List.h"
#include "Game.h"
#include "MainAux.h"
#include <stdio.h>
#include <stdlib.h>
Node* insert(Node* curr_node, int command_code, int* command_data, cell cell_data) {
/* 1. allocate new node */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
if (new_node == NULL){
memory_error("malloc");
}
/* 2. put in the data */
new_node->command_code = command_code;
new_node->command_data[0] = command_data[0];
new_node->command_data[1] = command_data[1];
new_node->command_data[2] = command_data[2];
new_node->cell_data = cell_data;
new_node->next = NULL;
new_node->prev = NULL;
/*3. check if the given prev_node is NULL */
if (curr_node == NULL) {
return new_node;
}
/* 4. Make next of new node as next of prev_node */
new_node->next = NULL;
/* 5. Make the next of prev_node as new_node */
curr_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = curr_node;
/* 7. Change previous of new_node's next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
*/
return new_node;
}
void clear_list(Node* head){
if(head == NULL)
return;
while (head->prev!=NULL){
head = head->prev;
free(head->next);
}
free(head);
}
void init_start_list(){
cell cell_data = {0,0,0,0,NULL};
int command_data1 [3];
int command_data2 [3];
start_list = insert(start_list,-2, command_data1, cell_data);
end_list = insert(start_list,2, command_data2, cell_data);
current_move = start_list;
}