-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.py
39 lines (27 loc) · 963 Bytes
/
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
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
def push(self, item):
self.items.append(item)
def pop(self):
if not self.isEmpty():
return self.items.pop()
def peek(self):
if not self.isEmpty():
return self.items[-1]
# Testing the stack implementation
if __name__ == "__main__":
my_stack = Stack()
print("Is the stack empty?", my_stack.isEmpty()) # Output: True
my_stack.push(10)
my_stack.push(20)
my_stack.push(30)
print("Size of the stack:", my_stack.size()) # Output: 3
print("Is the stack empty?", my_stack.isEmpty()) # Output: False
print("Top item of the stack:", my_stack.peek()) # Output: 30
print("Popped item:", my_stack.pop()) # Output: 30
print("Size of the stack after popping:", my_stack.size()) # Output: 2