
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python Program to Implement Queues using Stacks
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
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.
- Related Articles
- Python Program to Implement Stack Using Two Queues
- C++ Program to Implement Stack Using Two Queues
- C++ Program to Implement Queue Using Two Stacks
- Implement Stack using Queues in C++
- How to implement enqueue and dequeue using only two stacks in JavaScript?
- C++ Program to Evaluate an Expression using Stacks
- Java Program to Reverse a String Using Stacks
- Golang program to reverse a string using stacks
- Python Program to Implement Stack using One Queue
- C++ Program to Check for balanced paranthesis by using Stacks
- Using List as Stack and Queues in Python
- Python Program to Implement a Stack using Linked List
- Python Program to Implement Binary Tree using Linked List
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- IPC using Message Queues
