-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked list.py
125 lines (102 loc) · 3.06 KB
/
linked list.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
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
class Node:
#create the linked list
def __init__(self, data) -> None:
self.data = data
self.next = None
class Linked_list:
#transformators
def __init__(self) -> None:
self.head = None
#add to the beggining of the list
def add(self,data) -> None:
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
new_node.next = self.head
self.head = new_node
#add to the end of the list
def append(self,data) -> None:
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current_node = self.head
while(current_node.next):
current_node = current_node.next
current_node.next = new_node
#remove from beggining of the list
def remove(self) -> None:
if self.head is None:
print("Can`t remove from an empty list")
return
self.head = self.head.next
#remove from the end of the list
def remove_end(self) -> None:
if self.head is None:
print("Can`t remove from an empty list")
return
current_node = self.head
if self.length() < 2:
if current_node.next is None:
self.head = None
return
current_node.next = None
return
while(current_node.next.next):
current_node = current_node.next
current_node.next = None
def display(self, quantity):
current_node = self.head
res = ''
i = 0
while(i < quantity):
# while(current_node):
res += str(current_node.data) + '\n'
current_node = current_node.next
i += 1
print(res)
#destroy the list
def destroy(self) -> None:
self.head = None
#observators
def is_empty(self) -> bool:
if self.head is None:
return True
return False
def length(self) -> int:
if self.head is None:
return 0
current_node = self.head
length = 0
while(current_node):
current_node = current_node.next
length += 1
return length
def get(self):
return self.head.data
def main():
data = [('AGH', 'Kraków', 1919),
('UJ', 'Kraków', 1364),
('PW', 'Warszawa', 1915),
('UW', 'Warszawa', 1915),
('UP', 'Poznań', 1919),
('PG', 'Gdańsk', 1945)]
uczelnie = Linked_list()
for i in data[:3]:
uczelnie.append(i)
for i in data[3:]:
uczelnie.add(i)
uczelnie.display(uczelnie.length())
print(uczelnie.length())
uczelnie.remove()
uczelnie.display(1)
uczelnie.remove_end()
uczelnie.display(uczelnie.length())
uczelnie.destroy()
print(uczelnie.is_empty())
uczelnie.remove()
uczelnie.append(data[0])
uczelnie.remove_end()
print(uczelnie.is_empty())
main()