Skip to content

Commit 445e869

Browse files
updated linked stack
1 parent ac89851 commit 445e869

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Linked Stack.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Node:
2-
"""Representação de um nó em Python3. Com ele é possível criar uma pilha encadeada"""
2+
"""Class to represent a node in Python3"""
33

44
def __init__(self, data):
5-
self.data = data # Valor do nó
6-
self.next = None # Próximo nó
5+
self.data = data # Node value
6+
self.next = None # Next node
77

88

99
class LinkedStack:
10-
"""Representação de uma pilha dinâmica encadeada em Python3"""
10+
"""Class to represent a linked stack in Python3"""
1111

1212
def __init__(self):
13-
self._top = None # Define o topo da pilha
14-
self._size = 0 # Tamanho atual da pilha
13+
self._top = None # Top of stack
14+
self._size = 0 # The stack size
1515

1616
def push(self, elem):
17-
"""Insere um elemento no topo da pilha"""
17+
"""Adds an element at the top of a stack"""
1818
if self._size == 0:
1919
self._top = Node(elem)
2020
else:
@@ -24,35 +24,35 @@ def push(self, elem):
2424
self._size += 1
2525

2626
def pop(self):
27-
"""Deleta um item do topo da pilha e retorna o seu valor"""
27+
"""Removes the topmost element of a stack"""
2828
if self._size == 0:
29-
raise Exception("Pilha vazia")
29+
raise Exception("Empty stack")
3030
elem, self._top = self._top.data, self._top.next
3131
self._size -= 1
3232
return elem
3333

3434
def top(self):
35-
"""retorna o elemento no topo da pilha"""
35+
"""Returns the element on the top of the stack but does not remove it"""
3636
if self._size == 0:
37-
raise Exception("Pilha vazia")
37+
raise Exception("Empty stack")
3838
return self._top.data
3939

4040
def empty(self):
41-
"""Verifica se a pilha está vazia"""
41+
"""Returns true if the stack is empty, otherwise, it returns false"""
4242
if self._size == 0:
4343
return True
4444
return False
4545

4646
def length(self):
47-
"""Retorna o tamanho da pilha"""
47+
"""Returns the size of stack"""
4848
return self._size
4949

5050
def __del__(self):
51-
"""Método destrutor invocado sempre que o código é finalizado"""
51+
"""Destructor method"""
5252

5353
def __str__(self):
54-
"""Representa a pilha excluindo os obj NoneType"""
55-
rep = "\033[1;34m" + "topo -> " + "\033[0;0m"
54+
"""Method for representing the stack, excluding NoneType objects (user)"""
55+
rep = "\033[1;34m" + "top -> " + "\033[0;0m"
5656
if self._size == 0:
5757
rep += "None"
5858
return rep
@@ -66,4 +66,5 @@ def __str__(self):
6666
return rep
6767

6868
def __repr__(self):
69+
"""Method for representing the stack, excluding NoneType objects (developer)"""
6970
return str(self)

0 commit comments

Comments
 (0)