0% found this document useful (0 votes)
10 views25 pages

Queue and Deque Operations in Python

The document provides an overview of queue and deque data structures, highlighting their operations and characteristics. It includes multiple-choice questions, fill-in-the-blank exercises, and short answer questions to assess understanding of FIFO principles, operations, and differences between queues and stacks. Additionally, it discusses the implementation of these structures in Python and their applications in real-world scenarios.

Uploaded by

Navneeth sunil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Queue and Deque Operations in Python

The document provides an overview of queue and deque data structures, highlighting their operations and characteristics. It includes multiple-choice questions, fill-in-the-blank exercises, and short answer questions to assess understanding of FIFO principles, operations, and differences between queues and stacks. Additionally, it discusses the implementation of these structures in Python and their applications in real-world scenarios.

Uploaded by

Navneeth sunil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Summary Points

• Queue: FIFO, insertion at rear, deletion from front.


• Deque: Insertion & deletion possible from both ends.
• Python lists can implement both structures easily.
• Additional operations: isEmpty, peek, size, getFront, getRear.

MULTIPLE CHOICE QUESTION (MCQs)

1. What principle does a queue follow?


A) LIFO
B) FILO
C) FIFO
D) Random
Answer: C) FIFO
2. In a queue, the element is inserted at the:
A) Middle
B) Front
C) Rear
D) Random position
Answer: C) Rear
3. What happens when you try to dequeue from an empty queue?
A) Overflow
B) Error
C) Underflow
D) Crash
Answer: C) Underflow
4. Which operation is used to add an element to a queue?
A) Pop
B) Dequeue
C) Peek
D) Enqueue
Answer: D) Enqueue
5. In a queue implemented with a Python list, [Link](0) removes the element from:
A) End
B) Middle
C) Front
D) None

Answer: C) Front

6. What does the isEmpty() function check in a queue?


A) Queue is full
B) Queue has only one item
C) Queue has elements
D) Queue is empty

Answer: D) Queue is empty

7. In the context of a printer, how are print jobs managed?


A) LIFO
B) FIFO
C) Random
D) Priority only

Answer: B) FIFO

8. Which Python function is used to insert an element at the rear in a queue?


A) insert()
B) append()
C) pop()
D) push()

Answer: B) append()

9. The peek() operation in queue is used to:


A) Delete the last element
B) Check if queue is full
C) View the front element without deleting
D) View the rear element

Answer: C) View the front element without deleting

10. In the bank queue example, which operation is used when a person enters the queue?
A) Dequeue
B) Append
C) Peek
D) Enqueue

Answer: D) Enqueue
11. Which of the following is NOT required in Python while implementing a queue using a list?
A) isFull()
B) isEmpty()
C) peek()
D) dequeue()
Answer: A) isFull()
12. What is a deque?
A) A type of priority queue
B) Queue with limited size
C) Double-ended queue
D) Stack with extra features
Answer: C) Double-ended queue
13. Which operation removes an element from the rear of a deque?
A) deletionFront
B) pop(0)
C) deletionRear
D) enqueue
Answer: C) deletionRear
14. If you insert and delete elements from the same end in a deque, it behaves as a:
A) Queue
B) Stack
C) List
D) Tree
Answer: B) Stack
15. What is the output of getRear(myDeque) if myDeque = [23, 45, 67]?
A) 23
B) 45
C) 67
D) Error
Answer: C) 67
16. In palindrome checking using deque, characters are compared from:
A) Front only
B) Rear only
C) Front and Rear
D) Middle
Answer: C) Front and Rear

17. What does [Link](0, element) do in deque?


A) Inserts at rear
B) Inserts at front
C) Replaces the first element
D) Deletes the first element

Answer: B) Inserts at front

18. In Python, what will pop() do if no index is given in a list?


A) Removes front
B) Removes last
C) Removes middle
D) None of the above

Answer: B) Removes last

19. In a multitasking OS, how are jobs scheduled if FIFO is used?


A) The latest job is processed first
B) Jobs are randomly selected
C) Jobs are processed in the order they arrive
D) Only one job is processed permanently

Answer: C) Jobs are processed in the order they arrive

20. Which operation would you perform to add an element at the front of a deque?
A) insertRear()
B) append()
C) insertFront()
D) enqueue()

Answer: C) insertFront()

