0% found this document useful (0 votes)
80 views31 pages

Ch-4 Queue Notes-2

The document provides an overview of queues and double-ended queues (deques), explaining their definitions, operations, and real-life applications. It details the FIFO principle of queues, their implementation in Python, and contrasts it with deques that allow insertion and deletion from both ends. Additionally, it includes multiple-choice questions to test understanding of the concepts presented.

Uploaded by

sagar200720
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)
80 views31 pages

Ch-4 Queue Notes-2

The document provides an overview of queues and double-ended queues (deques), explaining their definitions, operations, and real-life applications. It details the FIFO principle of queues, their implementation in Python, and contrasts it with deques that allow insertion and deletion from both ends. Additionally, it includes multiple-choice questions to test understanding of the concepts presented.

Uploaded by

sagar200720
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
You are on page 1/ 31

II PUC COMPUTER SCIENCE SCANNER

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.

It is also known as First Come First Serve (FCFS).

• Queue is Implemented using two ends:–

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.

Applications of Queue Real-Life Applications


1. Train Ticket Waiting List

• 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.

• This strictly follows the First-In-First-Out (FIFO) principle.


2. Customer Service Centre (IVRS- Interactive Voice Response System)

• When you call a customer care number, you might hear: > “Please wait while your call is.

transferred…”

• Your call is placed in a queue.

• As soon as a customer care executive is free, the first caller in the queue is attended.

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 1


II PUC COMPUTER SCIENCE SCANNER

3. Traffic Management (One-Way Road or Toll Booth)

• Vehicles on a one-way street or at a fuel pump/toll plaza form a queue.

• The vehicle that arrived first gets to move first.

Applications in Computer Science


1. Webserver Request Handling

• 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.

• These requests are served one by one as per FIFO.


2. CPU Task Scheduling in Operating Systems

• 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

• If multiple print requests are sent to a shared printer:

The first print command sent is executed first.

Remaining ones wait in the print queue.

Operations on Queue
1. enqueue() : Insert an element at rear. Exception: Overflow if full (static case).

2. dequeue(): Remove from front. Exception: Underflow if empty.

3. isEmpty(): Check if queue is empty.

4. peek() :View the front item without removing.

5. isFull(): Check if queue is full (not required in Python list-based queue).

Visual Example (Queue of alphabets):

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 2


II PUC COMPUTER SCIENCE SCANNER

enqueue('Z') → Z

enqueue('X') → Z X

enqueue('C') → Z X C

dequeue() → X C

enqueue('V') → X C V

dequeue() → CV

dequeue() → V

Implementation of Queue using Python


Python program to implement Queue using list.
def enqueue(Q, ele):
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):
return len(Q)
def peek(Q):
if isEmpty(Q):
print("Queue is empty")
return None

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 3


II PUC COMPUTER SCIENCE SCANNER

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]

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 4


II PUC COMPUTER SCIENCE SCANNER

Q contains. [10, 20]


Number of elements in Q 3
Peek element of Queue is 10
Q contains. [10, 20, 30]
Q contains. [20, 30]
Q contains. [30]
Q contains. []
Queue is empty

Introduction to Deque (Double-Ended Queue):


• Deque allows insertion and deletion from both ends– front and rear.

• Can behave as both:

Stack (LIFO): insertion/deletion at same end.


Queue(FIFO): insertion/deletion at opposite ends.

Applications of Deque (Double-Ended Queue)


Real-Life Applications
1. Re-entry at Ticket Counter
• A person buys a ticket, leaves, and then comes back with a query.
• Since they’ve already been served, they might get the privilege to re-join
from the front, not the rear.

• This needs a data structure where you can insert from either end of a deque.

2. Toll Booth Queue Redirection


• Suppose multiple toll booths exist.

• If one booth clears early, vehicles from the rear of other queues might shift to the.

front of the vacant booth’s queue.

• This needs both front and rear insertions/deletions, which is deque-like behaviour.

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 5


II PUC COMPUTER SCIENCE SCANNER

Applications in Computer Science


1. Browser History Navigation
• URLs are added to a stack-like structure, but if the memory is limited, older entries (from the
rear) must be removed.

• Deque can store history and remove least-recently-used entries when full.

2. Undo/Redo in Text Editors


• Operations are stored so that the last action can be undone first, like a stack.

• 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.

• Then characters are compared from front and rear simultaneously.

• If they match all the way, the string is a palindrome.


• Deque allows accessing both ends easily.

Operations on Deque
InsertFront():- Insert the element at front end of Deque.

InsertRear() :- Insert the element at rear end of Deque.

DeletionFront() :- Delete the element from front end of Deque.

DeletionRear() :- Delete the element from rear end of Deque.

isEmpty() :- To check if Deque is empty

getFront() :- View the front element of Deque without remove.

getRear() :- View the rear element of Deque without remove.

Algorithm to check the string is Palindrome using Deque


1. Traverse string left to right.

2. Insert each character at rear.

3. Repeat: Remove the character from both front and rear, compare.

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 6


II PUC COMPUTER SCIENCE SCANNER

4. If all pairs match → it’s a Palindrome

Implementation of Deque using Python


def insertFront(DQ, ele):

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):

if isEmpty(DQ):
print("Deque is empty")

else:

return DQ.pop(0)

def display(DQ):

print("Deque elements are. ",DQ)

DQ=[]

display(DQ)

insertRear(DQ,10)

insertRear(DQ,20)

display(DQ)

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 7


