From 2178db61b0cbdf4aa39ee9ab90411f0835d9f7a7 Mon Sep 17 00:00:00 2001 From: Samarpreet Singh <99521823+XxPython28xX@users.noreply.github.com> Date: Sun, 8 Oct 2023 12:29:41 +0530 Subject: [PATCH] added stack_using_two_queues.py --- .../stacks/stack_using_two_queues.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 data_structures/stacks/stack_using_two_queues.py diff --git a/data_structures/stacks/stack_using_two_queues.py b/data_structures/stacks/stack_using_two_queues.py new file mode 100644 index 000000000000..c9bd06dadbf9 --- /dev/null +++ b/data_structures/stacks/stack_using_two_queues.py @@ -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)