Python Program to Implement Stack Using Two Queues

PythonServer Side ProgrammingProgramming

When it is required to implement a stack using two queues, a ‘Stack_structure’ class is required along with a Queue_structure class. Respective methods are defined in these classes to add and delete values from the stack and queue respectively.

Below is a demonstration of the same −

Example

 Live Demo

class Stack_structure:
   def __init__(self):
      self.queue_1 = Queue_structure()
      self.queue_2 = Queue_structure()

   def check_empty(self):
      return self.queue_2.check_empty()

   def push_val(self, data):
      self.queue_1.enqueue_operation(data)
      while not self.queue_2.check_empty():
         x = self.queue_2.dequeue_operation()
         self.queue_1.enqueue_operation(x)
      self.queue_1, self.queue_2 = self.queue_2, self.queue_1

   def pop_val(self):
      return self.queue_2.dequeue_operation()

class Queue_structure:
   def __init__(self):
      self.items = []
      self.size = 0

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

   def enqueue_operation(self, data):
      self.size += 1
      self.items.append(data)

   def dequeue_operation(self):
      self.size -= 1
      return self.items.pop(0)

   def size_calculate(self):
      return self.size

my_instance = Stack_structure()

print('Menu')
print('push <value>')
print('pop')
print('quit')

while True:
   my_input = input('What operation would you like to perform ? ').split()

   operation = my_input[0].strip().lower()
   if operation == 'push':
      my_instance.push_val(int(my_input[1]))
   elif operation == 'pop':
      if my_instance.check_empty():
         print('Stack is empty.')
      else:
         print('The deleted value is: ', my_instance.pop_val())
   elif operation == 'quit':
      break

Output

Menu
push <value>
pop
quit
What operation would you like to perform ? push 56
What operation would you like to perform ? push 34
What operation would you like to perform ? push 78
What operation would you like to perform ? push 90
What operation would you like to perform ? pop
The deleted value is: 90
What operation would you like to perform ? quit

Explanation

  • A ‘Stack_structure’ class is created that initializes an empty list.

  • A ‘check_empty’ method is defined to see if a stack is empty.

  • Another method named ‘push_val’ is defined that adds elements to the stack.

  • Another method named ‘pop_val’ is defined that deletes elements from the stack.

  • A ‘Queue_structure’ class is created that initializes an empty list and assigns size of list as 0.

  • A ‘check_empty’ method is defined to see if a queue is empty.

  • Another method named ‘enqueue_operation’ is defined that adds elements to the queue.

  • Another method named ‘dequeue_operation’ is defined that deletes elements from the queue.

  • Another method named ‘size_calculate’ is defined, that determines the size of the queue.

  • Two instances of this ‘Queue_structure’ are defined.

  • Four options are given- Menu, push, pop, and quit.

  • Based on the inut given by the user, operations are performed on the elements of stack.

  • The output is displayed on the console.

raja
Published on 15-Apr-2021 12:44:34
Advertisements