How to Implement Queue in Python
In this tutorial, we will learn how to program “How to Implement Queue in Python.” The main objective is to understand and implement queue operations in a clear and practical way. This tutorial will guide you step by step through the process of creating a queue and performing fundamental operations such as enqueue, dequeue, and display. By the end of this tutorial, you will have a solid understanding of how queues work in Python, helping you strengthen your problem-solving abilities and improve your overall coding skills.
This topic is straightforward and easy to understand. By simply following the instructions provided, you will be able to complete it with ease. The program will guide you step by step through the process of checking whether an expression is correctly parenthesized. So, let’s dive into the coding process and start implementing the solution!
Getting Started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Creating Main Function
This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.- class Queue:
- def __init__(self):
- self.items = []
- def is_empty(self):
- return self.items == []
- def enqueue(self, data):
- self.items.append(data)
- def dequeue(self):
- return self.items.pop(0)
- def display(self):
- if self.is_empty():
- print("Queue is empty.")
- else:
- print("Queue elements (front → rear):")
- for item in self.items:
- print(item, end=" ")
- print()
- # MAIN PROGRAM
- while True:
- print("\n================= Implement Queue =================\n")
- q = Queue()
- while True:
- print("\nMenu:")
- print("enqueue <value>")
- print("dequeue")
- print("display")
- print("quit")
- do = input("What would you like to do? ").split()
- if not do:
- continue
- operation = do[0].lower()
- if operation == "enqueue":
- if len(do) != 2:
- print("Usage: enqueue <value>")
- else:
- q.enqueue(int(do[1]))
- print("Value enqueued.")
- elif operation == "dequeue":
- if q.is_empty():
- print("Queue is empty.")
- else:
- print("Dequeued value:", q.dequeue())
- elif operation == "display":
- q.display()
- elif operation == "quit":
- break
- else:
- print("Invalid command.")
- # Try again option
- opt = input("\nDo you want to try again? (yes/no): ").strip().lower()
- if opt == "no":
- print("Exiting program...")
- break
- elif opt != "yes":
- print("Invalid choice. Exiting program...")
- break
This program demonstrates a simple queue implementation using a Python list and provides a menu-driven interface for interacting with it. The `Queue` class includes basic operations such as checking whether the queue is empty, adding elements to the rear using `enqueue`, removing elements from the front using `dequeue`, and displaying the current contents of the queue in FIFO order. The main loop allows users to repeatedly perform these operations through text commands, showing how elements move through the queue from front to rear. The program continues running until the user chooses to quit, making it a clear and practical example of how a queue data structure works.
Output:
There you have it we successfully created How to Implement Queue in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
More Tutorials for Python Language