Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from dataclasses import dataclass
- from typing import Any
- class StackNodo():
- @dataclass
- class _Node:
- value: Any
- prev: "_Node"
- __slots__=["_counter","_top"]
- def __init__(self,futurevalue=None):
- self._top=None
- self._counter=0
- if futurevalue is not None:
- for value in futurevalue:
- self.push(futurevalue)
- def __eq__(self,other):
- x=self._top
- y=other._top
- while x is not None and y is not None:
- if x.value != y.value:
- return False
- x=x.prev
- y=y.prev
- return x is None and y is None
- def __len__(self):
- return(self._counter)
- def lenrecur(self):
- if self._top
- def __repr__(self):
- values=[]
- node_aux=self._top
- while node_aux is not None:
- values.insert(0,node_aux.value)
- node_aux=node_aux.prev
- return "StackNodo(["+", ".join(repr(x) for x in values)+" ]"+")"
- def empty(self):
- return (self._counter==0)
- @property
- def top(self):
- assert not self.empty(), "La pila esta vacia"
- return(self._top.value)
- def push(self,futurevalue):
- self._top=StackNodo._Node(futurevalue,self._top)
- self._counter+=1
- def pop(self):
- assert not self.empty(), "La pila esta vacia"
- aux=self._top.value
- self._top=self._top.prev
- self._counter-=1
- return(aux)
- def clear(self):
- self._counter=0
- self._top=None
- def copy(self,other):
- aux=""
- if(self.empty()):
- return None
- else:
- aux=self.pop()
- self.copy(other)
- self.push(aux)
- other.push(aux)
- return(other)
Advertisement
Add Comment
Please, Sign In to add comment