-
Notifications
You must be signed in to change notification settings - Fork 0
/
linklisty.c
191 lines (172 loc) · 3.3 KB
/
linklisty.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
#include "linklist.h"
#include <stdlib.h>
void
inity (assem * asmb, unsigned int id)
{
asmb->beginy = asmb->endy = asmb->currenty = (linklist *) 0;
asmb->idy = id;
}
void
addy (assem * asmb, linklist * lst)
{
lst->prevy = lst->forwy = (linklist *) 0;
if (!asmb->beginy)
{
asmb->beginy = lst;
asmb->endy = lst;
lst->idy = 0;
}
else
{
asmb->endy->forwy = lst;
lst->prevy = asmb->endy;
asmb->endy = lst;
lst->idy = lst->prevy->idy + 1;
}
}
void
freey (assem * asmb)
{
linklist *tmp;
asmb->currenty = asmb->beginy;
while (asmb->currenty)
{
tmp = asmb->currenty;
asmb->currenty = asmb->currenty->forwy;
free (tmp);
}
}
void
freeally (assem * asmb)
{
linklist *tmp;
asmb->currenty = asmb->beginy;
while (asmb->currenty)
{
tmp = asmb->currenty;
asmb->currenty = asmb->currenty->forwy;
free (tmp->ptr);
free (tmp);
}
}
unsigned int
numelementy (assem * asmb)
{
return (asmb->endy->idy + 1);
}
linklist *
findbyidy (assem * asmb, unsigned int id)
{
asmb->currenty = asmb->beginy;
while (asmb->currenty)
{
if (id == asmb->currenty->idy)
{
return (asmb->currenty);
}
asmb->currenty = asmb->currenty->forwy;
}
return (0);
}
int
replacey (assem * asmb, linklist * lst, unsigned int id)
{
linklist *tmp;
if (!(tmp = findbyidy (asmb, id)))
{
return (0);
}
if (asmb->beginy == asmb->endy)
{
asmb->beginy = asmb->endy = (linklist *) 0;
addy (asmb, lst);
}
else if (tmp == asmb->beginy)
{
tmp->forwy->prevy = lst;
lst->prevy = tmp->prevy;
lst->forwy = tmp->forwy;
asmb->beginy = lst;
}
else if (tmp == asmb->endy)
{
tmp->prevy->forwy = lst;
lst->prevy = tmp->prevy;
lst->forwy = tmp->forwy;
asmb->endy = lst;
}
else
{
tmp->prevy->forwy = lst;
tmp->forwy->prevy = lst;
lst->prevy = tmp->prevy;
lst->forwy = tmp->forwy;
}
lst->idy = tmp->idy;
free (tmp);
return (1);
}
int
deletey (assem * asmb, unsigned int id)
{
linklist *tmp;
if (!(tmp = findbyidy (asmb, id)))
{
return (0);
}
else if (asmb->beginy == asmb->endy)
{
asmb->beginy = asmb->endy = (linklist *) 0;
}
else if (tmp == asmb->beginy)
{
tmp->forwy->prevy = tmp->prevy;
asmb->beginy = tmp->forwy;
}
else if (tmp == asmb->endy)
{
tmp->prevy->forwy = tmp->forwy;
asmb->endy = tmp->prevy;
}
else
{
tmp->prevy->forwy = tmp->forwy;
tmp->forwy->prevy = tmp->prevy;
}
asmb->currenty = tmp->forwy;
while (asmb->currenty)
{
asmb->currenty->idy--;
asmb->currenty = asmb->currenty->forwy;
}
free (tmp);
return (1);
}
int
inserty (assem * asmb, linklist * lst, unsigned int id)
{
linklist *tmp;
if (!(tmp = findbyidy (asmb, id)))
{
return (0);
}
if ((asmb->beginy == asmb->endy) || (tmp == asmb->endy))
{
addy (asmb, lst);
return (1);
}
else
{
lst->prevy = tmp;
lst->forwy = tmp->forwy;
tmp->forwy->prevy = lst;
tmp->forwy = lst;
}
asmb->currenty = lst;
while (asmb->currenty)
{
asmb->currenty->idy = asmb->currenty->prevy->idy + 1;
asmb->currenty = asmb->currenty->forwy;
}
return (1);
}