Theory of Intelligence
11TH•Computer Science•Computational Structures•Question 3

Solution

Write a simple program to implement a queue (insertion and deletion).

Solution

A queue follows FIFO (First-In, First-Out). Enqueue adds at the back; dequeue removes from the front.

PYTHON
# Built-in module to implement queues in Python
from queue import Queue

# Create a new queue
q = Queue()

# Add people to the queue (Enqueue)
q.put("Ahmed") # Adds Ahmed to the end of the queue
q.put("Fatima") # Adds Fatima to the end of the queue

# View the person at the front of the queue (Peek)
front_person = q.queue[0] # Looks at the person at the front without removing them
print(front_person)

# Remove a person from the front of the queue(Dequeue)
removed_person = q.get() # Removes and returns the person at the front of the queue
print(removed_person)

# Add another person to the queue (Enqueue)
q.put("Sara") # Adds Sara to the end of the queue

# View the updated queue
updated_queue = list(q.queue)
print(updated_queue)

Ahmed and Fatima are enqueued. Peek shows Ahmed at the front. Dequeue removes Ahmed. Sara is enqueued. The remaining line is Fatima, then Sara.

Discuss theoretical concepts, mathematical formulas, and architectural foundations.

Supports inline code with `backticks`
No comments yet. Be the first to start the discussion!