21.
Assertion (A): In a queue, the first element added is the first one to be removed.
Reason (R): Queue follows Last-In-First-Out (LIFO) order.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: C) A is true, but R is false.

22.
Assertion (A): Enqueue operation in a queue adds an element to the rear end.
Reason (R): Python’s append() function adds elements at the end of a list.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: A) Both A and R are true, and R is the correct explanation of A.

23.
Assertion (A): Deleting from an empty queue results in an Overflow.
Reason (R): Overflow happens when insertion is attempted in a full queue.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is false, but R is true.
D) Both A and R are false.

Answer: C) A is false, but R is true.

24.
Assertion (A): isEmpty() is necessary before performing dequeue in a queue.
Reason (R): Dequeue should only be performed when the queue is not empty to avoid underflow.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.

Answer: A) Both A and R are true, and R is the correct explanation of A.

25.
Assertion (A): Queue can be implemented using Python’s tuple.
Reason (R): Tuples are mutable and support deletion and insertion at both ends.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is false.

Answer: D) A is false, but R is false.

26.
Assertion (A): Web servers use queues to manage multiple requests.
Reason (R): A server can only serve one request at a time and queues allow orderly handling.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: A) Both A and R are true, and R is the correct explanation of A.

27.
Assertion (A): peek() operation modifies the queue by removing the front element.
Reason (R): peek() is similar to dequeue().
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.

Answer: D) Both A and R are false.

28.
Assertion (A): A deque allows insertion and deletion from both ends.
Reason (R): Deque can be used to simulate both queue and stack behavior.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: A) Both A and R are true, and R is the correct explanation of A.

29.
Assertion (A): A queue implemented in Python using a list can never overflow.
Reason (R): Lists in Python have dynamic sizing and can grow as needed.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: A) Both A and R are true, and R is the correct explanation of A.

30.
Assertion (A): In a normal queue, insertion and deletion happen from opposite ends.
Reason (R): This differentiates a queue from a stack where both happen from the same end.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.
Answer: A) Both A and R are true, and R is the correct explanation of A.
31.
Assertion (A): insertFront() in deque adds an element to index 0.
Reason (R): In Python, insert(0, element) places the element at the front of a list.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.
Answer: A) Both A and R are true, and R is the correct explanation of A.
32.
Assertion (A): Deletion from both ends in a deque is not allowed.
Reason (R): Deque behaves like a normal queue.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.

Answer: D) Both A and R are false.


33.
Assertion (A): Palindrome checking can be efficiently done using a deque.
Reason (R): Deque allows comparing characters from front and rear ends.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.
Answer: A) Both A and R are true, and R is the correct explanation of A.
34.
Assertion (A): getFront() and getRear() both modify the contents of a deque.
Reason (R): They use pop() and insert() operations.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.
Answer: D) Both A and R are false.
35.
Assertion (A): Job scheduling in operating systems can be based on queues.
Reason (R): FIFO strategy ensures fair processing order.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: A) Both A and R are true, and R is the correct explanation of A.

36.
Assertion (A): In deque, deletionRear() uses pop() without any arguments.
Reason (R): This removes the last element in the list.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: A) Both A and R are true, and R is the correct explanation of A.

37.
Assertion (A): All operations in deque can be implemented using only append() and pop() in Python.
Reason (R): Python lists allow only rear-end insertion and deletion.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.

Answer: C) A is true, but R is false.

38.
Assertion (A): size() function returns the number of elements in a queue.
Reason (R): It uses Python’s built-in len() function.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.

Answer: A) Both A and R are true, and R is the correct explanation of A.

39.
Assertion (A): A deque can behave as both a queue and a stack.
Reason (R): It supports insertion and deletion from both ends.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) A is false, but R is true.
Answer: A) Both A and R are true, and R is the correct explanation of A.
40.
Assertion (A): Overflow can happen in a queue implemented using Python lists.
Reason (R): Python lists are statically sized like arrays.
A) Both A and R are true, and R is the correct explanation of A.
B) Both A and R are true, but R is not the correct explanation of A.
C) A is true, but R is false.
D) Both A and R are false.
Answer: D) Both A and R are false.

FILL IN THE BLANKS

