-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniq.c
125 lines (103 loc) · 1.94 KB
/
uniq.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct iL{
int val;
struct iL* next;
} intList;
intList *init_intList(int key)
{
intList *result = malloc(sizeof(intList));
result->val = key;
result->next = NULL;
return result;
}
intList *append_intList(intList *root, int key)
{
intList *new_root = malloc(sizeof(intList));
new_root->val = key;
new_root->next = root;
return new_root;
}
intList *insertSorted_intList(intList *root, int key)
{
intList *new = malloc(sizeof(intList));
new->val = key;
new->next = NULL;
if(root == NULL) return new;
if(root->val >= key)
{
new->next = root;
return new;
}
intList *it;
intList *temp;
for(it = root; it->next != NULL && it->next->val < key; it = it->next);
temp = it->next;
it->next = new;
new->next = temp;
return root;
}
int isContained_intList(intList *root, int key)
{
intList *it = root;
while(it != NULL)
{
if(it->val == key) return 1;
it = it->next;
}
return 0;
}
void destroy_intList(intList *root)
{
intList *it = root;
intList *temp;
while(it != NULL)
{
temp = it->next;
free(it);
it = temp;
}
}
void output_intList(intList *root)
{
intList *it = root;
printf("Output Liste: ");
while(it != NULL)
{
printf("%i -> ", it->val);
it = it->next;
}
printf("NULL\n");
}
int main(int args, char **argv)
{
char *line = NULL;
size_t len = 0;
ssize_t read;
int i;
intList *list = NULL;
if(args < 3) return EXIT_FAILURE;
FILE *src = fopen(argv[1],"r");
FILE *dest = fopen(argv[2], "w+");
while((read = getline(&line, &len, src)) != -1)
{
i = atoi(line);
printf("%i gelesen.\n", i);
if(!isContained_intList(list, i))
{
printf("%i hinzugefügt.\n", i);
list = insertSorted_intList(list, i);
fprintf(dest, "%i\n", i);
free(line);
line = NULL;
} else{
printf("%i wird nicht hinzugefügt (bereits vorhanden).\n", i);
}
}
output_intList(list);
destroy_intList(list);
fclose(dest);
fclose(src);
return 0;
}