-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_ptr.c
143 lines (123 loc) · 2.86 KB
/
list_ptr.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
/* Titouan Teyssier - 11/27/16 */
#include <stdlib.h>
#include "list_ptr.h"
int areEqual (t_pos d1, t_pos d2) {
return (d1.line == d2.line && d1.col == d2.col);
}
typedef struct element {
struct element * next;
struct element * prev;
t_pos data;
} t_element;
t_element * gFlag;
t_element * gCurrElt;
int gNbElt;
void listPtr_init (void) {
gFlag = malloc (sizeof(t_element));
gFlag -> next = gFlag -> prev = gCurrElt = gFlag;
gNbElt = 0;
}
int listPtr_isEmpty (void) {
return gFlag -> next == gFlag;
}
int listPtr_isOut (void) {
return gCurrElt == gFlag;
}
void listPtr_move2end (void) {
gCurrElt = gFlag -> prev;
}
void listPtr_move2head (void) {
gCurrElt = gFlag -> next;
}
void listPtr_readData (t_pos * data) {
if (gCurrElt != gFlag) {// not out of list
*data = gCurrElt -> data;
}
}
void listPtr_removeElt (void) {
t_element * tmp = NULL;
if (gCurrElt != gFlag) {// not out of list
tmp = gCurrElt;
// redo the chain
gCurrElt -> next -> prev = gCurrElt -> prev;
gCurrElt -> prev -> next = gCurrElt -> next;
// put the current element on the left
gCurrElt = gCurrElt -> prev;
free(tmp);
tmp = NULL;
}
}
void listPtr_removeList (void) {
listPtr_move2end();
while (gCurrElt != gFlag) listPtr_removeElt();
}
void listPtr_next (void) {
if (gCurrElt != gFlag) gCurrElt = gCurrElt -> next;
}
void listPtr_prev (void) {
if (gCurrElt != gFlag) gCurrElt = gCurrElt -> prev;
}
void listPtr_appendLeft (t_pos data) {
t_element * tmp = malloc (sizeof (t_element));
tmp -> data = data;
// i dont use the fact that if the list is empty then gFlag = gCurrElt because it's clearer that way.
if (listPtr_isEmpty()) {
tmp -> next = gFlag;
tmp -> prev = gFlag;
gFlag -> next = tmp;
gFlag -> prev = tmp;
gCurrElt = tmp;
}
else if (!listPtr_isOut()) {
tmp -> prev = gCurrElt -> prev;
tmp -> next = gCurrElt;
gCurrElt -> prev -> next = tmp;
gCurrElt -> prev = tmp;
gCurrElt = tmp;
}
else {
free (tmp);
tmp = NULL;
}
}
void listPtr_appendRight (t_pos data) {
t_element * tmp = malloc (sizeof (t_element));
tmp -> data = data;
// i dont use the fact that if the list is empty then gFlag = gCurrElt because it's clearer that way.
if (listPtr_isEmpty()) {
tmp -> next = gFlag;
tmp -> prev = gFlag;
gFlag -> next = tmp;
gFlag -> prev = tmp;
gCurrElt = tmp;
}
else if (!listPtr_isOut()) {
tmp -> prev = gCurrElt;
tmp -> next = gCurrElt -> next;
gCurrElt -> next -> prev = tmp;
gCurrElt -> next = tmp;
gCurrElt = tmp;
}
else {
free (tmp);
tmp = NULL;
}
}
void listPtr_appendEnd (t_pos data) {
listPtr_move2end();
listPtr_appendRight(data);
}
void listPtr_appendHead (t_pos data) {
listPtr_move2head();
listPtr_appendLeft(data);
}
int listPtr_isInList (t_pos data) {
t_pos cmp;
listPtr_move2head();
while (!listPtr_isOut()) {
listPtr_readData(&cmp);
if (areEqual(cmp, data)) return 1;
listPtr_next();
}
return 0;
}