From 67cde58080d39b52c2c21ad15d2b34c825413d85 Mon Sep 17 00:00:00 2001 From: Xhunik Miguel Date: Sat, 20 Feb 2021 22:04:11 -0600 Subject: [PATCH] fixeado operaciones delete y print --- src/app.py | 14 +------------- src/models/ListaEnlazada.py | 36 +++++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/app.py b/src/app.py index 4ca8896..29e66bc 100644 --- a/src/app.py +++ b/src/app.py @@ -1,16 +1,4 @@ from models import ListaEnlazada -''' - TODO: - - fixear funciones 'delete' cuando se borra sobre una lista vacío - - fixear funcion 'print' cuando se imprime sobre vacío -''' - if __name__ == '__main__': - lista = ListaEnlazada() - lista.add_to_end('1') - lista.add_to_end('2') - lista.add_to_end('3') - lista.add_to_end('4') - lista.add_to_end('5') - lista.print_list() + pass diff --git a/src/models/ListaEnlazada.py b/src/models/ListaEnlazada.py index 0d9cff8..9469662 100644 --- a/src/models/ListaEnlazada.py +++ b/src/models/ListaEnlazada.py @@ -43,22 +43,32 @@ def get_by_index(self, index: int): node = node.next return None + # Elimina el primer elemento de la lista def delete_first(self): - self.head = self.head.next + if not self.is_void(): + self.head = self.head.next + # Elimina el ultimo elemento de la lista def delete_last(self): - node = self.head - prev = None - while node.next != None: - prev = node - node = node.next - prev.next = None + if not self.is_void(): + node = self.head + prev = None + while node.next != None: + prev = node + node = node.next + if self.get_size() == 1: + self.head = None + else: + prev.next = None # Imprimir Lista def print_list(self): - print('head: {}'.format(self.head.data)) - node = self.head - while node != None: - print('{}'.format(node.data), end=' -> ') - node = node.next - print() + if not self.is_void(): + print('head: {}'.format(self.head.data)) + node = self.head + while node != None: + print('{}'.format(node.data), end=' -> ') + node = node.next + print() + else: + print('is void')