1. A queue is a linear list of elements in which insertion takes place at the and deletion at
the .
Answer: rear, front
2. Queue follows the strategy, where the first element inserted is the first one to be removed.
Answer: FIFO (First In First Out)
3. The operation to insert an element into a queue is called .
Answer: enqueue
4. The operation to remove an element from a queue is called .
Answer: dequeue
5. Attempting to remove an element from an empty queue results in an error called .
Answer: underflow
6. In Python, the method is used to add an element at the rear of a list.
Answer: append()
7. A queue implemented using Python list does not require an function because the list size
is dynamic.
Answer: isFull
8. The function is used to check if the queue contains no elements.
Answer: isEmpty()
9. To view the front element of a queue without removing it, the operation is used.
Answer: peek
10. A allows insertion and deletion from both ends of the list.
Answer: deque (double-ended queue)
11. In Python, elements can be inserted at the front of a deque using the method.
Answer: insert(0, element)
12. To delete an element from the rear of a deque, the method is used in Python.
Answer: pop()
13. To remove the front element from a deque, the method is used with index 0.
Answer: pop(0)
14. A data structure that can behave like both a queue and a stack is known as .
Answer: deque
15. In palindrome checking using deque, characters are compared by removing from and
ends.
Answer: front, rear

2 MARKS QUESTIONS

Q1. What is meant by the FIFO principle in the context of queues? Explain with an example.
Answer:
FIFO stands for First-In-First-Out, meaning the element that enters the queue first is the one that exits
first.
Example: In a bank queue, the first customer to enter the line is the first to be served at the counter.
Q2. Differentiate between Queue and Stack.
Answer:

Queue Stack

Follows FIFO (First In First Out) Follows LIFO (Last In First Out)
Insertion at rear, deletion at front Insertion and deletion at same end

Q3. What is underflow and when does it occur in a queue?


Answer:
Underflow is an error that occurs when a dequeue operation is attempted on an empty queue, i.e., when
there are no elements to remove.
Q4. Name any four operations supported by a queue.
Answer:
The four operations are:
1. enqueue() – to insert an element
2. dequeue() – to delete an element
3. peek() – to view the front element
4. isEmpty() – to check if the queue is empty

Q5. Write the syntax of a function to insert an element at the rear of a queue in Python.
Answer:

def enqueue(myQueue, element):


[Link](element)

Here, append() adds the element at the end (rear) of the list used as a queue.

Q6. Write a Python function to check whether a queue is empty or not.


Answer:

def isEmpty(myQueue):
if len(myQueue) == 0:
return True
else:
return False

Q7. What is a deque? Mention one of its applications.


Answer:
A deque (double-ended queue) is a linear data structure that allows insertion and deletion from both
front and rear ends.
Application: Checking whether a string is a palindrome using character comparisons from both ends.

Q8. Give the syntax of the deletion from rear operation in a deque using Python.
Answer:

def deletionRear(myDeque):
if not isEmpty(myDeque):
return [Link]()
else:
print("Deque empty")

Q9. Distinguish between insertFront() and insertRear() in deque.


Answer:
STUDENT NOTES || CHAPTER 4 QUEUE April 18, 2025

insertFront() insertRear()

Inserts element at the front of Inserts element at the rear of deque


deque
Uses insert(0, element) Uses append(element)

Q10. What does the peek() function do in a queue? Provide its implementation.
Answer:
peek() is used to view the front element of the queue without removing it.

def peek(myQueue):
if isEmpty(myQueue):
print("Queue is empty")
return None
else:
return myQueue[0]

3 MARKS QUESTIONS

Q1. Define a queue. How does it differ from a stack?


Answer:
A queue is an ordered linear data structure that follows the FIFO (First-In-First-Out) principle. In a
queue, insertion takes place at the rear and deletion occurs from the front.
Difference between queue and stack:

Queue Stack

Follows FIFO Follows LIFO


Insertion at rear, deletion at front Insertion and deletion at the same end
Used in print queues, job scheduling Used in undo operations, expression evaluation

Q2. Explain the operations enqueue(), dequeue(), and peek() in the context of queue implemen-
tation in Python.
Answer:

• enqueue() inserts an element at the rear of the queue:


def enqueue(myQueue, element):
[Link](element)

• dequeue() removes and returns the front element:

def dequeue(myQueue):
if not isEmpty(myQueue):
return [Link](0)
else:
print("Queue is empty")

• peek() returns the front element without removing it:

def peek(myQueue):
if isEmpty(myQueue):
print("Queue is empty")
return None
else:
return myQueue[0]

Q3. Describe any three real-life or computer science applications of queues.


Answer:

