-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.py
43 lines (36 loc) · 1.03 KB
/
stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from node import Node
class Stack:
def __init__(self):
self.top = None
self._size = 0
def push(self, elem):
#insere um elemento na pilha
node = Node(elem)
node.next = self.top
self.top = node
self._size = self._size + 1
def pop(self):
# remove o elemento do topo da pilha
if self._size > 0:
node = self.top
self.top = self.top.next
self._size = self._size - 1
return node.data
#raise IndexError("The stack is empty")
def peek(self):
# retorna o topo sem remover
if self._size > 0:
return self.top.data
raise IndexError("The stack is empty")
def __len__(self):
"""Retorna o tamanho da lista"""
return self._size
def __repr__(self):
r = ""
pointer = self.top
while(pointer):
r = r + str(pointer.data) + " "
pointer = pointer.next
return r
def __str__(self):
return self.__repr__()