Guest User

Untitled

a guest
Apr 1st, 2026
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. class Celula:
  2.  
  3. def __init__(self, valor):
  4. self.valor = valor
  5. self.proximo = None
  6.  
  7. def __str__(self):
  8. return '[%d]' % self.valor
  9.  
  10. ---------------------
  11.  
  12.  
  13. from celula import *
  14.  
  15. class L:
  16.  
  17. def __init__(self):
  18. self.inicio = None
  19. self.fim = None
  20.  
  21. def adicionar(self,valor):
  22. novo = Celula(valor)
  23.  
  24. if self.inicio == None:
  25. self.inicio = novo
  26. self.fim = novo
  27. else:
  28. # aux
  29. #[3][5][8][11].....[100]
  30. #adcionar [6](novo)
  31. while aux.proximo != None and aux.proximo.valor < novo.valor:
  32. aux = aux.proximo
  33.  
  34. novo.proximo = aux.proximo #[8]
  35. # aux
  36. #[3]--->[5]--->[8]--->[11].....[100]
  37. # ^
  38. # [6]--|
  39. # novo
  40. aux.proximo = novo
  41. # aux
  42. #[3]--->[5] [8]--->[11].....[100]
  43. # | ^
  44. # |->[6]--|
  45. # novo
  46.  
  47.  
  48. def __str__(self):
  49. saida = ''
  50. aux = self.inicio
  51.  
  52. while aux != None:
  53. saida += aux.__str__()
  54. aux = aux.proximo
  55. return saida
  56.  
  57. def busca(self,valor):
  58.  
  59. aux = self.inicio
  60.  
  61. while aux != None and aux.valor != valor :
  62. aux = aux.proximo
  63.  
  64. return aux
  65.  
  66.  
  67. ---------------------
  68. from L import *
  69.  
  70. l1 = L()
  71.  
  72. A = Celula(1)
  73. B = Celula(4)
  74. C = Celula(8)
  75. D = Celula(10)
  76. #[1][4][5][8]
  77. A.proximo = B
  78. B.proximo = C
  79. C.proximo = D
  80. l1.inicio = A
  81. l1.fim = D
  82. print(l1)
  83. print(l1.busca(0))
Advertisement
Add Comment
Please, Sign In to add comment