fork download
  1. # Lista
  2. class Lista:
  3.  
  4. class Nodo:
  5. def __init__(self, valor, sig):
  6. self.valor = valor
  7. self.sig = sig
  8.  
  9. @property
  10. def valor(self):
  11. return self.__valor
  12.  
  13. @valor.setter
  14. def valor(self, x):
  15. self.__valor = x
  16.  
  17. @property
  18. def sig(self):
  19. return self.__sig
  20.  
  21. @sig.setter
  22. def sig(self, x):
  23. self.__sig = x
  24.  
  25. def __init__(self):
  26. self.__org = None
  27. self.__fin = None
  28. self.__num = 0
  29.  
  30. @property
  31. def org(self):
  32. return self.__org
  33.  
  34. @property
  35. def fin(self):
  36. return self.__fin
  37.  
  38. def inserta(self, x):
  39. assert(x != None)
  40. assert((self.org != None and self.fin != None)
  41. or (self.org == None and self.fin == None))
  42.  
  43. nodo = Lista.Nodo(x, None)
  44.  
  45. if self.fin != None:
  46. self.fin.sig = nodo
  47. self.__fin = self.fin.sig
  48. else:
  49. self.__fin = nodo
  50.  
  51. if self.org == None:
  52. self.__org = self.fin
  53.  
  54. self.__num += 1
  55. self.__recorre()
  56.  
  57. if __debug__:
  58. def __recorre(self):
  59. num = 0
  60. it = self.org
  61.  
  62. while it != None:
  63. num += 1
  64. it = it.sig
  65.  
  66. print("Recorrido.")
  67. assert(len(self) == num)
  68. else:
  69. def __recorre(self):
  70. pass
  71.  
  72. def __nonzero__(self):
  73. return len(self) > 0
  74.  
  75. def __len__(self):
  76. assert(self.__num > 0)
  77. return self.__num
  78.  
  79. def __str__(self):
  80. toret = "[ "
  81. it = self.org
  82.  
  83. while it != None:
  84. toret += str(it.valor) + ' '
  85. it = it.sig
  86.  
  87. return toret + ']'
  88.  
  89. l = Lista()
  90. l.inserta(1)
  91. l.inserta(2)
  92. l.inserta(3)
  93.  
  94. print(l)
  95.  
Success #stdin #stdout 0.02s 8736KB
stdin
Standard input is empty
stdout
[ 1 2 3 ]