Last active
November 4, 2016 00:55
-
-
Save ricardosiri68/0b3ec935be97d002317666ef492f6bc3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding: utf-8 | |
class nodo(): | |
def __init__ (self, sig=None): | |
sig.padre = None | |
self.sig = sig | |
if sig: | |
sig.padre = self | |
def ultimo(self): | |
actual = self | |
while actual.sig: | |
actual = actual.sig | |
return actual | |
def primero(): | |
actual = self | |
while actual.padre: | |
actual = actual.padre | |
return actual | |
def agregaFinal(self, objeto): | |
nodoFinal = self.ultimo() | |
nodoFinal.sig = objeto | |
def agregaInicio(self, objeto): | |
objeto.sig = self | |
return objeto | |
def insertar(self, objeto, pos): | |
actual = self | |
i = 1 # cabeza | |
while i < pos and actual.sig is not None: | |
actual = actual.sig | |
i += 1 | |
objeto.sig = actual.sig | |
actual.sig = objeto | |
def borraUltimo(self): | |
actual = self | |
while actual.sig is not None: | |
anterior = actual | |
actual = actual.sig | |
anterior.sig = None | |
def borraPrimero(self): | |
siguiente = self.sig | |
return siguiente | |
def borraPos(self, pos): | |
actual = self | |
i = 1 | |
while i<pos and actual.sig is not None: | |
actual = actual.sig | |
i += 1 | |
if actual.sig is not None: | |
actual.sig = actual.sig.sig | |
# misc | |
def __len__(self): | |
n = 0 | |
actual = self | |
while actual.sig is not None: | |
actual = actual.sig | |
n += 1 | |
return n + 1 | |
class nPersona(nodo): | |
def __init__ (self, nombre, edad): | |
nodo.__init__(self, None) | |
self.editar(nombre, edad) | |
def editar(self, nombre, edad): | |
self.nombre = nombre | |
self.edad = edad | |
c = nPersona("Alejandra",10) | |
c.agregaFinal(nPersona("Benja",11)) | |
c.agregaFinal(nPersona("Carla",12)) | |
c = c.agregaInicio(nPersona("Abel",9)) | |
c.insertar(nPersona("Jaime",15),10) | |
#c.borraUltimo() | |
c = c.borraPrimero() | |
# recorrer los objetos | |
actual = c # tomo el primero (cabezal) | |
while actual is not None: | |
print(actual.nombre, actual.edad) | |
actual = actual.sig | |
print (len(c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment