-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lista.cpp
83 lines (67 loc) · 1.41 KB
/
Lista.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
#include "Lista.h"
#include <iostream>
Lista::Lista(int wierzcholki)
{
ilosc_wierzch = wierzcholki;
lista = new Element_listy*[wierzcholki] {nullptr};
}
Lista::~Lista()
{
Element_listy* tymcz;
for (unsigned i = 0; i < ilosc_wierzch; ++i)
{
while (lista[i] != nullptr)
{
tymcz = lista[i]->nastepny;
delete lista[i];
lista[i] = tymcz;
}
}
delete lista;
}
void Lista::dodaj_k(int od_wierz, int do_wierz, int waga)
{
Element_listy* tymcz = lista[od_wierz];
Element_listy* krawedz = new Element_listy{ waga, do_wierz, nullptr };
if ( lista[od_wierz] == nullptr)
{
lista[od_wierz] = krawedz;
}
else
{
while (tymcz->nastepny != nullptr)
{
tymcz = tymcz->nastepny;
}
tymcz->nastepny = krawedz;
}
}
int Lista::pobierz_wage(int od_wierz, int do_wierz)
{
Element_listy* nastepny = lista[od_wierz];
for (unsigned q = 0; q <= ilosc_wierzch; ++q)
{
if (nastepny == nullptr)
{
return INT_MAX;
}
if (nastepny->do_wierz == do_wierz)
{
return nastepny->waga;
}
nastepny = nastepny->nastepny;
}
}
void Lista::wyswietl()
{
for (unsigned q = 0; q < ilosc_wierzch; ++q)
{
Element_listy* nastepny = lista[q];
while (nastepny != nullptr)
{
std::cout << '(' << nastepny->do_wierz << ',' << nastepny->waga << ')' << "\t";
nastepny = nastepny->nastepny;
}
std::cout << "\n";
}
}