Table of Contents
Example Description
Dive into the world of Python queues with this comprehensive guide. Discover the implementation of essential queue operations like enqueue and dequeue. Explore practical examples showcasing how to manage queues effectively, along with insights into queue sizes and emptiness checks.

Introduction:

Queues play a pivotal role in managing data structures. This tutorial delves into the implementation of queues in Python, covering key operations like enqueue and dequeue. Through real-world examples, you’ll learn how to create, manipulate, and efficiently manage queues, enhancing your Python programming skills along the way.

Code:

def isEmpty(myQueue):
    return len(myQueue) == 0

def enqueue(myQueue, element):
    myQueue.append(element)

def dequeue(myQueue):
    if isEmpty(myQueue):
        print('Queue is empty')
        return None
    else:
        return myQueue.pop(0)

def size(myQueue):
    return len(myQueue)

# Initialize the queue
myQueue = list()

# each person to be assigned a code as P1, P2, P3,...
element = input("Enter person’s code to enter in the queue: ")
enqueue(myQueue, element)

element = input("Enter person’s code for insertion in the queue: ")
enqueue(myQueue, element)

print("Person removed from the queue is:", dequeue(myQueue))
print("Number of people in the queue is:", size(myQueue))

element = input("Enter person’s code to enter in the queue: ")
enqueue(myQueue, element)

element = input("Enter person’s code to enter in the queue: ")
enqueue(myQueue, element)

element = input("Enter person’s code to enter in the queue: ")
enqueue(myQueue, element)

print("Now we are going to remove remaining people from the queue")
while not isEmpty(myQueue):
    print("Person removed from the queue is", dequeue(myQueue))
print("queue is empty")

Logic:

  1. Define the isEmpty function to check if a queue is empty.
  2. Define the enqueue function to add an element to the queue.
  3. Define the dequeue function to remove and return the front element from the queue.
  4. Define the size function to calculate the number of elements in the queue.
  5. Initialize an empty myQueue list.
  6. Use the enqueue function to add elements (person’s codes) to the queue.
  7. Use the dequeue function to remove a person from the front of the queue and print their code.
  8. Use the size function to display the number of people in the queue.
  9. Continue adding elements to the queue.
  10. Use a loop to remove remaining people from the queue using the dequeue function.

Output:

>>Enter person’s code to enter in the queue: P1

>>Enter person’s code for insertion in the queue: P2

>>Person removed from the queue is: P1

>>Number of people in the queue is: 1

>>Enter person’s code to enter in the queue: P3

>>Enter person’s code to enter in the queue: P4

>>Enter person’s code to enter in the queue: P5

>>Now we are going to remove remaining people from the queue

>>Person removed from the queue is P2

>>Person removed from the queue is P3

>>Person removed from the queue is P4

>>Person removed from the queue is P5

>>queue is empty