-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.5.c
More file actions
128 lines (109 loc) · 2.68 KB
/
2.5.c
File metadata and controls
128 lines (109 loc) · 2.68 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
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
126
127
128
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node *link;
typedef struct Node {
char *word;
link next;
} node;
typedef link list;
// Создание звена
link create_node(char *word) {
link p = (link)malloc(sizeof(node));
p->word = (char*)malloc(strlen(word) + 1);
strcpy(p->word, word);
p->next = NULL;
return p;
}
// Добавление звена в конец
void append(list *lp, char *word) {
link p = create_node(word);
if (*lp == NULL) {
*lp = p;
} else {
link cur = *lp;
while (cur->next != NULL)
cur = cur->next;
cur->next = p;
}
}
// Освобождение памяти
void destruct(list ls) {
link q;
while (ls != NULL) {
q = ls;
ls = ls->next;
free(q);
}
}
// Печать списка слов через пробел
void print(list ls) {
while (ls != NULL) {
printf("%s", ls->word);
if (ls->next != NULL)
printf(" ");
ls = ls->next;
}
printf("\n");
}
// Удаление
void remove_duplicates_of_last(list *lp) {
if (*lp == NULL) return;
// Найдём последнее слово
link last = *lp;
while (last->next != NULL)
last = last->next;
char *target = last->word;
// Удаляем совпадения, кроме последнего
link cur = *lp, prev = NULL;
while (cur != NULL) {
if (cur != last && strcmp(cur->word, target) == 0) {
link temp = cur;
if (prev == NULL)
*lp = cur->next;
else
prev->next = cur->next;
cur = cur->next;
free(temp);
} else {
prev = cur;
cur = cur->next;
}
}
}
char* read_line_dynamic(void) {
size_t cap = 256;
char *buf = (char*)malloc(cap);
size_t len = 0;
int ch;
while ((ch = getchar()) != EOF && ch != '\n') {
if (len + 1 >= cap) {
size_t ncap = cap * 2;
char *tmp = (char*)realloc(buf, ncap);
buf = tmp;
cap = ncap;
}
buf[len++] = (char)ch;
}
buf[len] = '\0';
return buf;
}
int main(void) {
list head = NULL;
char *line = read_line_dynamic();
// Разбиваем строку на слова и добавляем в список
char *token = strtok(line, " \t");
while (token != NULL) {
append(&head, token);
token = strtok(NULL, " \t");
}
free(line);
if (head) {
remove_duplicates_of_last(&head);
print(head);
} else {
putchar('\n');
}
destruct(head);
return 0;
}