Array of Queues in C++
- Array of Queues in C++
- Fundamental Operations for the Queue Data Structure in C++
- Steps to Implement Array of Queues With Variable Size in C++
This section will discuss a C++ global array of queues with variable sizes.
Array of Queues in C++
A queue is a linear data structure that allows the insertion of new elements at one end, called the head, and the extraction of elements from the other end, called the tail. Queues are often used to implement first-in-first-out (FIFO) queues or priority queues.
The C++ global array of queues is used to store an arbitrary number of queues. Each queue’s size can be set when the global array is created.
This makes it possible to create an array with different sizes and capacities, which can be used to store different types of data.
This data structure is useful for storing data in which there are many items, but these items are not all needed at the same time. For example, if you want to store a list of names but only need to look up names occasionally, this would be a good place for your list.
Fundamental Operations for the Queue Data Structure in C++
The queue data structure supports the following operations:
EnQueue
: This inserts a new item into the queue. When an item is added to the queue, it is always added at the end.DeQueue
: This method removes an item from the queue. An item is always withdrawn or de-queued from the front of the queue.isEmpty
: This determines whether the queue is empty.isFull
: This determines whether the queue is full.
Steps to Implement Array of Queues With Variable Size in C++
-
Define the queue size by specifying the maximum number of elements it can hold.
-
Create an array that will store the queue elements.
-
Initialize the array to hold a predefined number of empty slots.
-
Add elements to the end of the array by calling
Enqueue()
with a value and an index. -
Remove elements from the front of the array by calling
Dequeue()
with a value and index.
Example:
#include <bits/stdc++.h>
using namespace std;
struct Queue {
int start, stop, eon;
int* queue;
Queue(int y) {
start = stop = 0;
eon = y;
queue = new int;
}
void queueEnqueue(int ghm) {
if (eon == stop) {
cout << endl << "This Queue is not empty" << endl;
return;
} else {
queue[stop] = ghm;
stop++;
}
return;
}
void demoSam() {
int x;
for (x = start; x < stop; x++) {
cout << queue[x];
}
return;
}
};
int main(void) {
Queue q(3);
q.demoSam();
q.queueEnqueue(6);
q.queueEnqueue(3);
q.queueEnqueue(12);
q.demoSam();
q.queueEnqueue(13);
q.demoSam();
return 0;
}
Click here to check the working of the code as mentioned above.
Muhammad Adil is a seasoned programmer and writer who has experience in various fields. He has been programming for over 5 years and have always loved the thrill of solving complex problems. He has skilled in PHP, Python, C++, Java, JavaScript, Ruby on Rails, AngularJS, ReactJS, HTML5 and CSS3. He enjoys putting his experience and knowledge into words.
Facebook