Computer Science Chapter 4 β Queue β Complete Notes, Q&A & Exam Preparation
Complete Short Notes & Exam Preparation Guide
Based on the NCERT textbook PDF. The notes below keep the chapter sequence, technical terms, Python implementation, algorithms, figures and exam-oriented ideas from the selected chapter, while simplifying the prose for revision.
π Table of Contents
- 4.1 Introduction to Queue
- 4.2 Operations on Queue
- 4.3 Implementation of Queue using Python
- 4.4 Introduction to Deque
- 4.5 Implementation of Deque Using Python
- Textbook Exercise
- Summary
- Questions & Answers
- Important Questions
- β‘ Quick Revision
- Final Revision
4.1 Introduction to Queue
A queue is an ordered linear list of elements in which the ends used for adding and removing elements are different. A queue follows First-In-First-Out (FIFO), unlike the stack’s LIFO approach.
Examples shown in the chapter include students waiting in a line, customers at a bank cash counter and vehicles waiting at a fuel pump.
4.1.1 First In First Out (FIFO)
In FIFO, the element that enters the queue first is the first one to leave. The element that has stayed in the queue the longest is removed first. FIFO is also known as the First Come First Served (FCFS) approach.
- REAR = end where new items are added.
- FRONT = end from which items are removed.
- REAR is also called TAIL.
- FRONT is also called HEAD.
4.1.2 Applications of Queue
(A) Real-life applications
- Train-ticket waiting lists: waiting numbers form a queue and the ticket at the front can be confirmed when a confirmed ticket is cancelled.
- Customer-service / IVRS calls: callers wait in a queue until support is available.
- Single-lane roads and toll booths: vehicles are served according to the queue order.
(B) Computer-science applications
- Web-server requests: a queue can hold large numbers of requests when only a limited number can be handled concurrently.
- Operating-system jobs: jobs waiting for processor access can be queued and served in an order such as FIFO.
- Print requests: the OS queues requests sent to a shared printer and sends them one by one on a FIFO basis.
4.2 Operations on Queue
| Operation | Purpose | Exam point |
|---|---|---|
| ENQUEUE | Insert a new element at the rear. | Insertion beyond capacity gives Overflow. |
| DEQUEUE | Remove one element from the front. | Deletion from an empty queue gives Underflow. |
| IS EMPTY | Checks whether the queue has any element. | Used to avoid Underflow during dequeue. |
| PEEK | Views the front element without deleting it. | Reads the current front item. |
| IS FULL | Checks whether more elements can be added. | Helps avoid Overflow in a fixed-capacity queue. |
4.3 Implementation of Queue using Python
The chapter implements a queue using Python’s list data type. A list does not have a fixed size in the manner described for a fixed-capacity queue, so the chapter does not implement IS FULL for this list-based version.
0 for Front.Create queue
myQueue = list()enqueue()
def enqueue(myQueue, element):
myQueue.append(element)append() adds the element at the end of the list, which is the Rear.
isEmpty()
def isEmpty(myQueue):
if len(myQueue)==0:
return True
else:
return Falsedequeue()
def dequeue(myQueue):
if not (isEmpty(myQueue)):
return myQueue.pop(0)
else :
print("Queue is empty")pop(0) removes the first item, so it represents deletion at the Front.
size()
def size(myQueue):
return len(myQueue)peek()
def peek(myQueue):
if isEmpty(myQueue):
print('Queue is empty')
return None
else:
return myQueue[0]Bank cash-counter scenario – Program 4-1
myQueue = list()
# each person to be assigned a code as P1, P2, P3,...
element = input("enter personβs code to enter in queue :")
enqueue(myQueue,element)
element = input("enter personβs code for insertion in queue :")
enqueue(myQueue,element)
print("person removed from queue is:", dequeue(myQueue))
print("Number of people in the queue is :",size(myQueue))
element = input("enter personβs code to enter in queue :")
enqueue(myQueue,element)
element = input("enter personβs code to enter in queue :")
enqueue(myQueue,element)
element = input("enter personβs code to enter in queue :")
enqueue(myQueue,element)
print("Now we are going to remove remaining people from the queue")
while not isEmpty(myQueue):
print("person removed from queue is ",
dequeue(myQueue))Output
enter personβs code to enter in queue :P1
enter personβs code to enter in queue :P2
person removed from the queue is :p1
number of people in the queue is :1
enter personβs code to enter in queue :P3
enter personβs code to enter in queue :P4
enter personβs code to enter in queue :P5
Now we are going to remove remaining people from the queue
person removed from the queue is :p2
person removed from the queue is :p3
person removed from the queue is :p4
person removed from the queue is :p5
Queue is emptyNone when trying to print an empty queue.4.4 Introduction to Deque
Deque (pronounced βdeckβ) permits insertion and removal from either end: head/front or tail/rear. Because both ends can be used, it can implement stack or queue behaviour. It is also called a Double ended queue.
4.4.1 Applications of Deque
Real-life contexts
- Train-ticket counter: a person who already purchased a ticket may return later and be allowed to join from the front.
- Highway toll booth: vehicles may move from one queue toward a vacant booth, involving removal and joining from an end.
Computer-science contexts
- Browser history: the chapter describes stack-like history behaviour and removal of older/least-visited URLs when storage is limited.
- Do and Undo in a text editor.
- Palindrome checking by deleting and matching characters from both ends.
4.4.2 Operations on Deque
| Operation | Meaning |
|---|---|
| INSERTFRONT | Insert at the front. |
| INSERTREAR | Insert at the rear, like a normal queue. |
| DELETIONFRONT | Remove from the front. |
| DELETIONREAR | Remove from the rear. |
The deque also needs supporting operations used with a normal queue, including Is Empty, Peek and Size.
Algorithm 4.1 – Palindrome checking using deque
- Traverse the string madam from the left, one character at a time.
- Insert each character into the deque with INSERTREAR.
- Repeat the first two steps for all characters.
- Remove one character from the Front and one from the Rear using DELETIONFRONT and DELETIONREAR.
- Match the two removed characters.
- If they match, repeat until the deque is empty or has one character left; otherwise stop because the string is not a palindrome.
4.5 Implementation of Deque Using Python
Like the queue, the deque is implemented with the Python list data type in this chapter.
myDeque = list()insertFront()
def insertFront(myDeque, element):
myDeque.insert(0,element)insertRear()
def insertRear(myDeque,element):
myDeque.append(element)isEmpty()
def isEmpty(myDeque):
if len(myDeque) == 0:
return True
else:
return FalsedeletionRear()
def deletionRear(myDeque):
if not (isEmpty()):
return myDeque.pop()
else :
print("Deque empty")getFront()
def getFront(mydeque):
if not (isEmpty()):
return mydeque[0]
else :
print(" Queue empty")getRear()
def getRear(mydeque):
if not (isempty()):
return mydeque[len(mydeque)-1]
else :
print(" Deque empty")Deletion from Front: its implementation is the same as the queue’s dequeue() because the first element is removed with pop(0).
return myDeque.pop(0)Program 4-2 – Implementation of Deque in Python
def insertFront(myDeque,element):
myDeque.insert(0,element)
def getFront(myDeque):
if not (isEmpty(myDeque)):
return myDeque[0]
else:
print("Queue underflow")
def getRear(myDeque):
if not (isEmpty(myDeque)):
return myDeque[len(myDeque)-1]
else:
print ("Queue underflow")
def insertRear(myDeque,element):
myDeque.append(element)
def isEmpty(myDeque):
if len(myDeque) == 0:
return True
else:
return False
def deletionRear(myDeque):
if not isEmpty(myDeque):
return myDeque.pop()
else:
print("Queue underflow")
def deletionFront(myDeque):
if isEmpty(myDeque):
print("Queue underflow")
else:
return myDeque.pop(0)
def main():
dQu = list()
choice = int(input('enter 1 to use as normal queue 2 otherwise : '))
if choice == 1:
element = input("data for insertion at rear ")
insertRear(dQu,element)
element = getFront(dQu)
print("data at the beginning of queue is ", element)
element = input("data for insertion at front ")
insertRear(dQu,element)
print('data removed from front of queue is ', deletionFront(dQu))
print('data removed from front of queue is ', deletionFront(dQu))Output
enter 1 to use as normal queue 2 otherwise : 1
data for insertion at rear 23
data at the beginning of queue is 23
data for insertion at rear 45
data removed from front of queue is 23
data removed from front of queue is 45
Queue underflow
data removed from front of queue is None
enter 1 to use as normal queue 2 otherwise : 2
data for insertion at front 34
data at the end of queue is 34
data for insertion at front 56
data removed from rear of queue is 34
data removed from rear of queue is 56
Queue underflow
data removed from rear of queue is Noneπ Textbook Exercise – Chapter Practice
Exercise topics from the chapter
- Complete the chapter fill-in-the-blank concepts: queue definition, FIFO order, enqueue/dequeue, deletion end, the order of received elements, deque definition and the deletion sequence in a deque.
- Compare and contrast a queue with a stack.
- Explain how FIFO describes a queue.
- Write a menu-driven Python program using a queue to implement movement of a shuttlecock in its box.
- Explain how a queue data type differs from a deque data type.
- Show the status of a queue after the given sequence of enqueue, dequeue and peek operations.
- Show the status of a deque after the given sequence of peek, insertFront, insertRear, deletionFront and deletionRear operations.
- Write a Python program to check whether a given string is a palindrome using a deque, referring to Algorithm 4.1.
Chapter Summary
Queue
- Ordered linear data structure.
- Follows FIFO.
- Front and Rear mark the two ends.
- Insertion at Rear; deletion at Front.
- Python list implementation uses predefined list methods for the two ends.
Deque
- Double-ended queue.
- Insertion and deletion at either end.
- Can support stack and queue behaviour.
- Important operations include insertfront, insertrear, delete/deletion front and rear, getfront, getrear and isempty.
β Questions & Answers
1-Mark: Define queue.
1-Mark: What is FIFO?
1-Mark: What is Overflow?
1-Mark: What is Underflow?
1-Mark: What is deque?
2-Mark: Explain enqueue and dequeue.
2-Mark: What do isEmpty and peek do?
isEmpty checks whether the queue has elements. peek reads the Front item without deleting it.2-Mark: Name deque operations.
3-Mark: Explain FIFO with Front and Rear.
3-Mark: Give computer-science applications of queue.
3-Mark: Explain palindrome checking with deque.
4/5-Mark: Explain queue implementation in Python.
4/5-Mark: Explain Algorithm 4.1.
4/5-Mark: Explain deque implementation in Python.
π Important Questions
1 Mark
- Define queue.
- Expand FIFO and FCFS.
- Which end is used for insertion in a queue?
- Which end is used for deletion?
- What is Overflow?
- What is Underflow?
- Define deque.
2 Marks
- Differentiate enqueue and dequeue.
- Explain isEmpty and peek.
- State the roles of Front and Rear.
- Write the basic list methods used in the chapter for queue and deque.
3 Marks
- Explain FIFO.
- Explain queue applications given in the chapter.
- Explain the four basic deque operations.
- Explain palindrome checking using deque.
4/5 Marks
- Write the Python functions for implementing a queue using a list.
- Write the bank queue program and explain its output.
- Explain Algorithm 4.1 with the figures.
- Write the Python deque program and explain its output.
- Compare queue and deque with respect to insertion and deletion.
β‘ Quick Revision
Important Definitions
- Queue: ordered linear list; insertion/deletion at different ends; FIFO.
- FIFO: First In First Out.
- Deque: double-ended queue.
- Overflow: insertion beyond capacity.
- Underflow: deletion from an empty queue.
Important Terms
Important Differences
| Basis | Queue | Deque |
|---|---|---|
| Insertion | Rear | Front or Rear |
| Deletion | Front | Front or Rear |
| Behaviour | FIFO | Can act as queue or stack |
Important Syntax
myQueue = list()
myQueue.append(element)
myQueue.pop(0)
myDeque.insert(0,element)
myDeque.append(element)
myDeque.pop()
myDeque[len(myDeque)-1]Important Programs
- Queue implementation using list.
- Bank cash-counter queue scenario.
- Deque implementation in Python.
- Palindrome checking using deque.
Important Figures
- Figure 4.1 – bank queue
- Figure 4.2 – petrol-pump queue
- Figure 4.3 – queue operation stages
- Figure 4.4 – basic deque structure
- Figure 4.5 – deque after 4th iteration
- Figure 4.6 – deque after removal from both ends
Final Revision
append() + pop(0), with the support functions shown in the chapter.