-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.cpp
92 lines (71 loc) · 1.94 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
#include "list.h"
List::List() : head(nullptr), count(0) {}
List::List(const List &other) : head(nullptr), count(0) {
for (auto node = other.head.get(); node; node = node->next.get()) {
append(node->data);
}
}
List::~List() = default;
bool List::empty() const { return !head; }
unsigned int List::size() const { return count; }
void List::connectNodes(unsigned int index1, unsigned int index2) {
if (index1 == index2 || index1 >= count || index2 >= count) {
throw std::out_of_range{"Index out of range!"};
}
Node *node1 = nullptr;
Node *node2 = nullptr;
auto node = head.get();
for (unsigned int i = 0; i <= std::max(index1, index2) && node; ++i) {
if (i == index1)
node1 = node;
if (i == index2)
node2 = node;
node = node->next.get();
}
if (!node1 || !node2) {
throw std::out_of_range{"Index out of range!"};
}
node1->next = std::move(node2->next);
}
void List::print() const {
std::cout << "PrintList starts.\n";
for (auto node = head.get(); node; node = node->next.get()) {
std::cout << node->data << " ";
}
std::cout << std::endl;
}
void List::append(int value) {
auto newNode = std::make_unique<Node>();
newNode->data = value;
Node *current = head.get();
while (current && current->next) {
current = current->next.get();
}
if (current) {
current->next = std::move(newNode);
} else {
head = std::move(newNode);
}
++count;
}
int List::get(unsigned int index) const {
if (index >= count) {
throw std::out_of_range{"Index out of range!"};
}
auto node = head.get();
for (unsigned int i = 0; i < index; ++i) {
node = node->next.get();
}
return node->data;
}
bool List::operator==(const List &other) const {
auto node1 = head.get();
auto node2 = other.head.get();
while (node1 && node2) {
if (node1->data != node2->data)
return false;
node1 = node1->next.get();
node2 = node2->next.get();
}
return !node1 && !node2;
}