Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add the following 2 member functions to the ArrayQueue: int getPosition(const It

ID: 3768606 • Letter: A

Question

Add the following 2 member functions to the ArrayQueue:

int getPosition(const ItemType& itemToCheck) const;
// starting at 1, return the position in the queue, from the front, of itemToCheck
//               if not in the Queue, return 0

void removeLastMember();
// Precondition: the Queue has at least one member
// Postcondition: the last member of the Queue has been removed

Add the following 2 functions, as templates, to the driver ( they will not be member functions )

void printQueue( ArrayQueue info ); // will destroy the parameter, passed by value, which is ok

ArrayQueue joinTogether( ArrayQueue first, ArrayQueue second); // will destroy first and second,
// pass by value, which is ok

NOTE: Pass by value uses the copy constructor.
Returning an object uses the copy constructor.

Since there is no dynamic memory, the copy constructor and the = operator (both written by the compiler),
will be OK.

** ADT queue: Circular array-based implementation.
Listing 14-4.
@file ArrayQueue.h */

#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

/** ADT queue: Circular array-based implementation.
Listing 14-5.
@file ArrayQueue.cpp */

#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.

/** Listing 7-5.
@file PrecondViolatedExcep.h */

#ifndef _PRECOND_VIOLATED_EXCEP
#define _PRECOND_VIOLATED_EXCEP

#include <stdexcept>
#include <string>

using namespace std;

class PrecondViolatedExcep : public logic_error
{
public:
PrecondViolatedExcep(const string& message = "");
}; // end PrecondViolatedExcep
#endif

// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-6.
@file PrecondViolatedExcep.cpp */
#include "PrecondViolatedExcep.h"

PrecondViolatedExcep::PrecondViolatedExcep(const string& message): logic_error("Precondition Violated Exception: " + message)
{
} // end constructor

// End of implementation file.

/** Listing 13-1.
@file QueueInterface.h */

#ifndef _QUEUE_INTERFACE
#define _QUEUE_INTERFACE

template<class ItemType>
class QueueInterface

{
public:
/** Sees whether this queue is empty.
@return True if the queue is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the back of this queue.
@post If the operation was successful, newEntry is at the
back of the queue.
@param newEntry The object to be added as a new entry.
@return True if the addition is successful or false if not. */
virtual bool enqueue(const ItemType& newEntry) = 0;

/** Removes the front of this queue.
@post If the operation was successful, the front of the queue
has been removed.
@return True if the removal is successful or false if not. */
virtual bool dequeue() = 0;

/** Returns the front of this queue.
@pre The queue is not empty.
@post The front of the queue has been returned, and the
queue is unchanged.
@return The front of the queue. */
virtual ItemType peekFront() const = 0;
}; // end QueueInterface
#endif

#include "ArrayQueue.h" // ADT Queue operations
#include <iostream>
#include <string>

using namespace std;

void queueTester(QueueInterface<string>* queuePtr)
{
   string items[] = {"one", "two", "three", "four", "five", "six"};
   cout << "Empty: " << queuePtr->isEmpty() << endl;
   for (int i = 0; i < 6; i++)
{
       cout<<"Adding " << items[i] << endl;
bool success = queuePtr->enqueue(items[i]);
if (!success)
cout << "Failed to add " << items[i] << " to the queue." << endl;
   }

   cout << "Empty?: " << queuePtr->isEmpty() << endl;
  
   for (int i = 0; i < 6; i++)
{
       cout << "Loop " << i << endl;
  
try
{
cout << "peekFront: " << queuePtr->peekFront() << endl;
}
catch (PrecondViolatedExcep e)
{
cout << e.what() << endl;
} // end try/catch

       cout << "Empty: " << queuePtr->isEmpty() << endl;
       cout << "dequeue: " << queuePtr->dequeue() << endl;
   }

cout << "dequeue with an empty queue: " << endl;
   cout << "Empty: " << queuePtr->isEmpty() << endl;
   cout << "dequeue: " << queuePtr->dequeue() << endl; // nothing to dequeue!

try
{
cout << "peekFront with an empty queue: " << endl;
   cout << "peekFront: " << queuePtr->peekFront() << endl; // nothing to see!
}
catch (PrecondViolatedExcep e)
{
cout << e.what();
} // end try/catch
} // end queueTester

int main()
{
   QueueInterface<string>* queuePtr = new ArrayQueue<string>();
   cout << "Testing the Array-based queue:" <<endl;
   queueTester(queuePtr);
  
return 0;
} // end main
/*
Testing the Array-based queue:
Empty: 1
Adding one
Adding two
Adding three
Adding four
Adding five
Adding six
Empty?: 0
Loop 0
peekFront: one
Empty: 0
dequeue: 1
Loop 1
peekFront: two
Empty: 0
dequeue: 1
Loop 2
peekFront: three
Empty: 0
dequeue: 1
Loop 3
peekFront: four
Empty: 0
dequeue: 1
Loop 4
peekFront: five
Empty: 0
dequeue: 1
Loop 5
peekFront: six
Empty: 0
dequeue: 1
dequeue with an empty queue:
Empty: 1
dequeue: 0
peekFront with an empty queue:
peekFront: Precondition Violated Exception: peekFront() called with empty queue
*/

Explanation / Answer

#include<iostream.h>
#include<conio.h>
int queue[100],front=-1,rear=-1,max;
void insert_element();
void delete_element();
void display_queue();

void main()
{ clrscr();
int choice;
cout<<" Queue operations ";
cout<<"Enter the size of Queue : ";
cin>>max;
do
{cout<<"1.Insert an element to the queue";
cout<<" 2.Delete an element from the queue";
cout<<" 3.Display queue";
cout<<" 4.Exit";
cout<<" Enter your choice : ";
cin>>choice;
switch(choice)
{ case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: return 0;
}
}while(option!=4);
getch();
}

void insert_element()
{
int num;
cout<<" Enter the item to be inserted : ";
cin>>num;
if(front==0 && rear==max-1)
cout<<" Queue OverFlow Occured ";
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=num;

}
else if(rear==max-1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
void delete_element()
{
int element;
if(front==-1)
{
cout<<" Underflow ";
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==max-1)
front=0;
else
front++;
cout<<" The deleted element is : "<<element;
}

}
void display_queue()
{
int k;
if(front==-1)
cout<<" No elements to display";
else
{
cout<<" The queue elements are : ";
for(k=front;k<=rear;k++)
{
cout<<" "<<queue[i];
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote