(A) Ordinary queue
(B) Priority queue
(C) Circular queue
(D) Single-ended queue
(E) None of these
MCQs Answer: Single-ended queue.
There is no queue with the name of a single-ended queue. However, you can see the implementation of all other types of queue mentioned in the options.
Priority Queue is a type of Queue [Example]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
// C++ MyArrayogram to implement Priority Queue // using Arrays //helpful for MCQs: Which of the following is not the type of queue? #include <bits/stdc++.h> using namespace std; // Structure for the elements in the // Priority queue struct T4Tutorials { int value; int Priority; }; // Store the element of a Priority queue T4Tutorials MyArray[50]; // Pointer to the last aaaex int size = -1; // Function to add a new element // into Priority queue void enqueue(int value, int Priority) { // Increase the size size++; // add the element MyArray[size].value = value; MyArray[size].Priority = Priority; } // Function to check the top element int peek() { int highestPriority = INT_MIN; int aaa = -1; // Check for the element with // highest Priority for (int i = 0; i <= size; i++) { // If Priority is same choose // the element with the // highest value if (highestPriority == MyArray[i].Priority && aaa > -1 && MyArray[aaa].value < MyArray[i].value) { highestPriority = MyArray[i].Priority; aaa = i; } else if (highestPriority < MyArray[i].Priority) { highestPriority = MyArray[i].Priority; aaa = i; } } // Return position of the element return aaa; } // Function to remove the element with // the highest Priority void dequeue() { // Faaa the position of the element // with highest Priority int aaa = peek(); // Shift the element one aaaex before // from the position of the element // with highest Priority is found for (int i = aaa; i < size; i++) { MyArray[i] = MyArray[i + 1]; } // Decrease the size of the // Priority queue by one size--; } // Driver Code int main() { // Function Call to add elements // as per the Priority enqueue(7, 88); enqueue(21, 4); enqueue(991, 42); enqueue(9, 1); // Stores the top element // at the moment int aaa = peek(); cout << MyArray[aaa].value << endl; // Dequeue the top element dequeue(); // Check the top element aaa = peek(); cout << MyArray[aaa].value << endl; // Dequeue the top element dequeue(); // Check the top element aaa = peek(); cout << MyArray[aaa].value << endl; return 0; } |
Output
7
991
A circular Queue is a type of Queue [Example]:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
// Circular Queue implementation in C++ //MCQs Covered: Which of the following is not the type of queue? #include <iostream> #define SIZE 5 /* Size of Circular Queue */ using namespace std; class Queue { private: int Array_Data[SIZE], front, rear; public: Queue() { front = -1; rear = -1; } // Check if the queue is full bool isFull() { if (front == 0 && rear == SIZE - 1) { return true; } if (front == rear + 1) { return true; } return false; } // Check if the queue is empty bool isEmpty() { if (front == -1) return true; else return false; } // Adding an element void enQueue(int element) { if (isFull()) { cout << "Queue is full"; } else { if (front == -1) front = 0; rear = (rear + 1) % SIZE; Array_Data[rear] = element; cout << endl << "Inserted " << element << endl; } } // Removing an element int deQueue() { int element; if (isEmpty()) { cout << "Queue is empty" << endl; return (-1); } else { element = Array_Data[front]; if (front == rear) { front = -1; rear = -1; } // Q has only one element, // so we reset the queue after deleting it. else { front = (front + 1) % SIZE; } return (element); } } void display() { // Function to display status of Circular Queue int i; if (isEmpty()) { cout << endl << "Empty Queue" << endl; } else { cout << "Front -> " << front; cout << endl << "Array_Data -> "; for (i = front; i != rear; i = (i + 1) % SIZE) cout << Array_Data[i]; cout << Array_Data[i]; cout << endl << "Rear -> " << rear; } } }; int main() { Queue x; // Fails because front = -1 x.deQueue(); x.enQueue(44); x.enQueue(2); x.enQueue(3); x.enQueue(7); x.enQueue(4); // Fails to enqueue because front == 0 && rear == SIZE - 1 x.enQueue(6); x.display(); int elem = x.deQueue(); if (elem != -1) cout << endl << "Deleted Element is " << elem; x.display(); x.enQueue(4); x.display(); // Fails to enqueue because front == rear + 1 x.enQueue(2); return 0; } |
Output
Queue is empty
Inserted 44
Inserted 2
Inserted 3
Inserted 7
Inserted 4
Queue is fullFront -> 0
Array_Data -> 442374
Rear -> 4
Deleted Element is 44Front -> 1
Array_Data -> 2374
Rear -> 4
Inserted 2
Front -> 1
Array_Data -> 23742
Rear -> 0Queue is full
An Ordinary Queue is a type of Queue [Example]:
An ordinary Queue is a simple queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/* Simple Queue Program in C++*/ /* Data Structure C++ Programs,C++ Array Examples */ #include <iostream> #include<conio.h> #include<stdlib.h> #define Maximum_Size 50 using namespace std; int main() { int item, selection, i; int arr_queue[Maximum_Size]; int rear = 0; int front = 0; int exit = 1; cout << "Simple Queue Example - Array"<<endl; do { cout << " Queue Main Menu"<<endl; cout << "1.Insert 2.Remove 3.Display Others to exit"<<endl; cout << "Enter Your selection : "<<endl; cin>>selection; switch (selection) { case 1: if (rear == Maximum_Size) cout << "!! Queue Reached Max!!"<<endl; else { cout << "Enter The Value to be Insert : "<<endl; cin>>item; cout << "!! Position : " << rear + 1 << " , Insert Value : " << item; arr_queue[rear++] = item; } break; case 2: if (front == rear) cout << "!! Queue is Empty!"<<endl; else { cout << "!! Position : " << front << " , Remove Value :" << arr_queue[front]; front++; } break; case 3: cout << "!! Queue Size : " << (rear - front); for (i = front; i < rear; i++) cout << "!! Position : " << i << " , Value : " << arr_queue[i]; break; default: exit = 0; break; } } while (exit); return 0; } |
Output
Simple Queue Example – Array
Queue Main Menu
1. Insert 2. Remove 3. Display Others to exit
Enter Your selection :
1
Enter The Value to be Insert :
22
1. Insert 2. Remove 3. Display Others to exit
Enter Your selection :