-
Notifications
You must be signed in to change notification settings - Fork 0
/
p10.c
84 lines (67 loc) · 1.49 KB
/
p10.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
#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);
void RemoveDuplicates(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]);
RemoveDuplicates(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, *prev_node;
node = *head;
while (node != NULL) {
new_node = malloc(sizeof(struct node));
new_node->data = node->data;
SortedInsert(&new_head, new_node);
prev_node = node;
node = node->next;
free(prev_node);
}
*head = new_head;
}
void RemoveDuplicates(struct node *head)
{
struct node *node, *nextnode, *snextnode;
if (head == NULL || head->next == NULL)
return;
node = head;
nextnode = node->next;
while (node != NULL) {
nextnode = node->next;
while (nextnode != NULL && nextnode->data == node->data) {
snextnode = nextnode;
nextnode = nextnode->next;
free(snextnode);
}
node->next = nextnode;
node = node->next;
}
}