Stack-based queues

Queues can also be implemented using two stacks. We initially set two instance variables to create an empty queue upon initialization. These are the stacks that will help us to implement a queue. The stacks, in this case, are simply Python lists that allow us to call push and pop methods on them, which eventually allow us to get the functionality of enqueue and dequeue operations. Here is the Queue class:

class Queue: 
def __init__(self):
self.inbound_stack = []
self.outbound_stack = []

The inbound_stack is only used to store elements that are added to the queue. No other operation can be performed on this stack.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset