Menturox.in

Menturox - Header Only with Universal Menu
NCERT Solutions Class 12 Computer Science Chapter 4 Queue - 28 Programming Questions with Answers

NCERT Solutions Class 12 Computer Science Chapter 4 Queue

CBSE Class 12 Computer Science - Queue Programming Questions with C++ Solutions

1
Question

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

Answer

The delque() function removes and displays the front node from the linked queue. Here's the implementation:

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"; }

This function checks if the queue is not empty, displays the data of the front node, updates the front pointer, deletes the old front node, and handles the case when the queue becomes empty.

2
Question

Give the necessary declaration of linked implemented Queue containing players information. Also 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; };

Answer

Here's the function to delete a player's information from the queue:

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; }

This function removes the front player from the queue and returns the updated front pointer.

3
Question

Write a function QDELETE() in C++ to perform delete operation on a Linked Queue, which contains Passenger no and Passenger name. Consider the following definition of Node in the code:

struct node { long int Pno; char Pname[20]; node *Link; };

Answer

Function to delete passenger information from the linked queue:

Node *QDELETE(Node *front, int val, char vail[]) { Node *temp; if (front == NULL) cout << "Queue Empty"; else { temp = front; temp->Pno = val; strcpy(temp->Pname, vail); front = front->Link; delete temp; } return front; }

This function removes the front passenger from the queue and deallocates the memory.

4
Question

Write a function QINSERT() in C++ to perform insert operation on a Linked Queue, which contains Client no and Client name. Consider the following definition of NODE:

