Stacks Using Queue

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 168:-Stacks Using Queue

You can implement a stack using two queues. The basic idea is to use one queue for push (or enqueue) operations and another queue for pop (or dequeue) operations. Here's a Python implementation of a stack using two queues:

pythonCopy code

from collections import deque class StackUsingQueues: def __init__(self): self.queue1 = deque() # Queue for push operations self.queue2 = deque() # Queue for pop operations def push(self, item): # Enqueue the item into queue1 self.queue1.append(item) def pop(self): if not self.queue1: return None # Stack is empty # Move all but the last element from queue1 to queue2 while len(self.queue1) > 1: self.queue2.append(self.queue1.popleft()) # Pop the last element from queue1 (equivalent to popping from stack) item = self.queue1.popleft() # Swap the names of queue1 and queue2 self.queue1, self.queue2 = self.queue2, self.queue1 return item def top(self): if not self.queue1: return None # Stack is empty # Move all but the last element from queue1 to queue2 while len(self.queue1) > 1: self.queue2.append(self.queue1.popleft()) # Get the last element from queue1 (equivalent to top of stack) item = self.queue1[0] # Move the element back to queue1 self.queue2.append(self.queue1.popleft()) # Swap the names of queue1 and queue2 self.queue1, self.queue2 = self.queue2, self.queue1 return item def is_empty(self): return not self.queue1 def size(self): return len(self.queue1)

In this implementation, the push operation enqueues an item into queue1. For the pop operation, we move all elements except the last one from queue1 to queue2. This makes the last element in queue1 (which corresponds to the top of the stack) available for popping. After popping, we swap the names of queue1 and queue2 to maintain the order.

The top operation is similar to the pop operation, but it leaves the last element in queue1 so that we can retrieve the top element without actually removing it.

Just like with the previous example, keep in mind that while this implementation demonstrates the concept of implementing a stack using two queues, it might not be the most efficient way to implement a stack in practice, especially if you're dealing with a large number of push and pop operations.

23. Queue - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?