Advertisement
fahadkalil

lse_alunos_12092019

Sep 19th, 2019
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.57 KB | None | 0 0
  1. class Nodo:
  2.     def __init__(self, dado):
  3.         self.dado = dado
  4.         self.proximo = None
  5.  
  6.     def __str__(self):
  7.         return str(self.dado)
  8.  
  9. # ##############################
  10. # Lista Simplesmente Encadeada
  11. # [NODO] => [NODO] => None
  12. class LSE:
  13.     def __init__(self):
  14.         self.head = None # cabeca | inicio
  15.         self.tail = None # cauda  | fim
  16.         self.tam = 0     # quantidade de elementos
  17.  
  18.     def is_empty(self): # retorna se a lista esta vazia
  19.         if (self.head is None and self.tail is None):
  20.             return True
  21.         return False
  22.        
  23.     def inserirFim(self, novo): # similar ao append da List
  24.         self.tam += 1        
  25.         if self.is_empty():
  26.             # lista vazia
  27.             self.head = novo
  28.             self.tail = novo            
  29.         else:
  30.             # ja possui itens
  31.             ## o tail atual deve apontar para o novo
  32.             ## o tail sera atribuido para o novo
  33.             ultimo = self.tail
  34.             ultimo.proximo = novo
  35.             self.tail = novo            
  36.  
  37.     def inserirInicio(self, novo): # similar ao insert(0,item) da List
  38.         self.tam += 1
  39.         if self.is_empty():
  40.             self.head = novo
  41.             self.tail = novo
  42.         else:
  43.             # obtem o atual head
  44.             primeiro = self.head
  45.             novo.proximo = primeiro
  46.             self.head = novo            
  47.        
  48.     def removerInicio(self):
  49.         if self.is_empty():
  50.             print('Lista Vazia!')
  51.             return
  52.  
  53.         self.tam -= 1 # diminui o contador de itens
  54.        
  55.         ## quando temos apenas 1 item
  56.         if (self.head == self.tail):
  57.             removido = self.head
  58.             self.head = None
  59.             self.tail = None            
  60.  
  61.         # lista possui + de 1 item              
  62.         else:            
  63.             removido = self.head
  64.             novo_head = removido.proximo
  65.             self.head = novo_head
  66.             removido.proximo = None
  67.                        
  68.         return removido
  69.    
  70.     def removerFim(self):
  71.         if self.is_empty():
  72.             print('Lista Vazia!')
  73.            
  74.         ## precisamos descobrir quem eh o penultimo da lista!!
  75.         removido = None
  76.         item = self.head
  77.         while (item != None):
  78.             # quando tem apenas 1 item
  79.             if (item == self.tail and item == self.head):
  80.                 self.head = None
  81.                 self.tail = None
  82.                 self.tam -= 1
  83.                 return item
  84.  
  85.             # quando tem mais de 1
  86.             if (item.proximo != None and item.proximo == self.tail):
  87.                 removido = self.tail
  88.                 self.tail = item
  89.                 item.proximo = None
  90.                 self.tam -= 1
  91.                 return removido
  92.            
  93.             item = item.proximo
  94.            
  95.     def buscar(self, valor):
  96.         if self.is_empty():
  97.             print('Lista Vazia')
  98.             return
  99.         else:
  100.             item = self.head
  101.             while (item != None):
  102.                 if valor == item.dado:
  103.                     return item
  104.                
  105.                 item = item.proximo                
  106.             return None            
  107.    
  108.     def imprimir(self):
  109.         if (self.head is None and self.tail is None):
  110.             print('Lista Vazia')
  111.             return
  112.            
  113.         item = self.head
  114.         while (item != None):
  115.             print(item)
  116.             item = item.proximo
  117.  
  118.     def imprimirLadoALado(self):
  119.         saida = ''
  120.         item = self.head
  121.         while (item != None):
  122.             if item == self.head:
  123.                 saida += '[' + str(item) + ']'
  124.             else:
  125.                 saida += ' => ' + '[' + str(item) + ']'
  126.             item = item.proximo
  127.         print(saida)
  128.  
  129.     def __len__(self):
  130.         return self.tam
  131.  
  132.     def clear(self):
  133.         while self.head != None:
  134.             self.removerInicio()
  135.  
  136.     def remover(self, valor):
  137.         if self.is_empty():
  138.             print('Lista Vazia')
  139.             return
  140.         else:            
  141.             item = self.head
  142.             anterior = None
  143.             while (item != None):                
  144.                 if valor == item.dado: # encontrei o alvo                    
  145.                     self.tam -= 1 # decrementa a quantidade de itens                                        
  146.                     anterior.proximo = item.proximo # fazer o nodo anterior a ele apontar para o proximo
  147.                     item.proximo = None
  148.                     return item
  149.                
  150.                 anterior = item # guarda o item que representa o anterior
  151.                 item = item.proximo              
  152.             return None            
  153.  
  154.     def get(self, indice):
  155.         if self.is_empty():
  156.             print('Lista Vazia')
  157.             return
  158.         else:
  159.             item = self.head
  160.             cont = 0
  161.             while (item != None):
  162.                 if cont == indice:
  163.                     return item
  164.                
  165.                 item = item.proximo
  166.                 cont += 1
  167.                
  168.             return None
  169.  
  170. '''
  171. ## TESTES ##
  172. lista = LSE()
  173. lista.inserirFim( Nodo("ABC") )
  174. lista.inserirFim( Nodo("DEF") )
  175. #lista.inserirInicio( Nodo("123") )
  176. #lista.removerInicio()
  177.  
  178. #lista.clear()
  179. #lista.imprimirLadoALado()
  180. #print(len(lista))
  181.  
  182. #print(lista.buscar("ABC"))
  183. #print(lista.buscar("4252"))
  184.  
  185. lista.inserirFim(Nodo("XYZ"))
  186. removido = lista.remover("DEF")
  187. lista.imprimirLadoALado()
  188. print(len(lista))
  189.  
  190. achou = lista.get(1)
  191. achou.dado = "888"
  192. lista.imprimirLadoALado()
  193. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement