-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.h
More file actions
175 lines (144 loc) · 4.59 KB
/
list.h
File metadata and controls
175 lines (144 loc) · 4.59 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
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
#ifndef LIST_H
#define LIST_H
#include <memory>
#include <iostream>
template<typename T>
class ListNode {
std::shared_ptr<T> data;
std::shared_ptr<ListNode> next;
public:
// konstruktor
ListNode(const std::shared_ptr<T>& data) : data(data), next(nullptr) {}
std::shared_ptr<T> get_data() const { return data; }
std::shared_ptr<ListNode> get_next() const { return next; }
void set_next(const std::shared_ptr<ListNode>& node) { next = node; }
};
template<typename T>
class LinkedList {
std::shared_ptr<ListNode<T>> head;
public:
// vrati true ak je zoznam prazdny
bool is_empty() const {
return head == nullptr;
}
// vrati ukazovatel na prvy uzol
std::shared_ptr<ListNode<T>> get_head() const {
return head;
}
// vlozi novy prvok na zaciatok zoznamu
void push_front(const std::shared_ptr<T>& value) {
if (!value) {
std::cerr << "CHYBA: Pridavana hodnota je null.\n";
return;
}
auto new_node = std::make_shared<ListNode<T>>(value);
new_node->set_next(head);
head = new_node;
}
// vymaze prvy prvok v zozname, stary uzol zanikne vdaka shared_ptr
void pop_front() {
if (is_empty()) {
std::cerr << "CHYBA: Zoznam je prazdny, nie je co odstranit.\n";
return;
}
head = head->get_next();
}
void pop_index(int index) {
if (index < 0) {
std::cerr << "CHYBA: Index nemoze byt zaporny.\n";
return;
}
if (is_empty()) {
std::cerr << "CHYBA: Zoznam je prazdny, nie je co odstranit.\n";
return;
}
if (index == 0) {
pop_front();
return;
}
auto current = head;
int current_index = 0;
while (current && current_index < index - 1) {
current = current->get_next();
current_index++;
}
if (!current || !current->get_next()) {
std::cerr << "CHYBA: Index je mimo rozsahu zoznamu.\n";
return;
}
current->set_next(current->get_next()->get_next());
}
//najde index podla pointeru
int find_index(const std::shared_ptr<T>& value) const {
if (!value) {
std::cerr << "CHYBA: Hladana hodnota je null.\n";
return -1;
}
auto current = head;
int index = 0;
while (current) {
if (current->get_data() == value) {
return index;
}
current = current->get_next();
index++;
}
std::cerr << "CHYBA: Hladana hodnota sa v zozname nenachadza.\n";
return -1;
}
// vlozi novy prvok za uzol na danom indexe
void insert_after_index(int index, const std::shared_ptr<T>& value) {
if (index < 0) {
std::cerr << "CHYBA: Index nemoze byt zaporny.\n";
return;
}
if (!value) {
std::cerr << "CHYBA: Pridavana hodnota je null.\n";
return;
}
auto current = head;
int current_index = 0;
while (current && current_index < index) {
current = current->get_next();
current_index++;
}
if (!current) {
std::cerr << "CHYBA: Index je mimo rozsahu zoznamu.\n";
return;
}
auto new_node = std::make_shared<ListNode<T>>(value);
new_node->set_next(current->get_next());
current->set_next(new_node);
}
// vlozi novy node za dany node, ak node neexistuje, vypise chybu
void insert_after(const std::shared_ptr<ListNode<T>>& node, const std::shared_ptr<T>& value) {
if (!node) {
std::cerr << "CHYBA: Zadany uzol je null. Neda sa vlozit prvok.\n";
return;
}
if (!value) {
std::cerr << "CHYBA: Pridavana insert hodnota je null.\n";
return;
}
auto new_node = std::make_shared<ListNode<T>>(value);
new_node->set_next(node->get_next());
node->set_next(new_node);
}
// vypise cely zoznam pomocou get_id(), predpoklada sa ze typ T ma metodu get_id()
void print_all() const {
if (is_empty()) {
std::cout << "Zoznam je prazdny.\n";
return;
}
auto current = head;
while (current) {
if (current->get_data()) {
std::cout << current->get_data()->get_id() << '\n';
} else {
std::cerr << "CHYBA: Uzol obsahuje null data.\n";
}
current = current->get_next();
}
}
};
#endif // LIST_H