// This program demonstrates the IntQueue class
#include <iostream>
#include "IntQueue.h"
using namespace std;
int main()
{
IntQueue iQueue(5);
cout << "Enqueuing 5 items...\n";
//Enqueue 5 items.
for (int k = 1; k <= 5; k++)
iQueue.enqueue(k*k);
// Dequeue and retrieve all items in the queue.
cout << "The value in the queue were: ";
while (!Queue.isEmpty())
{
int value;
iQueue.dequeue(value);
cout << value << " ";
}
cout <<endl;
return 0;
}
#include <iostream>
#include "IntQueue.h"
using namespace std;
//********************************************************
// 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";
exit(1);
}
else
{
//Calculate the new rear position.
rear = (rear + 1) % queueSize;
// Insert new item.
queueArray[rear] = num;
// Update item count.
numItem++;
}
}
//**********************************************************
// Function dequeue removes the value at the *
// front of the queue, and copies it into num. *
//**********************************************************
void IntQueue::dequeue(int &num)
{
if (isEmpty())
{
cout << "The queue is empty.\n"
exit(1);
}
else
{
//Move front.
front = (front + 1) % queueSize;
//Retrieve the front item.
num = queueArray[front];
//Update item count.
numItems--;
}
}
//***********************************************************
// Function isFull returns true if the queue *
// is full, and false otherwise. *
//***********************************************************
bool IntQueue::isEmpty()
{
if (numItems > 0)
return false;
else
return true;
}
//************************************************************
// Function clear resets the front and rear *
// indices, and sets numItems to 0. *
//************************************************************
bool IntQueue::isFull()
{
if (numItems < queueSize)
return false;
else
return true;
}
//*************************************************************
// Function clear resets the front and rear *
// indices, and sets numItem to 0. *
//*************************************************************
void IntQueue::clear()
{
front = -1;
rear = -1;
numItem = 0;
}
#ifndef INTQUEUE_H
#define INTQUEUE_H
class IntQueue
{
private:
int *queueArray;
int queueSize;
int front;
int rear;
int numItems;
public;
IntQueue(int);
~IntQueue();
void enqueue(int);
void dequeue(int &);
bool isEmpty();
bool isFull();
void clear();
};
#endif