Skip to content

added stack_using_two_queues.py #10079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions data_structures/stacks/stack_using_two_queues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)