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

2 Comments

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

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

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 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

@sciaku1
sciaku1 Jan 11, 2024 at 3: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 About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support