SHOW:
|
|
- or go back to the newest paste.
| 1 | # Lista Duplamente Encadeada com uso de Sentinelas | |
| 2 | ||
| 3 | class Dnodo: | |
| 4 | def __init__(self, dado = None): | |
| 5 | self.dado = dado | |
| 6 | self.anterior = None | |
| 7 | self.proximo = None | |
| 8 | ||
| 9 | def __str__(self): | |
| 10 | return str(self.dado) | |
| 11 | ||
| 12 | class LDE: | |
| 13 | def __init__(self): | |
| 14 | self.header = Dnodo() # <<< Sentinela Cabeça | |
| 15 | self.trailer = Dnodo() # <<< Sentinela Cauda | |
| 16 | self.tam = 0 # qtde de itens na lista | |
| 17 | ||
| 18 | def is_empty(self): | |
| 19 | if self.header.proximo is None and self.trailer.anterior is None: | |
| 20 | return True | |
| 21 | return False | |
| 22 | ||
| 23 | def __inserir(self, novo): | |
| 24 | self.header.proximo = novo # header -> novo | |
| 25 | novo.anterior = self.header # header <- novo | |
| 26 | novo.proximo = self.trailer # novo -> trailer | |
| 27 | self.trailer.anterior = novo # novo <- trailer | |
| 28 | ||
| 29 | - | def inserirInicio(self, novo): |
| 29 | + | def inserir_inicio(self, novo): |
| 30 | self.tam += 1 | |
| 31 | if self.is_empty(): | |
| 32 | self.__inserir(novo) | |
| 33 | - | atual_primeiro = self.header.proximo #para saber quem eh o primeiro |
| 33 | + | |
| 34 | - | novo.proximo = atual_primeiro # atual <- novo |
| 34 | + | pass |
| 35 | - | atual_primeiro.anterior = novo # atual -> novo |
| 35 | + | |
| 36 | - | self.header.proximo = novo # header -> novo |
| 36 | + | def inserir_fim(self, novo): |
| 37 | - | novo.anterior = self.header # header <- novo |
| 37 | + | |
| 38 | if self.is_empty(): | |
| 39 | self.__inserir(novo) | |
| 40 | else: | |
| 41 | - | def inserirFim(self, novo): |
| 41 | + | |
| 42 | ||
| 43 | def remover_inicio(self): | |
| 44 | if self.is_empty(): | |
| 45 | print('Lista Vazia!')
| |
| 46 | return | |
| 47 | else: | |
| 48 | # lista possui + de 1 item | |
| 49 | - | def removerInicio(self): |
| 49 | + | |
| 50 | # restante do codigo aqui... | |
| 51 | ||
| 52 | self.tam -= 1 | |
| 53 | return removido | |
| 54 | ||
| 55 | def remover_fim(self): | |
| 56 | - | novo_head = removido.proximo |
| 56 | + | |
| 57 | - | self.header.proximo = novo_head |
| 57 | + | |
| 58 | - | removido.proximo = None |
| 58 | + | |
| 59 | - | removido.anterior = None |
| 59 | + | |
| 60 | else: | |
| 61 | # refazer os apontamentos | |
| 62 | pass | |
| 63 | ||
| 64 | - | def removerFim(self): |
| 64 | + | |
| 65 | return removido | |
| 66 | ||
| 67 | def imprimir(self, reverso=False): | |
| 68 | if self.is_empty(): | |
| 69 | print('Lista Vazia')
| |
| 70 | return | |
| 71 | ||
| 72 | if not reverso: | |
| 73 | item = self.header.proximo # devolve o 1o da lista | |
| 74 | while (item.proximo is not None): | |
| 75 | print(item) | |
| 76 | item = item.proximo | |
| 77 | else: | |
| 78 | item = self.trailer.anterior # devolve o ultimo da lista | |
| 79 | while (item.anterior is not None): | |
| 80 | print(item) | |
| 81 | - | if not reverso: # mesmo que if (reverso == True) |
| 81 | + | |
| 82 | ||
| 83 | def buscar(self, alvo): # retorna a 1a ocorrencia | |
| 84 | pass | |
| 85 | ||
| 86 | def get(self, indice): | |
| 87 | pass | |
| 88 | ||
| 89 | def remover_antes(self, alvo): | |
| 90 | # header <-> [] <-> [] <-> trailer | |
| 91 | ||
| 92 | - | def removerAntes(self, alvo): |
| 92 | + | |
| 93 | nodo_anterior = nodo_atual.anterior | |
| 94 | ||
| 95 | if (nodo_anterior != self.header): | |
| 96 | aux = nodo_anterior.anterior | |
| 97 | aux.proximo = nodo_atual | |
| 98 | nodo_atual.anterior = aux | |
| 99 | ||
| 100 | nodo_anterior.anterior = None | |
| 101 | nodo_anterior.proximo = None | |
| 102 | ||
| 103 | self.tam -= 1 | |
| 104 | ||
| 105 | def remover_apos(self, alvo): | |
| 106 | pass | |
| 107 | ||
| 108 | - | def removerApos(self, alvo): |
| 108 | + | |
| 109 | pass | |
| 110 | ||
| 111 | def buscar_todos(self, alvo): # retorna uma list com resultados | |
| 112 | lista_todos = [] | |
| 113 | ## | |
| 114 | ## sua logica aqui | |
| 115 | ## | |
| 116 | return lista_todos | |
| 117 | ||
| 118 | def primeiro(self): | |
| 119 | if self.is_empty(): | |
| 120 | - | def buscarTodos(self, alvo): # retorna uma list com resultados |
| 120 | + | |
| 121 | return | |
| 122 | else: | |
| 123 | return self.header.proximo | |
| 124 | ||
| 125 | def ultimo(self): | |
| 126 | pass | |
| 127 | ||
| 128 | def __len__(self): | |
| 129 | return self.tam | |
| 130 | ||
| 131 | ## TESTES ## | |
| 132 | ||
| 133 | lista = LDE() | |
| 134 | lista.inserir_inicio(Dnodo('abc'))
| |
| 135 | lista.inserir_inicio(Dnodo('001'))
| |
| 136 | lista.inserir_inicio(Dnodo('xyz'))
| |
| 137 | lista.remover_inicio() | |
| 138 | lista.remover_inicio() | |
| 139 | - | lista.inserirInicio(Dnodo('abc'))
|
| 139 | + | lista.remover_inicio() |
| 140 | - | lista.inserirInicio(Dnodo('001'))
|
| 140 | + | lista.remover_inicio() |
| 141 | - | lista.inserirInicio(Dnodo('xyz'))
|
| 141 | + |