Queue Using Stacks

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 167:-Queue Using Stacks

Using two stacks, you can implement a queue. The basic idea is to use one stack for enqueueing elements and the other stack for dequeueing elements. Here's a Python implementation of a queue using two stacks:

pythonCopy code

class QueueUsingStacks: def __init__(self): self.stack_enqueue = [] # Stack for enqueueing self.stack_dequeue = [] # Stack for dequeueing def enqueue(self, item): self.stack_enqueue.append(item) def _transfer(self): while self.stack_enqueue: self.stack_dequeue.append(self.stack_enqueue.pop()) def dequeue(self): if not self.stack_dequeue: self._transfer() if self.stack_dequeue: return self.stack_dequeue.pop() def front(self): if not self.stack_dequeue: self._transfer() if self.stack_dequeue: return self.stack_dequeue[-1] def is_empty(self): return not (self.stack_enqueue or self.stack_dequeue) def size(self): return len(self.stack_enqueue) + len(self.stack_dequeue)

In this implementation, the enqueue operation simply adds elements to the stack_enqueue. For the dequeue operation, if stack_dequeue is empty, we transfer elements from stack_enqueue to stack_dequeue in reverse order. This way, the front element becomes the top of stack_dequeue, and we can perform a pop operation to dequeue the element.

Note that this implementation has a time complexity of O(1) for the enqueue operation and an amortized time complexity of O(1) for the dequeue operation. The _transfer method has a time complexity of O(n) in the worst case, but it's spread out over multiple enqueue operations, resulting in an amortized O(1) time complexity for each dequeue operation.

Keep in mind that while this implementation demonstrates the concept of implementing a queue using two stacks, it might not be the most efficient way to implement a queue in practice, especially if you're dealing with a large number of enqueue and dequeue operations. In real-world scenarios, using a linked list-based implementation or other optimized data structures might be more efficient.

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?