-
Notifications
You must be signed in to change notification settings - Fork 0
/
p07.c
61 lines (47 loc) · 1001 Bytes
/
p07.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
#include <stdio.h>
#include <stdlib.h>
#include "node.h"
#include "test.h"
void SortedInsert(struct node **head, struct node *new_node);
void InsertSort(struct node **head);
int
main(void)
{
struct node **testcases = get_testcases();
int i;
for (i = 0; i < 7; i++) {
dump(testcases[i]);
InsertSort(&testcases[i]);
dump(testcases[i]);
(void)puts("");
}
return (EXIT_SUCCESS);
}
void
SortedInsert(struct node **head, struct node *new_node)
{
struct node **node;
for (node = head; *node != NULL; node = &((*node)->next)) {
if (new_node->data <= (*node)->data )
break;
}
new_node->next = *node;
*node = new_node;
}
void
InsertSort(struct node **head)
{
struct node *new_head = NULL;
struct node *new_node;
struct node *node, *snode;
node = *head;
while (node != NULL) {
new_node = malloc(sizeof(struct node));
new_node->data = node->data;
SortedInsert(&new_head, new_node);
snode = node;
node = node->next;
free(snode);
}
*head = new_head;
}