I just need the Main.cpp for the ArrayQueue.cpp and it\'s .h, to see how this ex
ID: 3574584 • Letter: I
Question
I just need the Main.cpp for the ArrayQueue.cpp and it's .h, to see how this example works. The main.cpp must verify how much front, back and count is worth.
----------------------------------------------------------------------------------------------------------------------------------
#ifndef _ARRAY_QUEUE
#define _ARRAY_QUEUE
#include "QueueInterface.h"
#include "PrecondViolatedExcep.h"
const int MAX_QUEUE = 50;
template<class ItemType>
class ArrayQueue : public QueueInterface<ItemType>
{
private:
ItemType items[MAX_QUEUE]; // Array of queue items
int front; // Index to front of queue
int back; // Index to back of queue
int count; // Number of items currently in the queue
public:
ArrayQueue();
// Copy constructor and destructor supplied by compiler
bool isEmpty() const;
bool enqueue(const ItemType& newEntry);
bool dequeue();
/** @throw PrecondViolatedExcep if queue is empty. */
ItemType peekFront() const throw(PrecondViolatedExcep);
}; // end ArrayQueue
#include "ArrayQueue.cpp"
#endif
---------------------------------------------------------------------------------------------------------------------------------------------------------
#include "ArrayQueue.h" // Header file
template<class ItemType>
ArrayQueue<ItemType>::ArrayQueue() : front(0), back(MAX_QUEUE - 1), count(0)
{
} // end default constructor
template<class ItemType>
bool ArrayQueue<ItemType>::isEmpty() const
{
return count == 0;
} // end isEmpty
template<class ItemType>
bool ArrayQueue<ItemType>::enqueue(const ItemType& newEntry)
{
bool result = false;
if (count < MAX_QUEUE)
{
// Queue has room for another item
back = (back + 1) % MAX_QUEUE;
items[back] = newEntry;
count++;
result = true;
} // end if
return result;
} // end enqueue
template<class ItemType>
bool ArrayQueue<ItemType>::dequeue()
{
bool result = false;
if (!isEmpty())
{
front = (front + 1) % MAX_QUEUE;
count--;
result = true;
} // end if
return result;
} // end dequeue
template<class ItemType>
ItemType ArrayQueue<ItemType>::peekFront() const throw(PrecondViolatedExcep)
{
// Enforce precondition
if (isEmpty())
throw PrecondViolatedExcep("peekFront() called with empty queue");
// Queue is not empty; return front
return items[front];
} // end peekFront
// End of implementation file.
Explanation / Answer
The main function is as below.
int main(){
// Create an instance of ArrayQueue with the ItemType as integer.
ArrayQueue<int> aq;
int choice;
int number;
// Loop for menu items.
do{
cout <<"Please select one of the following options: ";
// Select the menu items and do accordingly.
cout << "1: Enqueue "
"2: Dequeue "
"3: Check if Queue is Empty "
"4: Get the Front Element "
"5: Exit ";
cin >> choice;
//*****************************************************************************
// Switch menu to display the menu.
//*****************************************************************************
switch (choice)
{
case 1:
cout << "Please enter the number to Enqueue to Queue ";
cin >> number;
aq.enqueue(number);
break;
case 2:
cout << "You have chosen to Dequeue. ";
aq.dequeue();
break;
case 3:
cout << "You have chosen to see if Queue is Empty";
cout << ag.isEmpty() << endl;
break;
case 4:
cout << "You have chosen to get the Front element ";
cout << "The front element is " << ag.peekFront() << endl;
break;
case 5:
cout << "You have chosen Quit, Goodbye.";
break;
default:
cout<< "Your selection must be between 1 and 5! ";
break;
}
}while(choice!=4);
}