|
1 | 1 | """ |
2 | | -Lista Duplamente Encadeada |
| 2 | +Doubly Linked List |
3 | 3 | |
4 | | -A cabeca da lista sempre 'aponta' para o primeiro no |
5 | | -O rabo da lista sempre 'aponta' para o ultimo no |
| 4 | +The 'head' of the list always points to the first node |
| 5 | +The 'tail' of the list always points to the final node |
6 | 6 | |
7 | 7 | None <--- | 2 | ---> None |
8 | 8 | None <--- | 2 | <---> | 5 | ---> None |
|
11 | 11 | """ |
12 | 12 |
|
13 | 13 |
|
14 | | -class No: |
15 | | - def __init__(self, dado, anterior, proximo): |
16 | | - self.dado = dado |
17 | | - self.anterior = anterior |
18 | | - self.proximo = proximo |
| 14 | +class Node: |
| 15 | + def __init__(self, data, prev, next): |
| 16 | + self.data = data |
| 17 | + self.prev = prev |
| 18 | + self.next = next |
19 | 19 |
|
20 | 20 |
|
21 | | -class ListaDuplamenteEncadeada: |
22 | | - cabeca = None |
23 | | - rabo = None |
| 21 | +class DoublyLinkedList: |
| 22 | + head = None |
| 23 | + tail = None |
24 | 24 |
|
25 | | - def acrescentar(self, dado): |
26 | | - """Acrescenta um novo no a lista.""" |
27 | | - # Cria um novo no apontando para None (anterior e proximo) |
28 | | - novo_no = No(dado, None, None) |
| 25 | + def append(self, data): |
| 26 | + # Creates a new node pointing to None (prev and next) |
| 27 | + new_node = Node(data, None, None) |
29 | 28 |
|
30 | | - # Se a cabeca eh None a lista esta vazia |
31 | | - # Tanto a cabeca quanto o rabo recebem o novo no |
32 | | - if self.cabeca is None: |
33 | | - self.cabeca = novo_no |
34 | | - self.rabo = novo_no |
35 | | - # Caso contrario, se ja existir algum valor na lista |
| 29 | + # If the list is empty, both head and tail point to the new node |
| 30 | + if self.head is None: |
| 31 | + self.head = new_node |
| 32 | + self.tail = new_node |
36 | 33 | else: |
37 | | - # O anterior 'aponta' para o rabo (ultimo no adicionado) |
38 | | - novo_no.anterior = self.rabo |
39 | | - # O proximo sempre aponta para None |
40 | | - novo_no.proximo = None |
41 | | - # O proximo do rabo sempre aponta para o novo no |
42 | | - self.rabo.proximo = novo_no |
43 | | - # O rabo agora eh o novo no |
44 | | - self.rabo = novo_no |
45 | | - |
46 | | - def remover(self, dado): |
47 | | - """Remove um no da lista.""" |
48 | | - # O no atual eh o primeiro no da lista |
49 | | - no_atual = self.cabeca |
50 | | - |
51 | | - # Vamos procurar pelo dado que queremos remover |
52 | | - # Equanto o no atual for valido |
53 | | - while no_atual is not None: |
| 34 | + # For a non-empty list, adjust pointers to add the new node at the end |
| 35 | + new_node.prev = self.tail # New node's prev points to the current tail |
| 36 | + self.tail.next = new_node # Current tail's next points to the new node |
| 37 | + self.tail = new_node # Update tail to be the new node |
| 38 | + |
| 39 | + # No additional 'new_node.next = None' is needed as it's already None by default |
| 40 | + |
| 41 | + def delete(self, data): |
| 42 | + """Deletes a node from the list""" |
| 43 | + """ Current node is first node in the list""" |
| 44 | + curr_node = self.head |
| 45 | + |
| 46 | + # We search for the data we want to delete |
| 47 | + # While current node is not invalid |
| 48 | + while curr_node is not None: |
54 | 49 | # Verifica se eh o dado que estamos buscando |
55 | | - if no_atual.dado == dado: |
56 | | - # Se o dado que estamos buscando esta no primeiro no |
57 | | - # da lista, nao temos anterior |
58 | | - if no_atual.anterior is None: |
59 | | - # A cabeca 'aponta' para o proximo no da lista |
60 | | - self.cabeca = no_atual.proximo |
61 | | - # E o anterior do proximo no aponta para None |
62 | | - no_atual.proximo.anterior = None |
| 50 | + if curr_node.data == data: |
| 51 | + # If data we are looking for is in first node |
| 52 | + # If we do not have a previous node in the list |
| 53 | + if curr_node.prev is None: |
| 54 | + # Head points to next node in the list |
| 55 | + self.head = curr_node.next |
| 56 | + # And the previous of the next node does not point to None |
| 57 | + curr_node.next.prev = None |
63 | 58 | else: |
64 | | - # Exemplo: Removendo o valor 5 |
| 59 | + # Example: Deleting the value 5 |
65 | 60 | # ... <---> | 2 | <---> | 5 | <---> | 12 | <---> ... |
66 | 61 | # |
67 | | - # O proximo do valor 2 passa a apontar para o 12 e |
68 | | - # o anterior do valor 12 passa a apontar para o 2 |
| 62 | + # Next node of 2 will now point to 12 instead of 5 |
| 63 | + # Previous node of 12 will not point to 2 instead of 5 |
69 | 64 | # --------------- |
70 | 65 | # ... <---> | 2 | <---|--- | 5 | ---|---> | 12 | <---> ... |
71 | | - no_atual.anterior.proximo = no_atual.proximo |
72 | | - no_atual.proximo.anterior = no_atual.anterior |
73 | | - |
74 | | - # Se nao eh o no que estamos buscando va para o proximo |
75 | | - no_atual = no_atual.proximo |
76 | | - |
77 | | - def mostrar(self): |
78 | | - """Mostra todos os dados da lista.""" |
79 | | - print("Lista Duplamente Encadeada:") |
80 | | - |
81 | | - # O no atual eh o primeiro no da lista |
82 | | - no_atual = self.cabeca |
83 | | - |
84 | | - no = "" |
85 | | - # Para cada no valido da lista |
86 | | - while no_atual is not None: |
87 | | - if no_atual.anterior is None: |
88 | | - no += "None " |
89 | | - no += "<---> | " + str(no_atual.dado) + " | " |
90 | | - if no_atual.proximo is None: |
91 | | - no += "<---> None" |
92 | | - |
93 | | - no_atual = no_atual.proximo |
94 | | - print(no) |
| 66 | + curr_node.prev.next = curr_node.next |
| 67 | + curr_node.next.prev = curr_node.prev |
| 68 | + |
| 69 | + # If current node does not hold desired data, move to next node |
| 70 | + curr_node = curr_node.next |
| 71 | + |
| 72 | + def display(self): |
| 73 | + """Displays all data in the list""" |
| 74 | + print("Doubly Linked List: ") |
| 75 | + |
| 76 | + # Current node is head of the list |
| 77 | + curr_node = self.head |
| 78 | + |
| 79 | + node = "" |
| 80 | + # For each valid node in the list |
| 81 | + while curr_node is not None: |
| 82 | + if curr_node.prev is None: |
| 83 | + node += "None " |
| 84 | + node += "<---> | " + str(curr_node.data) + " | " |
| 85 | + if curr_node.next is None: |
| 86 | + node += "<---> None" |
| 87 | + |
| 88 | + curr_node = curr_node.next |
| 89 | + print(node) |
95 | 90 | print("=" * 80) |
96 | 91 |
|
97 | 92 |
|
98 | | -lista = ListaDuplamenteEncadeada() |
| 93 | +list = DoublyLinkedList() |
99 | 94 |
|
100 | | -lista.acrescentar(2) |
101 | | -lista.mostrar() |
102 | | -lista.acrescentar(5) |
103 | | -lista.mostrar() |
104 | | -lista.acrescentar(12) |
105 | | -lista.mostrar() |
106 | | -lista.acrescentar(20) |
107 | | -lista.mostrar() |
| 95 | +list.append(2) |
| 96 | +list.display() |
| 97 | +list.append(5) |
| 98 | +list.display() |
| 99 | +list.append(12) |
| 100 | +list.display() |
| 101 | +list.append(20) |
| 102 | +list.display() |
108 | 103 |
|
109 | | -lista.remover(12) |
110 | | -lista.mostrar() |
111 | | -lista.remover(5) |
112 | | -lista.mostrar() |
| 104 | +list.delete(12) |
| 105 | +list.display() |
| 106 | +list.delete(5) |
| 107 | +list.display() |
0 commit comments