/
v2.c
59 lines (57 loc) · 1.42 KB
/
v2.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
Status dlist_insert(DList *this, size_t index, void *data)
{
if (this == NULL || data == NULL) {
return ERR;
}
if (index == 0) {
return dlist_prepend(this, data);
} else if (index == this->size) {
return dlist_append(this, data);
} else if (0 < index && index < this->size) {
Node *new = node_create(data);
Node *target = node_at(this, index);
if (target != NULL && new != NULL) {
target->pre->nxt = new;
new->pre = target->pre;
new->nxt = target;
target->pre = new;
this->size++;
return OK;
}
}
return ERR;
}
Status dlist_prepend(DList *this, void *data)
{
Node *new = node_create(data);
if (this == NULL || data == NULL || new == NULL) {
return ERR;
}
if (this->head == NULL) {
this->head = new;
this->tail = this->head;
} else {
this->head->pre = new;
new->nxt = this->head;
this->head = new;
}
this->size++;
return OK;
}
Status dlist_append(DList *this, void *data)
{
Node *new = node_create(data);
if (this == NULL || data == NULL || new == NULL) {
return ERR;
}
if (this->tail == NULL) {
this->tail = new;
this->head = this->head;
} else {
this->tail->nxt = new;
new->pre = this->tail;
this->tail = new;
}
this->size++;
return OK;
}