Brunoroman

PilaNodo v1

May 9th, 2022
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. from dataclasses import dataclass
  2. from typing import Any
  3.  
  4. class StackNodo():
  5.     @dataclass
  6.     class _Node:
  7.         value: Any
  8.         prev: "_Node"
  9.  
  10.     __slots__=["_counter","_top"]
  11.  
  12.     def __init__(self,futurevalue=None):
  13.         self._top=None
  14.         self._counter=0
  15.         if futurevalue is not None:
  16.             for value in futurevalue:
  17.                 self.push(futurevalue)
  18.  
  19.     def __eq__(self,other):
  20.         x=self._top
  21.         y=other._top
  22.         while x is not None and y is not None:
  23.             if x.value != y.value:
  24.                 return False
  25.             x=x.prev
  26.             y=y.prev
  27.         return x is None and y is None
  28.  
  29.     def __len__(self):
  30.         return(self._counter)
  31.  
  32.     def lenrecur(self):
  33.         if self._top
  34.  
  35.     def __repr__(self):
  36.         values=[]
  37.         node_aux=self._top
  38.         while node_aux is not None:
  39.             values.insert(0,node_aux.value)
  40.             node_aux=node_aux.prev
  41.         return "StackNodo(["+", ".join(repr(x) for x in values)+" ]"+")"
  42.  
  43.     def empty(self):
  44.         return (self._counter==0)
  45.  
  46.     @property
  47.     def top(self):
  48.         assert not self.empty(), "La pila esta vacia"
  49.         return(self._top.value)
  50.  
  51.     def push(self,futurevalue):
  52.         self._top=StackNodo._Node(futurevalue,self._top)
  53.         self._counter+=1
  54.  
  55.     def pop(self):
  56.         assert not self.empty(), "La pila esta vacia"
  57.         aux=self._top.value
  58.         self._top=self._top.prev
  59.         self._counter-=1
  60.         return(aux)
  61.  
  62.     def clear(self):
  63.         self._counter=0
  64.         self._top=None
  65.  
  66.     def copy(self,other):
  67.         aux=""
  68.         if(self.empty()):
  69.             return None
  70.         else:
  71.             aux=self.pop()
  72.             self.copy(other)
  73.             self.push(aux)
  74.             other.push(aux)
  75.             return(other)
  76.        
  77.  
Advertisement
Add Comment
Please, Sign In to add comment