-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLIST.cpp
179 lines (140 loc) · 4.31 KB
/
LIST.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
#include <iostream>
#include <ranges>
#include <optional>
#include <variant>
#define T variant<int32_t, double, string>
using std::string;
using std::variant;
using std::optional;
struct NODE {
T data;
optional<NODE*> next;
};
struct LIST {
optional<NODE*> head;
};
void print(const LIST& list);
void add_front(LIST& list, const T& data);
void add_back(LIST& list, const T& data);
void add_after(LIST& list, const T& data_after, const T& data);
void delete_node(LIST& list, const T& data_delete);
void delete_match(LIST& list, const T& data_delete);
void bubble_sort(LIST& list);
void delete_list(LIST& list);
int32_t main() {
LIST list;
for(const int32_t& num: std::views::iota(0, 6)) {
add_front(list, num);
}
add_back(list, 5);
add_after(list, 2, "A");
add_after(list, 2, "B");
//delete_node(list, "World");
//delete_match(list, 5);
bubble_sort(list);
print(list);
delete_list(list);
return 0;
}
void print(const LIST& list) {
optional<NODE*> curr = list.head;
while(curr) {
std::visit([](const auto& data) { std::cout << data << ' '; }, curr.value()->data);
curr = curr.value()->next;
}
}
void add_front(LIST& list, const T& data) {
optional<NODE*> new_node = new NODE;
new_node.value()->data = data;
new_node.value()->next = list.head;
list.head = new_node;
}
void add_back(LIST& list, const T& data) {
optional<NODE*> new_node = new NODE;
new_node.value()->data = data;
if(!list.head) {
list.head = new_node;
return;
}
optional<NODE*> curr = list.head;
while(curr.value()->next) {
curr = curr.value()->next;
}
curr.value()->next = new_node;
}
void add_after(LIST& list, const T& data_after, const T& data) {
if(!list.head) {
return;
}
optional<NODE*> curr = list.head;
while(curr.value()->data != data_after && curr.value()->next) {
curr = curr.value()->next;
}
if(curr.value()->data != data_after) {
visit([](const auto& data) { std::cout << "'" << data << "' NOT FOUND!\n"; }, curr.value()->data);
return;
}
optional<NODE*> new_node = new NODE;
new_node.value()->data = data;
new_node.value()->next = curr.value()->next;
curr.value()->next = new_node;
}
void delete_node(LIST& list, const T& data_delete) {
if(!list.head) {
return;
}
if(list.head.value()->data == data_delete) {
list.head = list.head.value()->next;
return;
}
optional<NODE*> curr = list.head, prev;
while(curr.value()->data != data_delete && curr.value()->next) {
prev = curr;
curr = curr.value()->next;
}
if(curr.value()->data != data_delete) {
std::visit([](const auto& data_delete) { std::cout << "'" << data_delete << "' NOT FOUND!\n"; }, data_delete);
return;
}
(!prev) ? list.head = curr.value()->next : prev.value()->next = curr.value()->next;
delete curr.value();
curr.value() = nullptr;
}
void delete_match(LIST& list, const T& data_delete) {
if(!list.head) {
return;
}
optional<NODE*> curr = list.head, prev;
while(curr) {
if(curr.value()->data == data_delete) {
(!prev) ? list.head = curr.value()->next : prev.value()->next = curr.value()->next;
NODE* temp = curr.value();
curr = curr.value()->next;
delete temp;
temp = nullptr;
} else {
prev = curr;
curr = curr.value()->next;
}
}
}
void bubble_sort(LIST& list) {
for(optional<NODE*> curr = list.head; curr ;curr = curr.value()->next) {
for(optional<NODE*> next = curr.value()->next; next ;next = next.value()->next) {
if(curr.value()->data > next.value()->data) {
T temp = curr.value()->data;
curr.value()->data = next.value()->data;
next.value()->data = temp;
}
}
}
}
void delete_list(LIST& list) {
while(list.head) {
NODE* temp = list.head.value();
list.head = list.head.value()->next;
delete temp;
temp = nullptr;
}
}
/*EOF*/