Array implementation of Queue using class in c++
Sure! Here is an implementation of a queue using an array in C++:
#include <iostream>
#define SIZE 5
using namespace std;
class Queue {
private:
int items[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;
}
return false;
}
// Check if the queue is empty
bool isEmpty() {
if (front == -1)
return true;
else
return false;
}
// Adding an element to the queue
void enqueue(int element) {
if (isFull()) {
cout << "Queue is full" << endl;
} else {
if (front == -1) front = 0;
rear++;
items[rear] = element;
cout << "Inserted " << element << endl;
}
}
// Removing an element from the queue
int dequeue() {
int element;
if (isEmpty()) {
cout << "Queue is empty" << endl;
return -1;
} else {
element = items[front];
if (front >= rear) {
front = -1;
rear = -1;
} / Q has only one element, so we reset the queue after deleting it. /
else {
front++;
}
cout << "Deleted " << element << endl;
return element;
}
}
// Displaying the queue elements
void display() {
if (isEmpty()) {
cout << "Queue is empty" << endl;
} else {
cout << "Front index:" << front << endl;
cout << "Items: ";
for (int i = front; i <= rear; i++)
cout << items[i] << " ";
cout << endl;
cout << "Rear index:" << rear << endl;
}
}
};
int main() {
Queue q;
// Queue is empty
q.dequeue();
// Inserting elements in the queue
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
// Queue is full
q.enqueue(6);
// Displaying elements in the queue
q.display();
// Removing elements from the queue
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
q.dequeue();
// Queue is empty
q.dequeue();
return 0;
}
This implementation uses a class called Queue
to represent the queue data structure. The class has private member variables items
, front
, and rear
. The items
array is used to store the elements of the queue, while front
and rear
are used to keep track of the front and rear indices of the queue.
The Queue
class has several member functions. The isFull()
function checks if the queue is full by comparing the front
and rear
indices with the maximum size of the queue. The isEmpty()
function checks if the queue is empty by checking if the front
index is -1.
The enqueue()
function is used to add an element to the queue. It first checks if the queue is full using the isFull()
function. If the queue is not full, it increments the rear
index, assigns the element to the items
array at the rear
index, and prints a message indicating that the element has been inserted.
The dequeue()
function is used to remove an element from the queue. It first checks if the queue is empty using the isEmpty()
function. If the queue is not empty, it assigns the element at the front
index to a variable called element
. If the front
index is equal to or greater than the rear
index after removing the element, it means that the queue has only one element, so the front
and rear
indices are reset to -1. Otherwise, the front
index is incremented. The function then prints a message indicating that the element has been deleted and returns the element.
The display()
function is used to display the elements in the queue. It first checks if the queue is empty using the isEmpty()
function. If the queue is not empty, it prints the front
index, the elements in the items
array from front
to rear
, and the rear
index.
In the main()
function, an instance of the Queue
class is created. Several elements are enqueued using the enqueue()
function, and the elements in the queue are displayed using the display()
function. Then, several elements are dequeued using the dequeue()
function, and the elements in the queue are displayed again.