Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Celula:
- def __init__(self, valor):
- self.valor = valor
- self.proximo = None
- def __str__(self):
- return '[%d]' % self.valor
- ---------------------
- from celula import *
- class L:
- def __init__(self):
- self.inicio = None
- self.fim = None
- def adicionar(self,valor):
- novo = Celula(valor)
- if self.inicio == None:
- self.inicio = novo
- self.fim = novo
- else:
- # aux
- #[3][5][8][11].....[100]
- #adcionar [6](novo)
- while aux.proximo != None and aux.proximo.valor < novo.valor:
- aux = aux.proximo
- novo.proximo = aux.proximo #[8]
- # aux
- #[3]--->[5]--->[8]--->[11].....[100]
- # ^
- # [6]--|
- # novo
- aux.proximo = novo
- # aux
- #[3]--->[5] [8]--->[11].....[100]
- # | ^
- # |->[6]--|
- # novo
- def __str__(self):
- saida = ''
- aux = self.inicio
- while aux != None:
- saida += aux.__str__()
- aux = aux.proximo
- return saida
- def busca(self,valor):
- aux = self.inicio
- while aux != None and aux.valor != valor :
- aux = aux.proximo
- return aux
- ---------------------
- from L import *
- l1 = L()
- A = Celula(1)
- B = Celula(4)
- C = Celula(8)
- D = Celula(10)
- #[1][4][5][8]
- A.proximo = B
- B.proximo = C
- C.proximo = D
- l1.inicio = A
- l1.fim = D
- print(l1)
- print(l1.busca(0))
Advertisement
Add Comment
Please, Sign In to add comment