-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl1_z678.cpp
184 lines (165 loc) · 4.96 KB
/
l1_z678.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
struct lnode {
int key;
lnode *next;
lnode(int k, lnode *n = nullptr) : key(k), next(n) {}
};
// 6a - print elements of a linked list with space between them
void print(lnode *n) {
while (n != nullptr) {
std::cout << n->key << " ";
n = n->next;
}
std::cout << std::endl;
}
// 6b - sum elemnts of a linked list
int sum(lnode *n) {
int sum = 0;
while (n != nullptr) {
sum += n->key;
n = n->next;
}
return sum;
}
// 6c - nth element of a linked list or 0 if n is out of range /////////////////////
int nth(lnode *n, int i) {
while (n != nullptr && i > 0) {
n = n->next;
i--;
}
return n == nullptr ? 0 : n->key;
}
// 6d - insert element at the beginning of a linked list
void insert(lnode *&n, int x) {
n = new lnode(x, n);
}
// 6e - insert after smaller elements
void insert_after_smaller(lnode *&n, int x) {
// if list is empty
if (n == nullptr) {
n = new lnode(x);
return;
}
// if x is smaller than first element
if (n->key > x) {
n = new lnode(x, n);
return;
}
// if x is bigger than first element
lnode *p = n;
while (p->next != nullptr && p->next->key < x) {
p = p->next;
}
p->next = new lnode(x, p->next); // p->next == nullptr or p->next->key >= x
}
// 6f - remove elements with key x
void remove(lnode *&n, int x) {
// if list is empty
if (n == nullptr) return;
// if first element is x
if (n->key == x) {
lnode *p = n;
n = n->next;
delete p;
return;
}
// if first element is not x
lnode *p = n;
while (p->next != nullptr) {
if (p->next->key == x) { // if next element is x
lnode *q = p->next; // temporary pointer to next element
p->next = p->next->next;// overwrite next element
delete q; // delete temporary pointer
} else {
p = p->next;
}
}
}
// 6g - remove elements unsatisfying a condition
void filter(lnode *&n, bool (*cond)(int)) {
// if list is empty
if (n == nullptr) return;
// go through list and remove elements unsatisfying a condition
lnode *p = n;
while (p->next != nullptr) {
if (!cond(p->next->key)) { // if next element unsatisfies a condition
lnode *q = p->next; // temporary pointer to next element
p->next = p->next->next;// overwrite next element
delete q; // delete temporary pointer
} else {
p = p->next;
}
}
}
// 6h - destroy a linked list
void destroy(lnode *&n) {
while (n != nullptr) {
lnode *p = n;
n = n->next;
delete p;
}
}
/////////////////////////
// 7 - reverse without new and delete
void reverse(lnode *&n) {
lnode *p = nullptr;
while (n != nullptr) {
lnode *q = n; // temporary save list (first element)
n = n->next; // move list to next element
q->next = p; // first element of list points to first element of reversed list
p = q; //
}
n = p;
}
static int comparisons = 0;
/////////////////////////
// 8 - merge sorted lists, amount of comparisons <= length (a+b)
lnode *merge(lnode *a, lnode *b) {
if (a == nullptr)
return b;
if (b == nullptr)
return a;
++comparisons;
// max 1 comparison for each call
// a != nullptr && b != nullptr
if (a->key < b->key; ++comparisons) {
a->next = merge(a->next, b); // a is smaller, NEW a->next is bigger (from a or b)
return a; // return new a (with new a->next)
}
else {
b->next = merge(a, b->next);
return b; // return new b (with new b->next)
}
}
int main() {
lnode *list = new lnode(1, new lnode(2, new lnode(3, new lnode(4, new lnode(5)))));
print(list);
std::cout << "6b sum\t" << sum(list) << std::endl;
std::cout << "6c nth\t" << nth(list, 3) << std::endl;
std::cout << "6d insert 0\t";
insert(list, 0);
print(list);
std::cout << "6e ins after smaller 3\t";
insert_after_smaller(list, 3);
print(list);
std::cout << "6f remove 3\t";
remove(list, 3);
print(list);
std::cout << "6g filter even\t";
filter(list, [](int x) { return x % 2 == 0; });
print(list);
std::cout << "6h destroy\t";
destroy(list);
print(list);
std::cout << "7 reverse\t";
list = new lnode(1, new lnode(2, new lnode(3, new lnode(4, new lnode(5)))));
reverse(list);
print(list);
std::cout << "8 merge\t";
//lnode *list1 = new lnode(1, new lnode(1, new lnode(2, new lnode(7, new lnode(9)))));
lnode *list1 = new lnode(1, new lnode(2, new lnode(3)));
lnode *list2 = new lnode(2, new lnode(4, new lnode(6, new lnode(8, new lnode(10)))));
lnode *list3 = merge(list1, list2);
print(list3);
std::cout << "comparisons: " << comparisons << std::endl;
}