Chapter 10: Queue Class 12 Computer Science NCERT Solutions
Queues are an important linear data structure used in scenarios where elements are processed in the order they arrive. In Chapter 10 – Queue, students learn about the FIFO (First-In, First-Out) concept and how to implement queues using arrays in C++.
What You Will Learn in Chapter 10
This chapter explains how to declare, implement, and use queues in C++. Students will learn about queue operations like enqueue and dequeue, and how queues differ from stacks. The chapter also covers types of queues, including linear and circular queues.
Key Topics Covered
Introduction to Queue
A queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. The first element inserted is the first to be removed.
Queue Terminology
- Enqueue: Insert an element at the rear of the queue.
- Dequeue: Remove an element from the front of the queue.
- Front: Points to the first element.
- Rear: Points to the last element.
- Overflow: Trying to add to a full queue.
- Underflow: Trying to remove from an empty queue.
Queue Implementation Using Arrays
Declaration
int queue[SIZE];
int front = -1, rear = -1;
Enqueue Operation
if (rear == SIZE - 1) {
// Queue Overflow
} else {
if (front == -1) front = 0;
rear++;
queue[rear] = value;
}
Dequeue Operation
if (front == -1 || front > rear) {
// Queue Underflow
} else {
int val = queue[front];
front++;
}
Types of Queues
- Linear Queue: Basic FIFO structure with limited reuse of space.
- Circular Queue: Overcomes space limitation by wrapping rear to the front.
Circular Queue Example (Concept)
rear = (rear + 1) % SIZE;
front = (front + 1) % SIZE;
Queue Memory Representation
Queues use two pointers: front
and rear
to track the start and end. In a circular queue, these pointers loop back to the beginning when reaching the end of the array.
Applications of Queue
- Print queue management
- Task scheduling and job queues
- CPU process scheduling
- Customer service or ticketing systems
- Breadth-first search in graphs
Sample Queue Programs
- Insert and delete elements from a queue
- Implement circular queue using array
- Count the number of elements in a queue
- Queue reversal using stack
Output-Based Questions
Trace how front
and rear
values change after enqueue and dequeue operations. Identify underflow and overflow conditions from the code.
Common Errors
- Forgetting to reset
front
andrear
on overflow - Misusing index logic in circular queue
- Failing to check for underflow before
dequeue()
Download Chapter 10 Solutions PDF – Queue
Our PDF includes:
- Step-by-step NCERT solutions with array-based queue logic
- Program code with dry-run and pointer tracking
- Exam-style queue questions and board-focused programs
- Circular queue logic with diagrams
Highlights of Our NCERT Solutions
- Complete implementation of linear and circular queues
- Pointer movement diagrams and flowcharts
- Real exam-based programs for practice
- Queue vs Stack comparison table
- Structured code with detailed comments
Recommended Preparation Tips
- Practice basic enqueue and dequeue functions
- Write code for both linear and circular queues
- Dry-run programs to understand pointer movement
- Revise differences between queue and stack
- Understand real-world use cases of queues
Additional Study Resources
- Queue Flashcards: Operations and logic summaries
- Worksheets: Trace-the-code and fill-in-the-blank
- MCQs and Concept Check: Edge case testing
- Previous Year Questions: Queue-based coding questions
- Practice Programs: Circular queue simulation, task queues
Mastering Chapter 10 – Queue
Understanding queues is essential for managing data flow and sequential task processing. From real-life queues to computer task handling, queues model efficient data movement. With regular coding, clear logic building, and hands-on queue manipulation, students can excel in board exams and confidently tackle complex programming problems.
Class 12 Computer Science (C++) – Chapter 10 Queue
Long Answer Type Questions [4 marks each]
Question 1: Define member function delque() to perform delete operation on a linked queue where each node has the following structure:
struct node {
char name[20];
int marks;
node *link;
};
class queue {
node *front, *rear;
public:
queue() { front = rear = NULL; }
void delque();
};
void queue::delque() {
if (front != NULL) {
node *Temp = front;
cout << Temp->name << " " << Temp->marks;
front = front->link;
delete Temp;
if (front == NULL)
rear = NULL;
} else {
cout << "Queue is empty";
}
}
Question 2: Give the necessary declaration of linked implemented Queue containing players information and write a user-defined function in C++ to delete one Player’s information from the Queue.
struct node {
int PlayerNo;
char PlayerName[20];
node *Link;
};
node* QUEUEDEL(node* front, int val, char val2[]) {
node* temp;
if (front == NULL) {
cout << "Queue EMPTY";
} else {
temp = front;
temp->PlayerNo = val;
strcpy(temp->PlayerName, val2);
front = front->Link;
delete temp;
}
return front;
}
Question 3: Write a function QDELETE() in C++ to perform delete operation on a Linked Queue, which contains Passenger no and Passenger name.
struct node {
long int Pno;
char Pname[20];
node *Link;
};
node* QDELETE(node* front, int val, char val1[]) {
node* temp;
if (front == NULL) {
cout << "Queue Empty";
} else {
temp = front;
temp->Pno = val;
strcpy(temp->Pname, val1);
front = front->Link;
delete temp;
}
return front;
}
Question 4: Write a function QINSERT() in C++ to perform insert operation on a Linked Queue, which contains Client no and Client name.
struct Node {
long int Cno;
char Cname[20];
Node *Next;
};
Node* QINSERT(Node* rear, int val, char val[]) {
Node* temp = new Node;
temp->Cno = val;
strcpy(temp->Cname, val);
temp->Next = NULL;
if (rear != NULL)
rear->Next = temp;
rear = temp;
return rear;
}
Question 5: Write a function in C++ to perform Insert operation in a circular Queue containing Player’s information.
struct Player {
long PID;
char Pname[20];
Player* Link;
};
Player* front = NULL;
Player* rear = NULL;
void Insert() {
Player* P = new Player;
cout << "Enter Player ID & Name: ";
cin >> P->PID;
gets(P->Pname);
P->Link = NULL;
if (front == NULL && rear == NULL) {
front = rear = P;
rear->Link = front;
} else {
rear->Link = P;
rear = P;
rear->Link = front;
}
}
Question 6: Write a function in C++ to perform insert operation in a static circular queue containing book’s information.
struct BOOK {
long Accno;
char Title[20];
};
BOOK B[10];
int front = -1, rear = -1;
void insert() {
if ((rear == 9 && front == 0) || (front == rear + 1)) {
cout << "\nCircular queue is full";
return;
} else if (rear == -1) {
rear = front = 0;
} else if (rear == 9) {
rear = 0;
} else {
rear++;
}
cout << "Enter Title: ";
cin >> B[rear].Title;
cout << "Enter Accno: ";
cin >> B[rear].Accno;
}
Question 7: Write a function in C++ to perform insert operation in a dynamic queue containing DVD’s information.
struct DVD {
long No;
char Title[20];
DVD *Link;
};
void insert(DVD*& start, char data[20]) {
DVD* temp = new DVD;
temp->No = 0; // Assign appropriate number if needed
strcpy(temp->Title, data);
temp->Link = NULL;
if (start == NULL) {
start = temp;
} else {
DVD* q = start;
while (q->Link != NULL)
q = q->Link;
q->Link = temp;
}
}
Question 8: Write the definition of a member function INSERT() for a class QUEUE in C++, to insert a CUSTOMER in a dynamically allocated Queue of items.
struct CUSTOMER {
int CNO;
char CNAME[20];
CUSTOMER *Link;
};
class QUEUE {
CUSTOMER *R, *F;
public:
QUEUE() { R = F = NULL; }
void INSERT();
void DELETE();
~QUEUE();
};
void QUEUE::INSERT() {
CUSTOMER* T = new CUSTOMER;
cout << "Enter Customer No and Name: ";
cin >> T->CNO;
gets(T->CNAME); // or use cin >> T->CNAME
T->Link = NULL;
if (R == NULL) {
F = R = T;
} else {
R->Link = T;
R = T;
}
}