-
Notifications
You must be signed in to change notification settings - Fork 0
/
divide.c
64 lines (59 loc) · 1.41 KB
/
divide.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
#include "list.h"
#include "dirent.h"
void fast_slow_divide(node_t **list, node_t *lists[], size_t *listsSize)
{
size_t i = 0;
int top = 0;
node_t *stack[1000] = {NULL};
stack[top] = *list;
while (top >= 0)
{
node_t *left = stack[top--];
if (left)
{
node_t *slow = left;
for (node_t *fast = left->next; fast && fast->next; fast = fast->next->next)
slow = slow->next;
node_t *right = slow->next;
slow->next = NULL;
stack[++top] = left;
stack[++top] = right;
}
else
{
lists[i++] = stack[top--];
}
}
*listsSize = i;
}
void divide_to_single(node_t **list, node_t *lists[], size_t *listsSize)
{
size_t i = 0;
for (node_t *node = *list, *next; node; node = next)
{
next = node->next;
node->next = NULL;
lists[i++] = node;
}
*listsSize = i;
}
void divide_to_sorted(node_t **list, node_t *lists[], size_t *listsSize)
{
size_t i = 0;
node_t *sorted = *list;
while (sorted)
{
node_t *iter = sorted;
while (iter && iter->next)
{
if (iter->value < iter->next->value)
iter = iter->next;
else
break;
}
lists[i++] = sorted;
sorted = iter->next;
iter->next = NULL;
}
*listsSize = i;
}