II PUC COMPUTER SCIENCE SCANNER

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.

• Deque: Insertion & deletion possible from both ends.


• Python lists can implement both structures easily.

• Additional operations: isEmpty, peek, size, getFront, getRear.

MULTIPLE CHOICE QUESTION (MCQ)


1. What principle does a queue follow?
a. LIFO b. FILO c. FIFO d. Random

2. In a queue, the element is inserted at _______


a. Middle. b. Rear c. Front d.Random position

3. What happens when you try to dequeue from an empty queue?

a. Underflow b. Error. c. Overflow d. Crash Answer

4. Which operation is used to add an element to a queue?

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 8


II PUC COMPUTER SCIENCE SCANNER

a. Pop. b. Dequeue c. Peek d. Enqueue

5. In a queue implemented with a Python list, myQueue.pop(0) removes the element from:
a. End b. Middle. c. Front d. None

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

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


a. LIFO. b. FLIFO. c. Random. d. Priority only

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


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

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

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()

12. What is a deque?

A) Atype of priority queue B) Queue with limited size


C) Double-ended queue. D) Stack with extra features

13. Which operation removes an element from the rear of a deque?


A) deletionFront B) pop(0) C) deletionRear D) enqueue

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

MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 9


II PUC COMPUTER SCIENCE SCANNER

15. What is the output of getRear(myDeque) if myDeque = [23, 45, 67]?


A) 23. B) 45 C) 67. D) Error

16. In palindrome checking using deque, characters are compared from:


A) Front only B) Rear only C) Front and Rear. D) Middle

17. What does myDeque.insert(0, element) do in deque?


A) Inserts at rear B) Inserts at front

C) Replaces the first element D) Deletes the first element

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

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

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.

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.

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.

1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 0
II PUC COMPUTER SCIENCE SCANNER

C) A is true, but R is false.

D) A is false, but R is true.

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.

24. Assertion (A): is Empty() 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.

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.

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.

1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 1
II PUC COMPUTER SCIENCE SCANNER

D) A is false, but R is true.

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.

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.

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.

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.

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.

1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 2
II PUC COMPUTER SCIENCE SCANNER

D) Both A and R are false.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

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.

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.

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()

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

8. The __________ function is used to check if the queue contains no elements.


Answer: is Empty()

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.

2. Differentiate between Queue and Stack.

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

3. 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.

4. 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. is Empty()– to check if the queue is empty

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

Answer:

def enqueue(myQueue, element):

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

6. 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

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

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")

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

Answer:
insertFront() insertRear()

Inserts element at the front of deque. Inserts element at the rear of 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(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.

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

2. Explain the operations enqueue(), dequeue(), and peek() in the context of queue implementation in
Python.

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

def enqueue(myQueue, element):

myQueue.append(element)

• dequeue() removes and returns the front element:

def dequeue(myQueue):

if not isEmpty(myQueue):

return myQueue.pop(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:

1
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 9
II PUC COMPUTER SCIENCE SCANNER

return myQueue[0]

3. 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.
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():

def insertFront(myDeque, element):

myDeque.insert(0, element)

• deletionRear():

def deletionRear(myDeque):

if not isEmpty(myDeque):

return myDeque.pop()

else:

print("Deque empty")

5. Compare queue and deque based on structure and operations.


Answer:
Queue. Deque

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


Insertion at rear only. Insertion at both front and rear

Deletion from front only. Deletion from both front and rear

6. Describe the steps of palindrome checking using deque.

Answer:

1. Traverse the string character by character.

2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 0
II PUC COMPUTER SCIENCE SCANNER

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.

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:

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.

def enqueue(Q, ele):

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)

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)

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.

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 Python program that performs basic operations on a deque. Include insertion and deletion
from both ends and necessary checks.

Answer:

def insertFront(DQ, ele):

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):

print("Deque elements are. ",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:

1. Bank queues: First customer is served first.

2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 4
II PUC COMPUTER SCIENCE SCANNER

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. Webserver: 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():

myDeque.insert(0, element)

Inserts element at the front.

2. insertRear():

myDeque.append(element)

Inserts element at the rear.

3. deletionFront():

myDeque.pop(0)

Deletes element from front.


4. deletionRear():

myDeque.pop()

Deletes element from rear.


These allow deque to simulate both stack and queue behavior.

Q 8. Explain the following terms with reference to queues:

(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:

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).

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

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) 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()

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?

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)

def enqueue(box, shuttle):

box.append(shuttle)

def dequeue(box):

if len(box) > 0:

return box.pop(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:

2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 8
II PUC COMPUTER SCIENCE SCANNER

break

else:

print("Invalid choice")

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

Answer:
Queue. Deque

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

Insertion at rear only. Insertion at both front and rear

Deletion from front only. Deletion from both front and rear

Less flexible. More flexible |

Cannot simulate stack behaviour. 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

2
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 9
II PUC COMPUTER SCIENCE SCANNER

(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() → [] 1

0. 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

3
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 0
II PUC COMPUTER SCIENCE SCANNER

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:
deque.append(ch)

while len(deque) > 1:

front = deque.pop(0)

rear = deque.pop()

if front != rear:

return False

return True

s = input("Enter a string: ")

if isPalindrome(s):

print("Palindrome")

else:

print("Not

3
MR. PRABHAKAR N.K HOD OF COMPUTER SCIENCE, EXCEL PU COLLEGE,GURUVAYANAKERE 1

You might also like