Brunoroman

Implementacion Lista Simple

Apr 21st, 2022 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. from dataclasses import dataclass
  2. from typing import Any
  3.  
  4. class lsimple():
  5.  
  6.     @dataclass
  7.     class _Nodo:
  8.         valor: Any =None
  9.         prox: "_Nodo" =None
  10.  
  11.     class _NodoFantasma:
  12.         prox: "_Nodo" =None
  13.  
  14.     __slots__=["_contador","_back"]
  15.  
  16.     def __init__(self,valor=None):
  17.         self._contador=0
  18.         self._back=lsimple._NodoFantasma()
  19.         self._back.prox=self._back
  20.         if valor is not None:
  21.             for x in valor:
  22.                 self.append_front(valor)
  23.  
  24.     def __eq__(self,other):
  25.         c1=self.begin()
  26.         c2=other.begin()
  27.         c1.advance()
  28.         c2.advance()
  29.         while c1 != self.end() and c2 != other.end():
  30.             if c1.valor != c2.valor:
  31.                 return False
  32.             c1.advance()
  33.             c2.advance()
  34.         return c1 == self.end() and c2 == other.end()
  35.        
  36.     def __len__(self):
  37.         return self._contador
  38.  
  39.     def __iter__(self):
  40.         return lsimple.Iterador(self._back.prox,self._back)
  41.  
  42.     def __repr__(self):
  43.         return "ListaSimple(["+", ".join(repr(x) for x in self)+"]"+")"
  44.  
  45.     def empty(self):
  46.         return self._contador==0
  47.  
  48.     @property
  49.     def primero(self):
  50.         x=self.begin()
  51.         x.advance()
  52.         return x.valor
  53.    
  54.     @property
  55.     def ultimo(self):
  56.         return self.end().valor
  57.    
  58.     def begin(self):
  59.         return lsimple.Coordenada(self._back)
  60.    
  61.     def end(self):
  62.         aux=lsimple.Coordenada(self._back)
  63.         aux.advance()
  64.         while aux._nodo.prox is not self._back:
  65.             aux.advance()
  66.         return aux    
  67.            
  68.     def insert(self,coordenada,valor):
  69.         nodo_actual=coordenada._nodo
  70.         nuevo=lsimple._Nodo(valor)
  71.         nuevo.prox=nodo_actual.prox
  72.         nodo_actual.prox=nuevo
  73.  
  74.     def popvalue(self,valor):
  75.         coor_busqueda=self.begin()
  76.         coor_busqueda.advance()
  77.         coor2=self.begin()
  78.         while coor_busqueda._nodo != self._back:
  79.             if coor_busqueda._nodo.valor == valor:
  80.                 coor2._nodo.prox=coor_busqueda._nodo.prox
  81.                 return(valor)
  82.             coor2.advance()
  83.             coor_busqueda.advance()
  84.         assert not self.existe(),"No existe al valor a borrar"
  85.    
  86.     def append_back(self,valor):
  87.         self._contador+=1
  88.         self.insert(self.end(),valor)
  89.  
  90.     def append_front(self,valor):
  91.         self._contador+=1
  92.         self.insert(self.begin(),valor)
  93.        
  94.     def clear(self):
  95.         self._back.prox=self._back
  96.        
  97.     #def copy(self):
  98.        
  99.     #def reverse(self):
  100.        
  101.     def existe(self,valor):
  102.         coor_busqueda=self.begin()
  103.         coor_busqueda.advance()
  104.         while coor_busqueda._nodo != self._back:
  105.             if coor_busqueda._nodo.valor == valor:
  106.                 return True
  107.             coor_busqueda.advance()
  108.         return False
  109.              
  110.     class Coordenada():
  111.         __slots__=["_nodo"]
  112.        
  113.         def __init__(self,coord_nodo):
  114.             if isinstance(coord_nodo,lsimple.Coordenada):
  115.                 self._nodo=coord_nodo._nodo
  116.             else:
  117.                 self._nodo=coord_nodo
  118.                
  119.         def __eq__(self,other):
  120.             return self._nodo is other._nodo
  121.        
  122.         def valor(self):
  123.             return self._nodo.valor
  124.            
  125.         def advance(self):
  126.             self._nodo=self._nodo.prox
  127.                
  128.         def next(self):
  129.             return lsimple.Coordenada(self_nodo).advance()
  130.        
  131.     class Iterador():
  132.        
  133.         __slots__=["_ini","_fin"]
  134.  
  135.         def __init__(self,inicio,final):
  136.             self._ini=inicio
  137.             self._fin=final
  138.  
  139.         def __iter__(self):
  140.             return self
  141.  
  142.         def __next__(self):
  143.             if self._ini is self._fin:
  144.                 raise StopIteration
  145.             value=self._ini.valor
  146.             self._ini=self._ini.prox
  147.             return value
  148.        
  149.  
Add Comment
Please, Sign In to add comment