root/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
#include "List.h"

List* List_create() {
    List* this = malloc(sizeof(List));
    this->first = NULL;
    this->last = NULL;
    return this;
}

void List_destroy(List* this) {
    ListNode* it = this->first;
    while (it) {
        ListNode* next = it->next;
        ListNode_destroy(it);
        it = next;
    }
    free(this);
}

void List_pushBack(List* this, void* data) {
    ListNode* newNode = ListNode_create(this->last, data);
    if (!this->first) {
        this->first = newNode;
    }
    this->last = newNode;
}

void List_pushFront(List* this, void* data) {
    ListNode* newNode = ListNode_create(NULL, data);
    newNode->next = this->first;
    if (!this->last) {
        this->last = newNode;
    }
    this->first = newNode;
}

void List_eraseByData(List* this, void* data) {
    ListNode* it = this->first;
    while (it) {
        if (it->data == data) {
            if (it->previous) {
                it->previous->next = it->next;
            }
            if (it->next) {
                it->next->previous = it->previous;
            }
            if (it == this->first) {
                this->first = it->next;
            }
            if (it == this->last) {
                this->last = it->previous;
            }
            ListNode_destroy(it);
            return;
        }
        it = it->next;
    }
}

int List_getIndexForData(List* this, void* data) {
    int i = 0;
    ListNode* it = this->first;
    while (it) {
        if (it->data == data) {
            return i;
        }
        ++i;
        it = it->next;
    }
    return -1;
}

ListNode* List_getNodeForData(List* this, void* data) {
    ListNode* it = this->first;
    while (it) {
        if (it->data == data) {
            return it;
        }
        it = it->next;
    }
    return NULL;
}


ListNode* ListNode_create(ListNode* previous, void* data) {
    ListNode* this = malloc(sizeof(ListNode));
    this->previous = previous;
    this->next = NULL;
    this->data = data;
    if (previous) {
        previous->next = this;
    }
    return this;
}

void ListNode_destroy(ListNode* this) {
    free(this);
}

int List_length(List* this) {
    int n = 0;
    for (ListNode* it = this->first; it; it = it->next) {
    ++n;
    }
    return n;
}
void List_qSort(List* this, int (*compare)(void* a, void* b)) {
    ListNode* a = this->first;
    ListNode* b = this->last;
    if (a && b) {
        List_qSortIt(this, compare, a, b);
    }
}

void List_qSortIt(List* this, int (*compare)(void* a, void* b), ListNode* start, ListNode* end) {
    if (start == end) {
        return;
    }
    ListNode* it = start;
    ListNode* jt = end;
    void* pivot = end->data;

    while (it != jt) {
        while (1 != compare(it->data, pivot) && it != jt) {
            it = it->next;
        }

        while (-1 != compare(jt->data, pivot) && jt != it) {
            jt = jt->previous;
        }

        if (it != jt) {
            void* swapCache = it->data;
            it->data = jt->data;
            jt->data = swapCache;
        }
    }

    if (1 == compare(it->data, pivot)) {
        void* swapCache = it->data;
        it->data = end->data;
        end->data = swapCache;
    }

    if (it->previous && start != it) {
        List_qSortIt(this, compare, start, it->previous);
    }
    if (it->next && end != it) {
        List_qSortIt(this, compare, it->next, end);
    }
}