1. Customer Care Calls: Incoming calls are put in a queue and served in FIFO order.
2. Print Queue: Multiple print requests from different users are queued and printed one by one.
3. Operating System Task Scheduling: In multitasking systems, jobs are queued for processor time
in FIFO order.

Q4. What is a deque? Explain with two relevant operations and their Python implementations.
Answer:
A deque (double-ended queue) allows insertion and deletion from both front and rear.

• insertFront():

def insertFront(myDeque, element):


[Link](0, element)

• deletionRear():

def deletionRear(myDeque):
if not isEmpty(myDeque):
return [Link]()
else:
print("Deque empty")

22
Q5. Compare queue and deque based on structure and operations.
Answer:

Aspect Queue Deque

Structure Linear structure with two ends Linear, double-ended structure


Insertion At rear only At both front and rear
Deletion From front only From both front and rear
Flexibility Less flexible More flexible, supports stack
and queue behavior

Q6. Write the complete Python function to check if a queue is empty and to return its size.
Answer:

• isEmpty():

def isEmpty(myQueue):
if len(myQueue) == 0:
return True
else:
return False

• size():

def size(myQueue):
return len(myQueue)

These functions help prevent underflow and allow queue size tracking.

Q7. Describe the steps of palindrome checking using deque.


Answer:

1. Traverse the string character by character.


2. Insert each character into the deque from the rear.
3. Remove one character from the front and one from the rear, and compare.
4. Repeat until all characters are compared or only one remains.
5. If all pairs match, the string is a palindrome; otherwise, it’s not.

Q8. What are the roles of getFront() and getRear() in a deque? Provide their syntax.
Answer:

• getFront(): Returns the front element without deleting it.


def getFront(myDeque):
if not isEmpty(myDeque):
return myDeque[0]
else:
print("Queue underflow")

• getRear(): Returns the rear element without deleting it.

def getRear(myDeque):
if not isEmpty(myDeque):
return myDeque[len(myDeque) - 1]
else:
print("Deque empty")

These functions are useful for reading elements safely in a deque.

5 MARKS QUESTIONS

Q1. Explain the concept of queue. List and describe any four basic operations supported by a
queue with their purposes.
Answer:
A queue is a linear data structure that follows the FIFO (First-In-First-Out) principle, meaning the
element that enters the queue first is the first one to be removed.

Basic operations:

1. enqueue() – Inserts an element at the rear.


2. dequeue() – Deletes and returns the front element.
3. isEmpty() – Checks if the queue has no elements.
4. peek() – Views the front element without deleting it.

These operations help in maintaining the queue flow and avoid overflow/underflow issues.

Q2. With the help of Python code, demonstrate the implementation of a queue using a list. Include
all required functions.
Answer:

myQueue = list()

def enqueue(myQueue, element):


[Link](element)

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

def dequeue(myQueue):
if not isEmpty(myQueue):
return [Link](0)
else:
print("Queue is empty")

def size(myQueue):
return len(myQueue)

def peek(myQueue):
if isEmpty(myQueue):
print("Queue is empty")
return None
else:
return myQueue[0]

These functions simulate a queue using Python’s list type.


Q3. Compare queue and deque data structures in detail. Mention three applications where deque
is more suitable than queue.
Answer:

Feature Queue Deque (Double-Ended Queue)

Insertion Only at rear At both front and rear


Deletion Only from front From both front and rear
Flexibility Less flexible More flexible
Use case Print queue, OS job scheduler Palindrome checking, undo-redo

Applications where deque is more suitable:

1. Palindrome checking – allows character comparison from both ends.


2. Undo/Redo in editors – requires stack-like behavior.
3. Browser tab history – older entries are deleted from the rear, new ones from the front.

Q4. Describe the algorithm and steps to check if a string is a palindrome using deque. Provide a
brief explanation with an example.
Answer:
Algorithm Steps:
1. Traverse the string from left to right.
2. Insert each character into deque using insertRear.
3. Remove one character from front and rear, and compare them.
4. Repeat the process until:

• Deque is empty → string is palindrome.


• Characters don’t match → string is not palindrome.

Example:
For string "madam":
- Insert: m, a, d, a, m - Compare m with m, then a with a, and only d remains → palindrome.

Q5. Write a menu-driven Python program that performs basic operations on a deque. Include
insertion and deletion from both ends and necessary checks.
Answer:

def insertFront(myDeque, element):


