Ch-4 Queue Notes-2
Ch-4 Queue Notes-2
Chapter-4
QUEUE
Introduction to Queue
• A Queue is an ordered linear list of elements, having different ends for adding and removing elements
in it .
Queue follows the FIFO (First In First Out) principle. The element added first is the one removed first.
REAR(TAIL)→for insertion
FRONT(HEAD)→for deletion
• Items are inserted at the rear (tail) and removed from the front (head).
• Real-life Examples: Bank queues, toll booths, printer tasks, IVRS systems.
• When you book a ticket and it shows W/L1, it means your request is added to a queue.
• If someone cancels their confirmed ticket, the one at the front of the queue (like W/L1) gets
confirmed.
• When you call a customer care number, you might hear: > “Please wait while your call is.
transferred…”
• As soon as a customer care executive is free, the first caller in the queue is attended.
• A server can handle only a limited number of simultaneous requests (say 50).
• If 1000 students try to check results at the same time, the server keeps extra requests in
a queue.
• When multiple programs (called jobs) request CPU time, only one can run at a time.
• Jobs are queued and the CPU executes them in the order they arrive (simple job
scheduling).
3. Printer Queue Management
Operations on Queue
1. enqueue() : Insert an element at rear. Exception: Overflow if full (static case).
enqueue('Z') → Z
enqueue('X') → Z X
enqueue('C') → Z X C
dequeue() → X C
enqueue('V') → X C V
dequeue() → CV
dequeue() → V
else:
return Q[0]
def display(Q):
print("Q contains.",Q)
Q=[ ]
display(Q)
enqueue(Q,10)
display(Q)
enqueue(Q,20)
display(Q)
enqueue(Q,30)
print("Number of elements in Q",size(Q))
print("Peek element in Queue is ",peek(Q))
display(Q)
dequeue(Q)
display(Q)
dequeue(Q)
display(Q)
dequeue(Q)
display(Q)
dequeue(Q)
OUTPUT
Q contains. []
Q contains. [10]
• This needs a data structure where you can insert from either end of a deque.
• If one booth clears early, vehicles from the rear of other queues might shift to the.
• This needs both front and rear insertions/deletions, which is deque-like behaviour.
• Deque can store history and remove least-recently-used entries when full.
• But to manage multiple undo/redo and limited memory, deque provides efficient
handling.
3. Palindrome Checking
• A string is inserted character by character into a deque.
Operations on Deque
InsertFront():- Insert the element at front end of Deque.
3. Repeat: Remove the character from both front and rear, compare.
return DQ.insert(0,ele)
return DQ.append(ele)
def isEmpty(DQ):
return len(DQ) == 0
def deletionRear(DQ):
if isEmpty(DQ):
print("Deque is empty")
else:
return DQ.pop()
def deletionFront(DQ):
if isEmpty(DQ):
print("Deque is empty")
else:
return DQ.pop(0)
def display(DQ):
DQ=[]
display(DQ)
insertRear(DQ,10)
insertRear(DQ,20)
display(DQ)
deletionFront(DQ)
display(DQ)
deletionFront(DQ)
display(DQ)
insertFront(DQ,10)
insertFront(DQ,20)
display(DQ)
deletionRear(DQ)
deletionRear(DQ)
display(DQ)
deletionRear(DQ)
Summary Points
• Queue: FIFO, insertion at rear, deletion from front.
5. In a queue implemented with a Python list, myQueue.pop(0) removes the element from:
a. End b. Middle. c. Front d. None
10. In the bank queue example, which operation is used when a person enters the queue?
A) Dequeue B) Append C) Peek. 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()
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
20. Which operation would you perform to add an element at the front of a deque?
A) insertRear() B) append() C) insertFront() D) enqueue()
21. Assertion (A): In a queue, the first element added is the first one to be removed.
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.
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 0
II PUC COMPUTER SCIENCE SCANNER
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.
Reason (R): Tuples are mutable and support deletion and insertion at both ends.
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.
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 1
II PUC COMPUTER SCIENCE SCANNER
27. Assertion (A): peek() operation modifies the queue by removing the front element. Reason (R):
peek() is
similar to dequeue().
28. Assertion (A): A deque allows insertion and deletion from both ends.
Reason (R): Deque can be used to simulate both queue and stack behaviour.
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.
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.
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 2
II PUC COMPUTER SCIENCE SCANNER
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.
32. Assertion (A): Deletion from both ends in a deque is not allowed.
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.
34. Assertion (A): getFront() and getRear() both modify the contents of a deque.
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 3
II PUC COMPUTER SCIENCE SCANNER
35. Assertion (A): Job scheduling in operating systems can be based on queues.
36. Assertion (A): In deque, deletionRear() uses pop() without any arguments.
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.
38. Assertion (A): size() function returns the number of elements in a queue.
39. Assertion (A): A deque can behave as both a queue and a stack.
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 4
II PUC COMPUTER SCIENCE SCANNER
40. Assertion (A): Overflow can happen in a queue implemented using Python lists.
2. Queue follows the __________ strategy, where the first element inserted is the first one to be
removed. Answer: FIFO (First In First Out)
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()
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 5
II PUC COMPUTER SCIENCE SCANNER
7. A queue implemented using Python list does not require an __________ function because the
list size is dynamic.
Answer: is Full
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
1. 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.
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 6
II PUC COMPUTER SCIENCE SCANNER
Queue. Stack
Follows FIFO (First In First Out) Insertion at rear, Follows LIFO (Las99p8lIn First Out) Insertion
at rear,
deletion at front Follows LIFO (Last In First Out) Insertion and deletion at same end
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.
5. Write the syntax of a function to insert an element at the rear of a queue in Python.
Answer:
myQueue.append(element)
Here, append() adds the element at the end (rear) of the list used as a queue.
Answer:
def isEmpty(myQueue):
if len(myQueue) == 0:
return True
else:
return False
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 7
II PUC COMPUTER SCIENCE SCANNER
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.
8. Give the syntax of the deletion from rear operation in a deque using Python.
Answer:
def deletionRear(myDeque):
if not isEmpty(myDeque):
return myDeque.pop()
else:
print("Deque empty")
Answer:
insertFront() insertRear()
Inserts element at the front of deque. Inserts element at the rear of deque
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(Q):
if isEmpty(Q):
print("Queue is empty")
return None
else:
return Q[0]
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 8
II PUC COMPUTER SCIENCE SCANNER
3 MARKS QUESTIONS
1. 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.
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
2. Explain the operations enqueue(), dequeue(), and peek() in the context of queue implementation in
Python.
myQueue.append(element)
def dequeue(myQueue):
if not isEmpty(myQueue):
return myQueue.pop(0)
else:
print("Queue is empty")
def peek(myQueue):
if isEmpty(myQueue):
print("Queue is empty")
return None
else:
1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 9
II PUC COMPUTER SCIENCE SCANNER
return myQueue[0]
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.
4. 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():
myDeque.insert(0, element)
• deletionRear():
def deletionRear(myDeque):
if not isEmpty(myDeque):
return myDeque.pop()
else:
print("Deque empty")
Deletion from front only. Deletion from both front and rear
Answer:
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 0
II PUC COMPUTER SCIENCE SCANNER
3. Remove one character from the front and one from the rear, and compare.
5 MARKS QUESTIONS
Q1. Explain the concept of queue. Explain the 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:
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.
Q.append(ele)
def isEmpty(Q):
return len(Q) == 0
def dequeue(Q):
if not isEmpty(Q):
return Q.pop(0)
else:
print("Queue is empty")
def size(Q):
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 1
II PUC COMPUTER SCIENCE SCANNER
return len(Q)
def peek(Q):
if isEmpty(Q):
print("Queue is empty")
return None
else:
return Q[0]
def display(Q):
print("Q contains.",Q)
Q=[ ]
display(Q)
enqueue(Q,10)
display(Q)
enqueue(Q,20)
display(Q)
enqueue(Q,30)
display(Q)
dequeue(Q)
display(Q)
dequeue(Q)
display(Q)
dequeue(Q)
display(Q)
dequeue(Q)
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 2
II PUC COMPUTER SCIENCE SCANNER
Q4. Describe the algorithm and steps to check if a string is a palindrome using deque. Provide a brief
explanation with an example.
3. Remove one character from front and rear, and compare them.
Example:
Q5. Write a Python program that performs basic operations on a deque. Include insertion and deletion
from both ends and necessary checks.
Answer:
return DQ.insert(0,ele)
def insertRear(DQ, ele):
return DQ.append(ele)
def isEmpty(DQ):
return len(DQ) == 0
def deletionRear(DQ):
if isEmpty(DQ):
print("Deque is empty")
else:
return DQ.pop()
def deletionFront(DQ):
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 3
II PUC COMPUTER SCIENCE SCANNER
if isEmpty(DQ):
print("Deque is empty")
else:
return DQ.pop(0)
def display(DQ):
DQ=[]
display(DQ)
insertRear(DQ,10)
insertRear(DQ,20)
display(DQ)
deletionFront(DQ)
display(DQ)
deletionFront(DQ)
display(DQ)
insertFront(DQ,10)
insertFront(DQ,20)
display(DQ)
deletionRear(DQ)
deletionRear(DQ)
display(DQ)
deletionRear(DQ)
Q6. Explain with examples how queues are used in real life and in computer science applications.
Answer:
Real-life applications:
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 4
II PUC COMPUTER SCIENCE SCANNER
Q7. What are the major operations supported by deque? Write the syntax of any four operations with
explanation.
Answer:
Operations:
1. insertFront():
myDeque.insert(0, element)
2. insertRear():
myDeque.append(element)
3. deletionFront():
myDeque.pop(0)
myDeque.pop()
(a) Underflow
(b) Overflow
(c) Rear
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 5
II PUC COMPUTER SCIENCE SCANNER
(d) Front
(e) isEmpty()
Answer:
b) Overflow: Attempting to insert into a full queue (not applicable in Python lists).
Q 9. 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 re-joins 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 high way 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
behaviour.
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
scenarios efficiently.
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 6
II PUC COMPUTER SCIENCE SCANNER
EXERCISE SOLUTION
1. Fill in the blanks
a) ____________________ is a linear list of elements in which insertion and deletion takes place from
different ends.
Answer: Queue
c) Insertion operation in a queue is called ______________ and deletion operation in a queue is called
____________________.
Answer: enqueue, dequeue
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) Adeque 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()
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
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 7
II PUC COMPUTER SCIENCE SCANNER
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)
box.append(shuttle)
def dequeue(box):
if len(box) > 0:
return box.pop(0)
else:
return len(box) == 0
def main():
box = []
while True:
print("3. Exit")
if choice == 1:
enqueue(box, shuttle)
elif choice == 2:
print("Removed:", dequeue(box))
elif choice == 3:
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 8
II PUC COMPUTER SCIENCE SCANNER
break
else:
print("Invalid choice")
Answer:
Queue. Deque
Deletion from front only. Deletion from both front and rear
Cannot simulate stack behaviour. Can simulate both stack and queue |
enqueue(34)
enqueue(54)
dequeue()
enqueue(12)
dequeue()
enqueue(61)
peek()
dequeue()
dequeue()
dequeue()
dequeue()
enqueue(1)
Answer
2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 9
II PUC COMPUTER SCIENCE SCANNER
1. enqueue(34) → [34]
3. dequeue() → [54]
5. dequeue() → [12]
7. peek() → 12
8. dequeue() → [61]
9. dequeue() → [] 1
Operations:
peek()
insertFront(12)
insertRear(67)
deletionFront()
insertRear(43)
deletionRear()
deletionFront()
deletionRear()
Answer
3
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 0
II PUC COMPUTER SCIENCE SCANNER
2. insertFront(12) → [12]
4. deletionFront() → [67]
6. deletionRear() → [67]
7. deletionFront() → []
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:
deque.append(ch)
front = deque.pop(0)
rear = deque.pop()
if front != rear:
return False
return True
if isPalindrome(s):
print("Palindrome")
else:
print("Not
3
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 1