-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab003Task001.py
170 lines (118 loc) · 3.84 KB
/
Lab003Task001.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
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
"""
Реализуйте АТД линейный список
посредством двусвязных списков,
дополнительно предусмотрите операции:
Search(x,L), возвращающую позицию
элемента x в списке, PrintList(L),
печатающую элементы списка в
порядке их расположения.
"""
class Node:
def __init__(self, label):
self.label = label
self.next = None
def getLabel(self):
return self.label
def setLabel(self, label):
self.label = label
def getNext(self):
return self.next
def setNext(self, next):
self.next = next
class LinkedList:
def __init__(self):
self.first = None
self.last = None
self.len_list = 0
def push(self, label, index):
if index >= 0:
node = Node(label)
if self.empty():
self.first = node
self.last = node
else:
if index == 0:
node.setNext(self.first)
self.first = node
elif index >= self.len_list:
self.last.setNext(node)
self.last = node
else:
prev_node = self.first
curr_node = self.first.getNext()
curr_index = 1
while curr_node != None:
if curr_index == index:
node.setNext(curr_node)
prev_node.setNext(node)
prev_node = curr_node
curr_node = curr_node.getNext()
curr_index += 1
self.len_list += 1
def pop(self, index):
if not self.empty() and index >= 0 and index < self.len_list:
flag_remove = False
if self.first.getNext() == None:
self.first = None
self.last = None
flag_remove = True
elif index == 0:
self.first = self.first.getNext()
flag_remove = True
else:
prev_node = self.first
curr_node = self.first.getNext()
curr_index = 1
while curr_node != None:
if index == curr_index:
# o proximo do anterior aponta para o proximo do nó corrente
prev_node.setNext(curr_node.getNext())
curr_node.setNext(None)
flag_remove = True
break
prev_node = curr_node
curr_node = curr_node.getNext()
curr_index +=1
if flag_remove:
# atualiza o tamanho da lista
self.len_list -= 1
def empty(self):
if self.first == None:
return True
return False
def length(self, x):
return self.len_list
def show(self):
curr_node = self.first
while curr_node != None:
print(curr_node.getLabel(), end=' ')
curr_node = curr_node.getNext()
print()
def search(self, x):
curr_node = self.len_list
while curr_node == x:
print(curr_node.getLabel(), end=' ')
curr_node = curr_node.getLabel()
print()
list = LinkedList()
list.push('Marcos', 0)
list.show()
list.push('Maria', 1)
list.show()
list.push('Yankee', 0)
list.show()
list.push('Catarina', 2)
list.show()
list.push('Lilica', 5)
list.show()
list.push('Roberta', 4)
list.show()
print('Tamanho da lista: {}.'.format(list.length()))
list.pop(0)
list.show()
list.pop(2)
list.show()
list.pop(5)
list.show()
print('Tamanho da lista: {}.'.format(list.length()))
list.search(1)