struct Node { long int Cno; // Client No char Cname[20]; // Client Name Node *Next; };

Answer

Function to insert client information into the linked queue:

Node *QINSERT(Node *rear, int val, char val[]) { Node *temp; temp = new Node; temp->Cno = val; strcpy(temp->Cname, val); temp->Next = NULL; if (rear != NULL) rear->Next = temp; rear = temp; return rear; }

This function creates a new node, assigns the client data, and adds it to the rear of the queue.

5
Question

Write a function in C++ to perform Insert operation in a circular Queue containing Player information (represented with the help of an array of structure Player):

struct Player { long PID; //Player ID char Pname[20]; //Player Name Player *Link; };

Answer

Function to insert player information into a circular queue:

void Insert() { Player *P = new Player; cout << "Enter Player ID & Name: "; cin >> P->PID; cin.ignore(); cin.getline(P->Pname, 20); P->Link = NULL; if ((front == NULL) && (rear == NULL)) { front = rear = P; rear->Link = front; // Circular link } else { rear->Link = P; P->Link = front; // Maintain circular property rear = P; } }

This function maintains the circular property of the queue by linking the rear node to the front.

6
Question

Write a function in C++ to perform insert operation in a static circular queue containing books information (represented with the help of an array of structure BOOK):

struct BOOK { long Accno; //Book Accession Number char Title[20]; //Book Title };

Answer

Implementation for static circular queue of books:

const int SIZE = 10; struct BOOK { long Accno; char Title[20]; } B[SIZE];int front = -1, rear = -1;void insert() { if ((rear == SIZE-1 && front == 0) || (front == rear+1)) { cout << "\nCircular queue is full"; return; } else if (rear == -1) { rear = 0; front = 0; } else if (rear == SIZE-1) rear = 0; else rear++; cout << "Enter Title: "; cin >> B[rear].Title; cout << "Enter Accno: "; cin >> B[rear].Accno; }

This function handles the circular nature of the queue by wrapping around when necessary.

7
Question

Write a function in C++ to perform insert operation in a dynamic queue containing DVDs information (represented with the help of an array of structure DVD).

Answer

Function to insert DVD information into a dynamic queue:

struct DVD { long No; // DVD Number char Title[20]; // DVD Title DVD *Link; };void insert(DVD **start, char data[20]) { DVD *temp = new DVD; temp->No = 0; // Initialize with default value 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; } }

This function dynamically allocates memory for a new DVD node and inserts it at the end of the queue.

8
Question

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 considering the following code:

struct CUSTOMER { int CNO; char CNAME[20]; CUSTOMER *Link; };class QUEUE { CUSTOMER *R, *F; public: QUEUE() {R=NULL; F=NULL;} void INSERT(); void DELETE(); ~QUEUE(); };

Answer

Member function INSERT() for the QUEUE class:

void QUEUE::INSERT() { CUSTOMER *T = new CUSTOMER; cout << "Enter Customer Number: "; cin >> T->CNO; cout << "Enter Customer Name: "; cin.ignore(); cin.getline(T->CNAME, 20); T->Link = NULL; if (R == NULL) // Queue is empty { F = T; R = T; } else { R->Link = T; R = T; } }

This function creates a new customer node and adds it to the rear of the queue, maintaining both front (F) and rear (R) pointers.

9
Question

Write a function in C++ to perform Insert operation in a circular Queue containing Player information (represented with the help of an array of structure Player).

struct Player { long PID; //Player ID char Pname[20]; //Player Name Player *Link; };

Answer

Function to insert Player information in a circular queue:

Player *front = NULL, *rear = NULL;void Insert() { Player *P = new Player; cout << "Enter Player ID & Name: "; cin >> P->PID; cin.ignore(); cin.getline(P->Pname, 20); P->Link = NULL; if ((front == NULL) && (rear == NULL)) { front = rear = P; rear->Link = front; // Make it circular } else { rear->Link = P; P->Link = front; rear = P; } }

This implementation maintains the circular property by ensuring the rear always points to the front.

15
Question

Write a function to implement a priority queue using arrays. The priority queue should support insertion and deletion based on priority levels.

Answer

Implementation of priority queue using arrays:

#include <iostream> using namespace std;struct Element { int data; int priority; };class PriorityQueue { Element arr[100]; int size; public: PriorityQueue() { size = 0; } void insert(int data, int priority) { if (size >= 100) { cout << "Queue is full"; return; } arr[size].data = data; arr[size].priority = priority; size++; // Sort based on priority for (int i = size - 1; i > 0; i--) { if (arr[i].priority > arr[i-1].priority) { Element temp = arr[i]; arr[i] = arr[i-1]; arr[i-1] = temp; } else break; } } void deleteElement() { if (size == 0) { cout << "Queue is empty"; return; } cout << "Deleted: " << arr[0].data << " (Priority: " << arr[0].priority << ")"; for (int i = 0; i < size - 1; i++) arr[i] = arr[i + 1]; size--; } };

This priority queue maintains elements in sorted order based on priority, with higher priority elements at the front.

Key Points to Remember

  • Queue follows FIFO (First In First Out) principle
  • Front pointer points to the first element, rear pointer points to the last element
  • Circular queues efficiently utilize memory by wrapping around
  • Dynamic queues use linked lists for flexible memory allocation
  • Static queues use arrays with fixed size limitations
  • Priority queues serve elements based on priority rather than insertion order
  • Always check for empty/full conditions before operations
  • Proper memory management is crucial in dynamic implementations

Queue Applications and Concepts

Queues are fundamental data structures with numerous real-world applications:

Common Applications

  • CPU scheduling in operating systems
  • Printer queue management
  • Breadth-First Search (BFS) algorithms
  • Buffer for data streams
  • Handling requests in web servers

Types of Queues

  • Simple Queue: Basic FIFO implementation
  • Circular Queue: Efficient memory utilization
  • Priority Queue: Elements served based on priority
  • Deque: Double-ended queue (insertion/deletion at both ends)

Time Complexity

  • Enqueue (Insert): O(1)
  • Dequeue (Delete): O(1)
  • Front/Rear access: O(1)
  • Search: O(n)

Understanding these queue implementations is essential for:

  • CBSE Class 12 Computer Science examinations
  • Competitive programming contests
  • Software development projects
  • Algorithm design and analysis
  • System design interviews

Leave a Comment

Your email address will not be published. Required fields are marked *