-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_duplicate.c
More file actions
62 lines (53 loc) · 1.32 KB
/
remove_duplicate.c
File metadata and controls
62 lines (53 loc) · 1.32 KB
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
#include <stdio.h>
#include <stdlib.h>
typedef struct List {
int car;
struct List *cdr;
} List_t;
List_t* makeList( size_t N, int const a[static N] ) {
size_t i;
List_t* new;
List_t* last;
for ( i = N-1, last = NULL ; i < N ; i = i - 1, last = new ) {
new = malloc( sizeof( List_t ) );
printf("i = %zu\n", i);
// printf("adding %d\n", a[i]);
new->car = a[i];
new->cdr = last;
}
return new;
}
void freeList( List_t* l ) {
for ( ; l != NULL ; ) {
List_t* toDelete = l;
l = l->cdr;
free( toDelete );
}
}
void printList( List_t* l, char* name ) {
printf( "%s:", name );
for ( ; l != NULL ; l = l->cdr ) {
printf( " %d", l->car );
}
printf( "\n" );
}
void remove_duplicates( List_t* l ) {
for ( List_t* cur = l ; cur != NULL ; cur = cur->cdr ) {
for ( List_t* runner = cur ; runner->cdr != NULL ; runner = runner->cdr ) {
if ( cur->car == runner->cdr->car ) {
List_t* new_cdr = runner->cdr->cdr;
free( runner->cdr );
runner->cdr = new_cdr;
}
}
}
}
int main ( int argc, char* argv[argc+1] ) {
int const a[8] = { 1, 1, 129, 2, 3, 3, 129, 2301, };
List_t* a_list = makeList( 8, a );
printList( a_list, "a" );
remove_duplicates( a_list );
printList( a_list, "a no duplicates" );
freeList( a_list );
return EXIT_SUCCESS;
}