Static Queue Using Circular Array
Static Queue Using Circular Array
#include <iostream>
using namespace std;
class IntQueue
{
private:
int *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
void dequeue(int &);
bool isEmpty(void);
bool isFull(void);
};
//*************************
// Constructor *
//*************************
IntQueue::IntQueue(int s)
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
//*************************
// Destructor *
//*************************
IntQueue::~IntQueue(void)
{
delete [] queueArray;
}
//********************************************
// Function enqueue inserts the value in num *
// at the rear of the queue. *
//********************************************
void IntQueue::enqueue(int num)
{
if (isFull())
cout << "The queue is full.\n";
else
{
if (isEmpty())
{ rear = front = 0;
queueArray[rear] = num;
else
queueArray[rear] = num;}
numItems++;
}
}
//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************
{ num = queueArray[front];
front=rear= -1;}
else {
num = queueArray[front];
}
numItems--;
}
}
//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************
bool IntQueue::isEmpty(void)
{
bool status;
return status;
}
//********************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//********************************************
bool IntQueue::isFull(void)
{
bool status;
if (rear%queueSize == front)
status = true;
else
status = false;
return status;
}
// This program demonstrates the IntQeue class
void main(void)
{
IntQueue iQueue(5);
Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.
The values in the queue were:
0
Enqueuing 5 items...
Correct version:
#include <iostream>
class IntQueue
{
private:
int* queueArray;
int queueSize;
int front;
int rear;
int numItems;
public:
IntQueue(int);
~IntQueue(void);
void enqueue(int);
void dequeue(int&);
bool isEmpty(void);
bool isFull(void);
};
IntQueue::IntQueue(int s)
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void)
{
delete[] queueArray;
}
bool IntQueue::isEmpty(void)
{
bool status;
return status;
}
bool IntQueue::isFull(void)
{
bool status;
if (queueSize == numItems)
status = true;
else
status = false;
return status;
}
int main()
{
int t;
IntQueue iQueue(3);
cout << "Enqueuing items...\n";
// Enqueue items.
for (int x = 0; x < 3; x++)
iQueue.enqueue(x);
iQueue.dequeue(t);
iQueue.enqueue(6);
iQueue.dequeue(t);
iQueue.enqueue(7);
iQueue.dequeue(t);