This page contains the NCERT Computer Science class 12 chapter 4 Queue. You can find the solutions for the chapter 4 of NCERT class 12 Computer Science Exercise. So is the case if you are looking for NCERT class 12 Computer Science related topic Queue questions and answers for the Exercise
Exercise
Question 1
1. Fill in the blank:
a)
is a linear list of elements in which insertion and deletion takes place from different ends. (Queue)
b)
Operations on a queue are performed in order. (FIFO – First In First Out)
c)
Insertion operation in a queue is called and deletion operation in a queue is called . (Enqueue, Dequeue)
d)
Deletion of elements is performed from end of the queue. (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. (A, S, D, F)
f)
is a data structure where elements can be added or removed at either end, but not in the middle. (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. (DELETIONFRONT, DELETIONREAR, DELETIONREAR, DELETIONFRONT, DELETIONFRONT)
Answer 1
a)
Queue
b)
FIFO – First In First Out
c)
Enqueue, Dequeue
d)
Front
e)
A, S, D, F
f)
Deque
g)
DELETIONFRONT, DELETIONREAR, DELETIONREAR, DELETIONFRONT, DELETIONFRONT (ℹ️ Info: Last one could also be DELETIONREAR because only one element is left)
Question 2
2. Compare and contrast queue with stack.
Answer 2
Basis
Queue
Stack
Working principle
FIFO (First In First Out) — first inserted element comes out first
LIFO (Last In First Out) — last inserted element comes out first
Ends used
Uses two ends: FRONT and REAR
Uses one end (same end) for both insertion and deletion (TOP)
Insertion happens at
REAR end (adding)
TOP (push)
Deletion happens from
FRONT end (removing)
TOP (pop)
Common operation names
ENQUEUE (insert), DEQUEUE (delete), PEEK (view front)
PUSH (insert), POP (delete), PEEK/TOP (view top)
Simple real-life example
People standing in a line / FCFS
Stack of plates (last plate kept is removed first)
Question 3
3. How does FIFO describe queue?
Answer 3
FIFO means First In First Out.
So, the element that enters first in the queue will be the first one to come out.
That is why queue is also called First Come First Served (FCFS).
In a queue, we add elements at REAR and remove elements from FRONT.
Question 4
4. Write a menu driven python program using queue, to implement movement of shuttlecock in its box.
Answer 4
Program:
# Menu Driven Program: Shuttlecock Box using Queue (FIFO).
# Queue concept:
# - ENQUEUE: add shuttlecock at REAR end
# - DEQUEUE: remove shuttlecock from FRONT end
# - PEEK: view shuttlecock at FRONT without removing
# - Underflow: trying to DEQUEUE from empty queue
def enqueue(q, item):
# Insert at REAR
q.append(item)
def isEmpty(q):
return len(q) == 0
def dequeue(q):
# Delete from FRONT
if not isEmpty(q):
return q.pop(0)
else:
return None # Underflow situation
def peek(q):
# View FRONT element
if not isEmpty(q):
return q[0]
else:
return None
def size(q):
return len(q)
def display(q):
# Display queue from FRONT to REAR
if isEmpty(q):
print("Box is empty.")
else:
print("Shuttlecocks in box (FRONT -> REAR):", q)
# Main Program (Menu Driven)
box = list() # this is our queue
while True:
print("\n--- SHUTTLECOCK BOX MENU (QUEUE / FIFO) ---")
print("1. Add shuttlecock (ENQUEUE)")
print("2. Remove shuttlecock (DEQUEUE)")
print("3. See next shuttlecock (PEEK)")
print("4. Box size (SIZE)")
print("5. Display box")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
code = input("Enter shuttlecock code/name to add: ")
enqueue(box, code)
print(code, "added at REAR.")
elif choice == 2:
removed = dequeue(box)
if removed is None:
print("Queue Underflow: Box is empty, cannot remove.")
else:
print("Removed from FRONT:", removed)
elif choice == 3:
front_item = peek(box)
if front_item is None:
print("Box is empty (nothing at FRONT).")
else:
print("Next shuttlecock at FRONT is:", front_item)
elif choice == 4:
print("Number of shuttlecocks in box:", size(box))
elif choice == 5:
display(box)
elif choice == 6:
print("Exiting program...")
break
else:
print("Invalid choice. Try again.")
Sample Output:
--- SHUTTLECOCK BOX MENU (QUEUE / FIFO) ---
1. Add shuttlecock (ENQUEUE)
2. Remove shuttlecock (DEQUEUE)
3. See next shuttlecock (PEEK)
4. Box size (SIZE)
5. Display box
6. Exit
Enter your choice: 1
Enter shuttlecock code/name to add: YONEX-1
YONEX-1 added at REAR.
--- SHUTTLECOCK BOX MENU (QUEUE / FIFO) ---
1. Add shuttlecock (ENQUEUE)
2. Remove shuttlecock (DEQUEUE)
3. See next shuttlecock (PEEK)
4. Box size (SIZE)
5. Display box
6. Exit
Enter your choice: 5
Shuttlecocks in box (FRONT -> REAR): ['YONEX-1']
--- SHUTTLECOCK BOX MENU (QUEUE / FIFO) ---
1. Add shuttlecock (ENQUEUE)
2. Remove shuttlecock (DEQUEUE)
3. See next shuttlecock (PEEK)
4. Box size (SIZE)
5. Display box
6. Exit
Enter your choice: 2
Removed from FRONT: YONEX-1
--- SHUTTLECOCK BOX MENU (QUEUE / FIFO) ---
1. Add shuttlecock (ENQUEUE)
2. Remove shuttlecock (DEQUEUE)
3. See next shuttlecock (PEEK)
4. Box size (SIZE)
5. Display box
6. Exit
Enter your choice: 6
Exiting program...
Question 5
5. How is queue data type different from deque data type?
Answer 5
Basis
Queue
Deque
(Double Ended Queue)
(Double Ended Queue)
Full form / meaning
Queue follows FIFO strategy
Deque (pronounced “deck”) allows addition/removal from any end (front/rear)
Insertion happens at
REAR end (insertion at rear)
Can insert at FRONT (INSERTFRONT) or REAR (INSERTREAR)
Deletion happens from
FRONT end (deletion at front)
Can delete from FRONT (DELETIONFRONT) or REAR (DELETIONREAR)
Restriction
Insertion and deletion are from different fixed ends (rear/front)
No restriction on the side for addition/removal (front or rear)
What it can implement
Only queue operations
Can be used to implement stack or queue
Other notes (from book)
Uses Front and Rear to indicate beginning and end
“Deque is a version of queue” allowing insertion/deletion at both ends; supports both stack & queue operations
Question 6
6. Show the status of queue after each operation:
enqueue(34)enqueue(54)dequeue()enqueue(12)dequeue()enqueue(61)peek()dequeue()dequeue()dequeue()dequeue()enqueue(1)Answer 6
Queue shown as: FRONT → REAR
Step
Operation
Element Returned
Queue Status
(FRONT → REAR)
(FRONT → REAR)
1
enqueue(34)
—
[34]
2
enqueue(54)
—
[34, 54]
3
dequeue()
34
[54]
4
enqueue(12)
—
[54, 12]
5
dequeue()
54
[12]
6
enqueue(61)
—
[12, 61]
7
peek()
12
[12, 61]
8
dequeue()
12
[61]
9
dequeue()
61
[]
10
dequeue()
Underflow
[]
11
dequeue()
Underflow
[]
13
enqueue(1)
—
[1]
Question 7
7. Show the status of deque after each operation:
peek()insertFront(12)insertRear(67)deletionFront()insertRear(43)deletionRear()deletionFront()deletionRear()Answer 7
Deque shown as: FRONT → REAR
(Assuming peek() means view FRONT)
Step
Operation
Element Returned
Deque Status
(FRONT → REAR)
(FRONT → REAR)
1
peek()
Underflow
[]
2
insertFront(12)
—
[12]
3
insertRear(67)
—
[12, 67]
4
deletionFront()
12
[67]
5
insertRear(43)
—
[67, 43]
6
deletionRear()
43
[67]
7
deletionFront()
67
[]
8
deletionRear()
Underflow
[]
Question 8
8. Write a python program to check whether the given string is palindrome or not, using deque. (Hint: refer to algorithm 4.1)
Answer 8
Program:
# Palindrome check using DEQUE (implemented using list)
# Algorithm idea (as in the book):
# 1) Insert all characters using INSERTREAR
# 2) Remove one char from FRONT and one from REAR
# 3) Compare them until deque becomes empty or has 1 char
def insertRear(dq, ch):
dq.append(ch)
def deletionFront(dq):
return dq.pop(0)
def deletionRear(dq):
return dq.pop()
# Main Program
s = input("Enter a string: ")
# Create deque and insert characters at REAR (like normal queue)
dq = list()
for ch in s:
insertRear(dq, ch)
is_palindrome = True
# Compare FRONT and REAR characters
while len(dq) > 1:
front_ch = deletionFront(dq)
rear_ch = deletionRear(dq)
if front_ch != rear_ch:
is_palindrome = False
break
if is_palindrome:
print('The word "' + s + '" is a palindrome')
else:
print('The word "' + s + '" is not a palindrome')
Sample Output:
Enter a string: madam
The word "madam" is a palindrome
Enter a string: hello
The word "hello" is not a palindrome