-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstack_with_list.py
52 lines (36 loc) · 937 Bytes
/
stack_with_list.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
45
46
47
48
49
50
51
52
#methods 1. pop, push, peek, isEmpty, delete
class Stack:
def __init__(self):
super().__init__()
self.list = []
def __str__(self):
values = self.list.reverse()
values = [str(x) for x in self.list]
return '\n'.join(values)
def push(self, value):
self.list.append(value)
def pop(self):
if not self.isEmpty():
return self.list.pop()
def peek(self):
if not self.isEmpty():
return self.list[-1]
def isEmpty(self):
return self.list == []
def delete(self):
self.list = None
stack = Stack()
# Check if stack is empty
print(stack.isEmpty())
# Adding values to stack
stack.push(2)
stack.push(5)
stack.push(8)
# Check if stack is empty again
print(stack.isEmpty())
# Peek at element from stack
print(stack.peek())
# Pop element from stack
print(stack.pop())
print(stack.peek())
stack.delete()