0% found this document useful (0 votes)
7 views

Static Queue Using Circular Array

The document contains two versions of a C++ program implementing a circular integer queue using a class called IntQueue. The incorrect version has errors in the enqueue and dequeue methods, leading to incorrect output when trying to add more items than the queue can hold. The correct version fixes these issues and successfully demonstrates the queue functionality with appropriate output.

Uploaded by

jgr102005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Static Queue Using Circular Array

The document contains two versions of a C++ program implementing a circular integer queue using a class called IntQueue. The incorrect version has errors in the enqueue and dequeue methods, leading to incorrect output when trying to add more items than the queue can hold. The correct version fixes these issues and successfully demonstrates the queue functionality with appropriate output.

Uploaded by

jgr102005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Incorrect version:

#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

{ rear = rear +1;

queueArray[rear] = num;}

numItems++;
}
}

//*********************************************
// Function dequeue removes the value at the *
// front of the queue, and copies t into num. *
//*********************************************

void IntQueue::dequeue(int &num)


{
if (isEmpty())
cout << "The queue is empty.\n";
else
{ if (front==rear)

{ num = queueArray[front];

front=rear= -1;}

else {
num = queueArray[front];

front = (front + 1) % queueSize;

}
numItems--;
}
}

//*********************************************
// Function isEmpty returns true if the queue *
// is empty, and false otherwise. *
//*********************************************

bool IntQueue::isEmpty(void)
{
bool status;

if (front == -1 && rear == -1)


status = true;
else
status = false;

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

cout << "Enqueuing 5 items...\n";


// Enqueue 5 items.
for (int x = 0; x < 5; x++)
iQueue.enqueue(x);

// Attempt to enqueue a 6th item.


cout << "Now attempting to enqueue again...\n";
iQueue.enqueue(5);

// Deqeue and retrieve all items in the queue


cout << "The values in the queue were:\n";
while (!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
}

Expected Program Output

Enqueuing 5 items...
Now attempting to enqueue again...
The queue is full.
The values in the queue were:
0

Actual Program Output:

Enqueuing 5 items...

The queue is full.


The queue is full.

The queue is full.

The queue is full.

The queue is full.

Now attempting to enqueue again...

The queue is full.

The values in the queue were:

Correct version:
#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);
};

IntQueue::IntQueue(int s)
{
queueArray = new int[s];
queueSize = s;
front = -1;
rear = -1;
numItems = 0;
}
IntQueue::~IntQueue(void)
{
delete[] queueArray;
}

void IntQueue::enqueue(int num)


{
if (isFull())
cout << "The queue is full.\n";
else
{
if (isEmpty())
{
rear = front = 0;
queueArray[rear] = num;
}
else
{
rear = (rear + 1) % queueSize;
queueArray[rear] = num;
}
numItems++;
}
}

void IntQueue::dequeue(int& num)


{
if (isEmpty())
cout << "The queue is empty.\n";
else
{
if (front == rear)
{
num = queueArray[front];
front = rear = -1;
}
else {
num = queueArray[front];
front = (front + 1)% queueSize;
}
numItems--;
}
}

bool IntQueue::isEmpty(void)
{
bool status;

if (front == -1 && rear == -1)


status = true;
else
status = false;

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

// Deqeue and retrieve all items in the queue


while (!iQueue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << endl;
}
return 0;
}

You might also like