-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
293 lines (259 loc) · 8.03 KB
/
LinkedList.cpp
File metadata and controls
293 lines (259 loc) · 8.03 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "LinkedList.h"
#include <iostream>
using namespace std;
// Konstruktor dla elementu listy (Nod)
Nod::Nod(int d, int prio) : data(d), priority(prio), next(nullptr) {}
// Implementacja konstruktora LinkedList
LinkedList::LinkedList() : head(nullptr) {}
// Implementacja destruktora
LinkedList::~LinkedList()
{
clear(); // Wywołanie metody clear() aby zwolnić pamięć po obiekcie LinkedList
}
// Metoda dodająca element o priorytecie p
void LinkedList::insert(int e, int p)
{
Nod* newNode = new Nod(e, p); // Utworzenie nowego węzła
// Sprawdzenie czy lista jest pusta lub czy nowy element ma najwyższy priorytet
if (!head || p > head->priority)
{
newNode->next = head; // Nowy element staje się głową listy
head = newNode;
}
else
{
Nod* curr = head;
// Szukanie miejsca dla nowego elementu w liście
while (curr->next && curr->next->priority >= p)
{
curr = curr->next;
}
newNode->next = curr->next; // Wstawienie nowego elementu
curr->next = newNode;
}
}
// Implementacja metody extract_max
void LinkedList::extract_max()
{
if (!head)
{
cout << "Lista jest pusta!" << endl; // Komunikat o błędzie, jeśli lista jest pusta
return;
}
Nod* temp = head;
head = head->next; // Przesunięcie głowy listy na kolejny element
delete temp; // Zwolnienie pamięci zajmowanej przez usuwany element
}
// Implementacja metody find_max
// Jeśli elementy mają ten sam priorytet, stosowana jest zasada FIFO (First In, First Out)
void LinkedList::find_max() const
{
if (!head)
{
cout << "Lista jest pusta!" << endl; // Komunikat o błędzie, jeśli lista jest pusta
return;
}
// Wyświetlenie informacji o elemencie o najwyższym priorytecie
cout << "Maksymalny priorytet znajduje sie na poczatku listy i wynosi: ";
cout << head->priority << ", a wartosc elementu to: " << head->data << endl;
int j = 0;
Nod* curr = head->next;
// Sprawdzenie czy istnieją inne elementy o tym samym priorytecie
while (curr && curr->priority == head->priority)
{
// Wyświetlenie informacji o innych elementach o tym samym priorytecie
cout << "Inny element o tym samym priorytecie to: ";
cout << curr->priority << ", a wartosc elementu to: " << curr->data << endl;
curr = curr->next;
j++;
}
// Wyświetlenie komunikatu, jeśli nie ma innych elementów o tym samym priorytecie
if (j == 0)
{
cout << "Brak innych elementow o tym samym priorytecie" << endl;
}
}
// Implementacja metody modify_key
void LinkedList::modify_key(int e, int p)
{
Nod* prev = nullptr;
Nod* curr = head;
int index = 0; // Zmienna przechowująca aktualny indeks
// Szukanie elementu o danej wartości
while (curr && curr->data != e)
{
prev = curr;
curr = curr->next;
index++;
}
// Jeśli nie znaleziono elementu, wyświetl komunikat o błędzie
if (!curr)
{
cout << "Element o wartosci " << e << " nie istnieje na liscie." << endl;
return;
}
// Jeśli element ma być przesunięty do przodu, usuń go z obecnej pozycji
if (p > curr->priority)
{
if (prev)
{
prev->next = curr->next; // Usunięcie elementu z listy
}
else
{
head = curr->next; // Aktualizacja głowy listy, jeśli usuwany element był na początku
}
// Przeszukiwanie listy w celu znalezienia miejsca do wstawienia elementu
Nod* temp = head;
Nod* prev_temp = nullptr;
while (temp && temp->priority >= p)
{
prev_temp = temp;
temp = temp->next;
}
// Wstawienie elementu na odpowiednie miejsce w liście
if (prev_temp)
{
curr->next = prev_temp->next;
prev_temp->next = curr;
}
else
{
curr->next = head;
head = curr;
}
}
// Jeśli element ma być przesunięty do tyłu, usuń go z obecnej pozycji
else if (p < curr->priority)
{
if (prev) {
prev->next = curr->next; // Usunięcie elementu z listy
}
else
{
head = curr->next; // Aktualizacja głowy listy, jeśli usuwany element był na początku
}
// Przeszukiwanie listy w celu znalezienia miejsca do wstawienia elementu
Nod* temp = head;
Nod* prev_temp = nullptr;
while (temp && temp->priority >= p)
{
prev_temp = temp;
temp = temp->next;
}
// Wstawienie elementu na odpowiednie miejsce w liście
if (prev_temp)
{
curr->next = prev_temp->next;
prev_temp->next = curr;
}
else
{
curr->next = head;
head = curr;
}
}
// Aktualizacja priorytetu elementu
curr->priority = p;
// Wyświetlenie informacji o indeksie zmienionego priorytetu
cout << "Zmieniono priorytet elementu o wartosci " << e << " pod indeksem " << index << endl;
}
// Implementacja metody increase_key
void LinkedList::increase_key(int index, int p)
{
if (index < 0 || index >= return_size())
{
cout << "Indeks poza zakresem!" << endl; // Komunikat o błędzie
return;
}
Nod* curr = head;
// Przesunięcie do elementu o danym indeksie
for (int i = 0; i < index; i++)
{
curr = curr->next;
}
// Jeśli nowy priorytet jest mniejszy lub równy obecnemu, zwróć błąd
if (p <= curr->priority)
{
cout << "Nowy priorytet musi byc wiekszy niz obecny!" << endl;
return;
}
// Zaktualizuj priorytet elementu
curr->priority = p;
// Przesuń element w górę kolejki, jeśli to konieczne
while (curr->next && curr->next->priority < p)
{
Nod* temp = curr->next;
curr->next = temp->next;
temp->next = curr;
curr = temp;
}
}
// Implementacja metody decrease_key
void LinkedList::decrease_key(int index, int p)
{
if (index < 0 || index >= return_size())
{
cout << "Indeks poza zakresem!" << endl; // Komunikat o błędzie
return;
}
Nod* curr = head;
// Przesunięcie do elementu o danym indeksie
for (int i = 0; i < index; i++)
{
curr = curr->next;
}
// Jeśli nowy priorytet jest większy lub równy obecnemu, zwróć błąd
if (p >= curr->priority)
{
cout << "Nowy priorytet musi byc mniejszy niz obecny!" << endl;
return;
}
// Zaktualizuj priorytet elementu
curr->priority = p;
// Przesuń element w dół kolejki, jeśli to konieczne
while (curr->next && curr->next->priority > p)
{
Nod* temp = curr->next;
curr->next = temp->next;
temp->next = curr;
curr = temp;
}
}
// Implementacja metody return_size
int LinkedList::return_size() const
{
int count = 0;
Nod* curr = head;
// Przechodzenie po liście i zliczanie elementów
while (curr)
{
count++;
curr = curr->next;
}
return count;
}
// Implementacja metody show
void LinkedList::show() const
{
Nod* curr = head;
cout << "POCZATEK" << endl;
int i = 0;
// Przechodzenie po liście i wyświetlanie wartości oraz priorytetów elementów
while (curr) {
cout << i + 1 << ".) " << curr->priority << endl;
curr = curr->next;
i++;
}
cout << "KONIEC" << endl;
}
// Implementacja metody clear
void LinkedList::clear()
{
while (head) {
Nod* temp = head;
head = head->next;
delete temp; // Zwolnienie pamięci zajmowanej przez usuwany element
}
head = nullptr;
}