-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.c
210 lines (191 loc) · 4.14 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// gcc -g -O3 -Wall -fPIC -c list.c && ar -rcs liblist.a list.o && rm list.o
// list.c
// adjacencylist
//
// Created by Sam Goldman on 6/21/11.
// Copyright 2011 Sam Goldman. All rights reserved.
//
#include "list.h"
#include <stdlib.h>
Node *node_create(void *data);
List *list_create(node_data_free_callback_t node_data_free_callback,int (*cmp)(const void *a, const void *b)) {
List *list = malloc(sizeof(List));
list->head = NULL;
list->count = 0;
list->node_data_free_callback = node_data_free_callback;
list->cmp = cmp;
return list;
}
Node *node_create(void *data) {
Node *node = malloc(sizeof(Node));
node->data = data;
node->next = NULL;
return node;
}
void list_add_data(List *list, void *data) {
Node *node = node_create(data);
node->next = list->head;
list->head = node;
list->count++;
}
void list_add_data_sorted(List *list, void *data) {
Node *node = node_create(data);
Node *n = list->head;
Node *prev_n = NULL;
while (n && list->cmp(n->data, data) < 0) {
prev_n = n;
n = n->next;
}
node->next = n;
if (!prev_n) {
list->head = node;
}
else {
prev_n->next = node;
}
list->count++;
}
void list_remove_data(List *list, void *data) {
Node *n = list->head;
Node *prev_n = NULL;
while (n) {
if (!list->cmp(n->data, data)) {
if (!prev_n) {
list->head = n->next;
}
else {
prev_n->next = n->next;
}
list->node_data_free_callback(n->data);
list->count--;
free(n);
break;
}
prev_n = n;
n = n->next;
}
}
int list_pop_front(List *list){
if (!list || !list->count) return -1;
Node *n = list->head;
list->head = n->next ? n->next : NULL;
list->node_data_free_callback(n->data);
list->count--;
return list->count;
}
void *shift(List *list){
if (!list || !list->count) return NULL;
Node *n = list->head;
list->head = n->next ? n->next : NULL;
list->count--;
return n->data;
}
int list_empty(List *list) {
if (!list) return -1;
return !list->count;
}
void list_free(List *list) {
Node *n = list->head;
while (n) {
Node *next_n = n->next;
list->node_data_free_callback(n->data);
free(n);
n = next_n;
}
free (list);
}
/*
ListNode *mergeSortedList(ListNode *L1, ListNode *L2)
{
ListNode dummy(-1), *p1 = &dummy, *p2 = L2; //L1的辅助头结点dummy,因为可能在头部插入
dummy.next = L1;
while(p1->next != NULL && p2 != NULL) //停止条件,也包括了判断两个链表是否为空
{
if(p1->next->value >= p2->value)
{
L2 = p2->next;
p2->next = p1->next;
p1->next = p2;
p1 = p2;
p2 = L2;
}
else
{
p1 = p1->next;
}
}
if(p1->next == NULL) //L2可能还有未处理的结点,直接加在L1尾部即可
{
p1->next = p2;
}
return dummy.next;
}
List *listMergeSort(List *head){
if(head == NULL || head->next == NULL) //链表为空,或者只有一个结点,直接返回
return head;
List *slow = head, *fast = head;
while(fast->next != NULL && fast->next->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
List *leftHead = head, *rightHead = slow->next;
slow->next = NULL; //需要把左半链表的尾结点的next赋空值,所以用一个变量来记录右半链表的头
leftHead = listMergeSort(leftHead);
rightHead = listMergeSort(rightHead);
return mergeSortedList(leftHead, rightHead);
}
*/
void list_sort(List *list) {
Node *p, *q, *e, *head, *tail;
int insize, nmerges, psize, qsize, i;
if (!list || !list->head)
return;
head = list->head;
insize = 1;
while (1) {
p = head;
head = NULL;
tail = NULL;
nmerges = 0;
while (p) {
nmerges++;
q = p;
psize = 0;
for (i = 0; i < insize; i++) {
psize++;
q = q->next;
if (!q) break;
}
qsize = insize;
while (psize > 0 || (qsize > 0 && q)) {
if (psize == 0) {
e = q; q = q->next; qsize--;
}
else if (qsize == 0 || !q) {
e = p; p = p->next; psize--;
}
else if (list->cmp(p->data, q->data) <= 0) {
e = p; p = p->next; psize--;
}
else {
e = q; q = q->next; qsize--;
}
if (tail) {
tail->next = e;
}
else {
head = e;
}
tail = e;
}
p = q;
}
tail->next = NULL;
if (nmerges <= 1) {
list->head = head;
return;
}
insize *= 2;
}
}