-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindexed_linked_lists.cpp
More file actions
111 lines (92 loc) · 2.5 KB
/
indexed_linked_lists.cpp
File metadata and controls
111 lines (92 loc) · 2.5 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
#include <iostream>
using namespace std;
struct indexed_linked_lists {
// тип даних, що зберігаються
typedef int value_type;
struct linked_list {
struct node {
value_type value;
node *next;
node(value_type value) {
this->value = value;
next = nullptr;
}
};
// якщо потрібно підтримувати розмір, розкоментуйте всі його згадування
// size_t size;
node *begin, *end;
linked_list() {
// size = 0;
begin = nullptr;
end = nullptr;
}
void push_back(value_type value) {
node *node_new = new node(value);
if (empty()) {
begin = node_new;
} else {
end->next = node_new;
}
end = node_new;
// size++;
}
bool empty() {
return begin == nullptr;
}
~linked_list() {
node *i = begin;
while (i != nullptr) {
node *s = i;
i = i->next;
delete s;
}
}
};
size_t size;
linked_list* lists;
indexed_linked_lists(size_t size) {
lists = new linked_list[size];
}
linked_list& operator[] (size_t index) {
return lists[index];
}
~indexed_linked_lists() {
delete[] lists;
}
};
int main() {
// будемо зберігати числа з різними залишками від 10
// у різних зв'язаних списках
indexed_linked_lists storage(10);
// заповнюємо
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r;
cin >> r;
storage[r % 10].push_back(r);
}
// шукаємо
int m;
cin >> m;
for (int i = 0; i < n; i++) {
int r;
cin >> r;
indexed_linked_lists::linked_list& row = storage[r % 10];
if (!row.empty()) {
indexed_linked_lists::linked_list::node* it = row.begin;
for (it = row.begin; it != nullptr; it = it->next) {
if (it->value == r) {
cout << "YES" << endl;
break;
}
}
if (it == nullptr) {
cout << "NO" << endl;
}
} else {
cout << "NO" << endl;
}
}
return 0;
}