-
Notifications
You must be signed in to change notification settings - Fork 8
/
26_odwracanie_listy.py
78 lines (62 loc) · 2.05 KB
/
26_odwracanie_listy.py
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
from dataclasses import dataclass
from typing import Optional
@dataclass
class Wezel:
wartosc: int
nastepny: Optional["Wezel"] = None
class ListaPolaczona:
def __init__(self):
self.korzen = None
def dodaj(self, wartosc: int):
nowy_wezel = Wezel(wartosc)
if self.korzen is None:
self.korzen = nowy_wezel
else:
aktualny_wezel = self.korzen
while aktualny_wezel.nastepny is not None:
aktualny_wezel = aktualny_wezel.nastepny
aktualny_wezel.nastepny = nowy_wezel
def usun(self, wartosc: int):
if self.korzen is None:
return
if self.korzen.wartosc == wartosc:
self.korzen = self.korzen.nastepny
return
aktualny_wezel = self.korzen
while aktualny_wezel.nastepny is not None:
if aktualny_wezel.nastepny.wartosc == wartosc:
aktualny_wezel.nastepny = aktualny_wezel.nastepny.nastepny
return
aktualny_wezel = aktualny_wezel.nastepny
def dlugosc(self) -> int:
dlugosc = 0
aktualny_wezel = self.korzen
while aktualny_wezel is not None:
dlugosc += 1
aktualny_wezel = aktualny_wezel.nastepny
return dlugosc
def __iter__(self):
aktualny_wezel = self.korzen
while aktualny_wezel is not None:
yield aktualny_wezel.wartosc
aktualny_wezel = aktualny_wezel.nastepny
def __str__(self):
return str(list(self))
def odwroc_liste(lista: ListaPolaczona) -> ListaPolaczona:
wezly = []
for wezel in lista:
wezly.append(wezel)
wezly.reverse()
nowa_lista = ListaPolaczona()
for wezel in wezly:
nowa_lista.dodaj(wezel)
return nowa_lista
if __name__ == "__main__":
lista = ListaPolaczona()
lista.dodaj(1)
lista.dodaj(2)
lista.dodaj(3)
lista.dodaj(4)
lista.dodaj(5)
print(lista)
print(odwroc_liste(lista))