-
Notifications
You must be signed in to change notification settings - Fork 0
/
lecture020_Stack.py
44 lines (35 loc) · 1.18 KB
/
lecture020_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
44
class Stack:
def __init__(self): # Função onde eu inicio a pilha
self.stack = []
def push(self, e): # Função onde eu coloco os elementos
self.stack.append(e)
def pop(self): # Função onde eu retiro os elementos
if not self.empty():
self.stack.pop(len(self.stack)-1)
def top(self): # Função que retorna o último elemento da pilha
if not self.empty():
return self.stack[-1]
print('Stack empty')
def empty(self):
if(len(self.stack)==0):
return True
return False
def show(self):
if not self.empty():
return self.stack
def length(self): # Função que mostra o tamanho da pilha
return len(self.stack)
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.push(6)
stack.push(7)
print('\nThe first element in the stack is: {}.'.format(stack.top()),'\n')
stack.pop()
print('After "pop" element.')
print('The first element in the stack is: {}.'.format(stack.top()),'\n')
print('Number of elements in the stack is: {}.'.format(stack.length()),'\n')
print('Elements in the stack is: {}.'.format(stack.show()),'\n')