[Link](0, element)

def insertRear(myDeque, element):


[Link](element)

def deletionFront(myDeque):
if not isEmpty(myDeque):
return [Link](0)
else:
print("Queue underflow")

def deletionRear(myDeque):
if not isEmpty(myDeque):
return [Link]()
else:
print("Deque empty")

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

def main():
myDeque = []
choice = int(input("Enter 1 for queue mode, 2 for deque mode: "))
if choice == 1:
insertRear(myDeque, input("Insert at rear: "))
print("Front element:", myDeque[0])
print("Deleted:", deletionFront(myDeque))
else:
insertFront(myDeque, input("Insert at front: "))
print("Rear element:", myDeque[-1])
print("Deleted from rear:", deletionRear(myDeque))

This menu-driven program allows use of deque as either a normal queue or double-ended queue.

Q6. Explain with examples how queues are used in real life and in computer science applications.
Answer:
Real-life applications:

1. Bank queues: First customer is served first.


2. Toll booths: Vehicles are allowed in FIFO order.
3. Customer care calls (IVRS): Callers wait in order of arrival.

Computer science applications:

1. Print queue: Print jobs are handled one by one in order.


2. Web server: Handles requests in FIFO order.
3. Job scheduling in OS: Jobs wait in queue for processor time.

Q7. What are the major operations supported by deque? Write the syntax of any four operations
with explanation.
Answer:

Operations:

1. insertFront():

[Link](0, element)

Inserts element at the front.

2. insertRear():

[Link](element)

Inserts element at the rear.

3. deletionFront():

[Link](0)

Deletes element from front.

4. deletionRear():
[Link]()

Deletes element from rear.

These allow deque to simulate both stack and queue behavior.

Q8. Discuss how the queue operations are simulated in the example of a bank cash counter scenario
from the PDF. Include the Python code provided.
Answer:

Scenario:

• People enter a queue (enqueue).


• Cashier serves people (dequeue).
• Queue length is checked (size).
• Remaining people are served using a loop.

Code:

myQueue = list()
element = input("Enter person’s code: ")
enqueue(myQueue, element)
element = input("Enter person’s code: ")
enqueue(myQueue, element)

print("Removed:", dequeue(myQueue))
print("Queue size:", size(myQueue))

for _ in range(3):
element = input("Enter person’s code: ")
enqueue(myQueue, element)

while not isEmpty(myQueue):


print("Removed:", dequeue(myQueue))

Q9. Write a function in Python to display all elements of a queue without deleting them. Also,
explain how this is different from peek().
Answer:

Function:

def displayQueue(myQueue):
if isEmpty(myQueue):
print("Queue is empty")
else:
for item in myQueue:
print(item)

• peek(): Returns only the front element without deleting.


• displayQueue(): Prints all elements of the queue without modifying it.

Q10. Explain the following terms with reference to queues: (a) Underflow (b) Overflow (c) Rear
(d) Front (e) isEmpty()
Answer:

a) Underflow: Attempting to delete from an empty queue.

b) Overflow: Attempting to insert into a full queue (not applicable in Python lists).

c) Rear: End where new elements are inserted.

d) Front: End from where elements are removed.

e) isEmpty(): A function that checks if a queue has no elements (len(queue) == 0).

Q11. Explain any five applications of deque from real life and computer science contexts.
Answer:
A deque (double-ended queue) is a linear data structure that allows insertion and deletion at both
ends—front and rear. Due to this flexibility, deque is used in various real-life and computer science
scenarios:

1. Re-entry in Ticket Queue (Real-life):


A person who has already purchased a train ticket may return to the counter for a query and be
allowed to rejoin from the front of the queue instead of the rear. This is possible only with a
deque structure.
2. Toll Booth Management (Real-life):
At highway toll plazas, if one booth becomes free, vehicles from the rear of other queues may shift
to the front of the available booth’s queue, involving both front and rear deletions/insertions—
ideal for deque.
3. Palindrome Checking (Computer Science):
Characters of a string are inserted into a deque from the rear. Then, characters are compared and
removed from both front and rear to check if the string is a palindrome.
4. Undo/Redo Functionality in Editors:
The undo/redo feature in text editors uses deque, where operations can be pushed and popped from
either end, simulating both stack and queue behavior.

5. Browser Tab History Navigation:


URLs visited in a browser are stored in a deque. The most recent URL is reopened first (LIFO),
and the oldest can be discarded from the rear (FIFO), based on memory limits.

