-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
153 lines (125 loc) · 4.91 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
/******************************************************************************
* list.c: *
* Creates and manipulates the snake. *
* *
* Functions: *
* add_to_list(), move_tail(), end_of_tail(), first_tail(), *
* expand_tail(), dealloc_list() *
******************************************************************************/
#include "retro_snake_lib.h"
/******************************************************************************
* add_to_list: Creates and inserts nodes to the list. *
* Contains pointer to the next and previous node. *
* Returns whether memory allocation is successful or not. *
******************************************************************************/
int add_to_list(node_t ** list, char * c)
{
node_t * new_node;
node_t * last;
new_node = (node_t *) malloc(sizeof(node_t));
if (new_node == NULL) {
fprintf(stderr, "Error: malloc failed in add_to_list\n");
return 0;
}
last = *list;
new_node->val = c;
new_node->next = NULL;
if (*list == NULL) {
*list = new_node;
new_node->prev = new_node;
}
else {
while (last->next != NULL) {
new_node->prev = last;
last = last->next;
}
new_node->prev = last;
last->next = new_node;
}
return 1;
}
/******************************************************************************
* move_tail: Prepares the movement of the snake. *
* It turns the head from '@' to '*'. *
* Then it either changes the last part of the tail to ' ' *
* or to '@' if the move concludes at the very end of the tail, *
* like a dog chasing its tail . *
******************************************************************************/
void move_tail(node_t ** list, const int head_to_tail)
{
node_t *last;
last = *list;
*((*list)->val) = '*';
while (last->next != NULL)
last = last->next;
if (head_to_tail)
*(last->val) = '@';
else
*(last->val) = ' ';
while (last != *list) {
last->val = last->prev->val;
last = last->prev;
}
last->val = last->prev->val;
}
/******************************************************************************
* end_of_tail: Returns whether or not a given point *
* concurs with the end of the tail *
******************************************************************************/
int end_of_tail(node_t ** list, const char * pos)
{
node_t *last;
last = *list;
while(last->next != NULL)
last = last->next;
if (last->val == pos)
return 1;
return 0;
}
/******************************************************************************
* first_tail: Returns whether or not a given point *
* is the first node of the snake's tail. *
******************************************************************************/
int first_tail(node_t ** list, const char * pos)
{
if (pos == (*list)->next->val)
return 1;
return 0;
}
/******************************************************************************
* expand_tail: Inserts new node to the tail. *
* Returns whether memory allocation is successful or not. *
******************************************************************************/
int expand_tail(node_t ** list)
{
node_t *last;
last = *list;
*((*list)->val) = '*';
while (last->next != NULL)
last = last->next;
/* Checking whether add_to_list was successful in allocating memory. */
if(add_to_list(list, last->val))
;
else
return 0;
last = last->next;
*(last->val) = '*';
while (last != *list) {
last->val = last->prev->val;
last = last->prev;
}
last->val = last->prev->val;
return 1;
}
/******************************************************************************
* dealloc_list: Releases the memory allocated to the list (snake). *
******************************************************************************/
void dealloc_list(node_t ** list)
{
node_t * temp;
while (*list != NULL) {
temp = *list;
*list = (*list)->next;
free(temp);
}
}