forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_using_two_queues.py
50 lines (38 loc) · 970 Bytes
/
stack_using_two_queues.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
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if not self.is_empty():
return self.items.pop()
else:
return "Stack is empty"
def peek(self):
if not self.is_empty():
return self.items[-1]
else:
return "Stack is empty"
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
# Create a stack
stack = Stack()
# PUSH operation
stack.push(1)
stack.push(2)
stack.push(3)
# Display the stack
print("Stack:", stack.items)
# POP operation
popped_item = stack.pop()
print("Popped item:", popped_item)
# Display the updated stack
print("Stack after POP:", stack.items)
# PEEK operation
top_item = stack.peek()
print("Top item (PEEK):", top_item)
# Check the size of the stack
stack_size = stack.size()
print("Stack size:", stack_size)