Thus, deque is a versatile data structure capable of handling complex real-world and programming sce-
narios efficiently.

CHAPTER END EXERCISES

1. Fill in the blanks

a) is a linear list of elements in which insertion and deletion takes place from
different ends.
Answer: Queue

b) Operations on a queue are performed in order.


Answer: FIFO (First In First Out)

c) Insertion operation in a queue is called and deletion operation in a queue is called


.
Answer: enqueue, dequeue

d) Deletion of elements is performed from end of the queue.


Answer: front

e) Elements ‘A’, ‘S’, ‘D’ and ‘F’ are present in the queue, and they are deleted one at a time,
is the sequence of element received.
Answer: A, S, D, F

f) is a data structure where elements can be added or removed at either end, but not
in the middle.
Answer: Deque

g) A deque contains ‘z’, ‘x’, ‘c’, ‘v’ and ‘b’. Elements received after deletion are ‘z’, ‘b’, ‘v’, ‘x’ and ‘c’.
is the sequence of deletion operation performed on deque.
Answer: deletionFront(), deletionRear(), deletionRear(), deletionFront(), deletionFront()

2. Compare and contrast queue with stack.

Answer:
Queue Stack

Follows FIFO (First In First Out) Follows LIFO (Last In First Out)
Insertion at rear, deletion at front Insertion and deletion at the same end
Used in print queues, job scheduling Used in undo operations, expression evaluation

3. How does FIFO describe queue?


Answer:
FIFO stands for First-In-First-Out, which means that the element inserted first in the queue is the one
that is removed first. It ensures that elements are served in the same order as they arrive, just like people
standing in a queue.
4. Write a menu-driven Python program using queue to implement movement of shuttlecock in its
box.
Answer:
(Interpretation: Assuming each shuttlecock is added and removed in FIFO order)

def enqueue(box, shuttle):


[Link](shuttle)

def dequeue(box):
if len(box) > 0:
return [Link](0)
else:
return "Box is empty"

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

def main():
box = []
while True:
print("\n1. Add Shuttlecock")
print("2. Remove Shuttlecock")
print("3. Exit")
choice = int(input("Enter choice: "))
if choice == 1:
shuttle = input("Enter shuttlecock code: ")
enqueue(box, shuttle)
elif choice == 2:
print("Removed:", dequeue(box))
elif choice == 3:
break
else:
print("Invalid choice")

5. How is queue data type different from deque data type?


Answer:
Queue | Deque |

||–| | Insertion only at rear | Insertion at front and rear | | Deletion only from front | Deletion from front
and rear | | Less flexible | More flexible | | Cannot simulate stack behavior | Can simulate both stack and
queue |
6. Show the status of queue after each operation:
Operations:

enqueue(34)
enqueue(54)
dequeue()
enqueue(12)
dequeue()
enqueue(61)
peek()
dequeue()
dequeue()
dequeue()
dequeue()
enqueue(1)

Answer (Step-by-step queue status):

1. enqueue(34) → [34]

2. enqueue(54) → [34, 54]

3. dequeue() → [54]

4. enqueue(12) → [54, 12]


5. dequeue() → [12]

6. enqueue(61) → [12, 61]

7. peek() → 12

8. dequeue() → [61]

9. dequeue() → []

10. dequeue() → Underflow (queue is empty)

11. dequeue() → Underflow

12. enqueue(1) → [1]

7. Show the status of deque after each operation:

Operations:

peek()
insertFront(12)
insertRear(67)
deletionFront()
insertRear(43)
deletionRear()
deletionFront()
deletionRear()

Answer (Step-by-step deque status):

1. peek() → Deque is empty

2. insertFront(12) → [12]

3. insertRear(67) → [12, 67]

4. deletionFront() → [67]
5. insertRear(43) → [67, 43]

6. deletionRear() → [67]

7. deletionFront() → []

8. deletionRear() → Underflow (deque is empty)

8. Write a Python program to check whether the given string is palindrome or not using deque.

Answer:

def isPalindrome(string):
deque = []
for ch in string:
[Link](ch)

while len(deque) > 1:


front = [Link](0)
rear = [Link]()
if front != rear:
return False
return True

# Example usage
s = input("Enter a string: ")
if isPalindrome(s):
print("Palindrome")
else:
print("Not a palindrome")

You might also like