Python Program to Implement Queues using Stacks

PythonServer Side ProgrammingProgramming

When it is required to implement a queue using a stack, a queue class can be defined, where two stack instances can be defined. Different operations can be performed on the queue that are defined as methods in this class.

Below is a demonstration of the same −

Example

 Live Demo

class Queue_structure:
   def __init__(self):
      self.in_val = Stack_structure()
      self.out_val = Stack_structure()

   def check_empty(self):
      return (self.in_val.check_empty() and self.out_val.check_empty())

   def enqueue_operation(self, data):
      self.in_val.push_operation(data)

   def dequeue_operation(self):
      if self.out_val.check_empty():
         while not self.in_val.check_empty():
            deleted_val = self.in_val.pop_operation()
            self.out_val.push_operation(deleted_val)
      return self.out_val.pop_operation()

class Stack_structure:
   def __init__(self):
      self.items = []

   def check_empty(self):
      return self.items == []

   def push_operation(self, data):
      self.items.append(data)

   def pop_operation(self):
      return self.items.pop()

my_instance = Queue_structure()

while True:
   print('enqueue <value>')
   print('dequeue')
   print('quit')
   my_input = input('What operation would you like to perform ?').split()

   operation = my_input[0].strip().lower()
   if operation == 'enqueue':
      my_instance.enqueue_operation(int(my_input[1]))
   elif operation == 'dequeue':
      if my_instance.check_empty():
         print('The queue is empty')
      else:
         deleted_elem = my_instance.dequeue_operation()
         print('The deleted element is : ', int(deleted_elem))
   elif operation == 'quit':
      break

Output

enqueue <value>
dequeue
quit
What operation would you like to perform ?enqueue 45
enqueue <value>
dequeue
quit
What operation would you like to perform ?enqueue 23
enqueue <value>
dequeue
quit
What operation would you like to perform ?enqueue 78
enqueue <value>
dequeue
quit
What operation would you like to perform ?dequeue
The deleted element is : 45
enqueue <value>
dequeue
quit
What operation would you like to perform ?quit

Explanation

  • A ‘Queue_structure’ is defined, that defines two instances of Stack.

  • It has a method named ‘check_empty’ that checks to see if the queue is empty.

  • Another method named ‘enqueue_operation’ is defined, that helps add elements to the queue.

  • Another method named ‘dequeue_operation’ is defined, that deletes an element from the queue.

  • Another ‘Stack_structure’ class is created.

  • It initializes an empty list.

  • It has a method named ‘check_empty’ that checks to see if the stack is empty.

  • Another method named ‘push_operation’ is defined, that helps add elements to the queue.

  • Another method named ‘pop_operation’ is defined, that deletes an element from the queue.

  • A ‘Queue_structure’ instance is created.

  • Three options are given- enqueue, dequeue, and quit.

  • Based on the option given by user, the operations are performed, and relevant output is displayed on the console.

raja
Published on 15-Apr-2021 12:46:28
Advertisements