Question: C++ Programming. I already have the rest of the code written out (4 header files, 4 cpp files) just copy and paste the header and
C++ Programming. I already have the rest of the code written out (4 header files, 4 cpp files) just copy and paste the header and cpp files below into a project and complete the 2 "Exercises". Please make sure both parts are fully answered and that you upload copyable code for each cpp or header file that was edited. Make sure it runs and compiles so I can give 5 stars <3
Exercises
- implement the Queue ADT (50 points) using array based approach and Ex 2 and Ex 3
- implement the Queue ADT (50 points) using LinkedList based approach and Ex 2 and Ex 3
test7.cpp:
//-------------------------------------------------------------------- // // Laboratory 7 test7.cpp // // Test program for the operations in the Queue ADT // //--------------------------------------------------------------------
#include
#include "config.h"
using namespace std;
#if LAB7_TEST1 # include "QueueLinked.cpp" #else # include "QueueArray.cpp" #endif
//--------------------------------------------------------------------
void print_help();
//--------------------------------------------------------------------
template
print_help();
do { try { testQueue.showStructure(); // Output queue
cout << endl << "Command: "; // Read command cin >> cmd; if ( cmd == '+' || cmd == '>' ) cin >> testData;
switch ( cmd ) { case 'H' : case 'h' : print_help(); break;
case '+' : // enqueue cout << "Enqueue " << testData << endl; testQueue.enqueue(testData); break;
case '-' : // dequeue cout << "Dequeued " << testQueue.dequeue() << endl; break;
case 'C' : case 'c' : // clear cout << "Clear the queue" << endl; testQueue.clear(); break;
case 'E' : case 'e' : // empty if ( testQueue.isEmpty() ) cout << "Queue is empty" << endl; else cout << "Queue is NOT empty" << endl; break;
case 'F' : case 'f' : // full if ( testQueue.isFull() ) cout << "Queue is full" << endl; else cout << "Queue is NOT full" << endl; break;
#if LAB7_TEST2 case '>' : // Programming Exercise 2 cout << "Put " << testData << " in front " << endl; testQueue.putFront(testData); break;
case '=' : // Programming Exercise 2 cout << "Got " << testQueue.getRear() << " from rear " << endl; break; #endif
#if LAB7_TEST3 case '#' : // Programming Exercise 3 cout << "Length = " << testQueue.getLength() << endl; break; #endif
case 'Q' : case 'q' : // Quit test program break;
default : // Invalid command cout << "Inactive or invalid command" << endl; } } catch (logic_error e) { cout << "Error: " << e.what() << endl; } } while ( cin && cmd != 'Q' && cmd != 'q' );
if( !cin ) { cout << "input error" << endl; }
}
//--------------------------------------------------------------------
int main() { #if !LAB7_TEST1 cout << "Testing array implementation" << endl; QueueArray
return 0; }
//--------------------------------------------------------------------
void print_help() { cout << endl << "Commands:" << endl; cout << " H : Help (displays this message)" << endl; cout << " +x : Enqueue x" << endl; cout << " - : Dequeue" << endl; cout << " C : Clear the queue" << endl; cout << " E : Empty queue?" << endl; cout << " F : Full queue?" << endl; cout << " >x : Put x at front (" #if LAB7_TEST2 << " Active " #else << "Inactive " #endif << ": Programming Exercise 2)" << endl; cout << " = : Get x from rear (" #if LAB7_TEST2 << " Active " #else << "Inactive " #endif << ": Programming Exercise 2)" << endl; cout << " # : Length (" #if LAB7_TEST3 << " Active " #else << "Inactive " #endif << ": Programming Exercise 3)" << endl; cout << " Q : Quit the test program" << endl; cout << endl; }
show7.cpp:
//-------------------------------------------------------------------- // show7.cpp: includes implementation of showStructure //--------------------------------------------------------------------
//--------------------------------------------------------------------
#include "Queue.h" #include "QueueArray.h" #include "QueueLinked.h"
template < class DT > void Queue
// Linked list implementation. Outputs the elements in a queue. If // the queue is empty, outputs "Empty queue". This operation is // intended for testing and debugging purposes only.
{ QueueNode
if ( isEmpty() ) cout << "Empty queue" << endl; else { cout << "Front\t"; for ( p = front ; p != 0 ; p = p->next ) { if( p == front ) { cout << '[' << p->dataItem << "] "; } else { cout << p->dataItem << " "; } } cout << "\trear" << endl; } }
//--------------------------------------------------------------------
template
{ int j; // Loop counter
if ( front == -1 ) cout << "Empty queue" << endl; else { cout << "Front = " << front << " Back = " << back << endl; for ( j = 0 ; j < maxSize ; j++ ) cout << j << "\t"; cout << endl; if ( back >= front ) for ( j = 0 ; j < maxSize ; j++ ) if ( ( j >= front ) && ( j <= back ) ) cout << dataItems[j] << "\t"; else cout << " \t"; else for ( j = 0 ; j < maxSize ; j++ ) if ( ( j >= front ) || ( j <= back ) ) cout << dataItems[j] << "\t"; else cout << " \t"; cout << endl; } }
QueueLinked.cpp:
#include "QueueLinked.h"
template
template
template
template
template
template
template
template
template
template
template
template
template
template
if ( isEmpty() ) cout << "Empty queue" << endl; else { cout << "Front\t"; for ( p = front ; p != 0 ; p = p->next ) { if( p == front ) { cout << '[' << p->dataItem << "] "; } else { cout << p->dataItem << " "; } } cout << "\trear" << endl; } }
QueueArray.cpp:
#include "QueueArray.h"
template
template
template
template
template
template
template
template
template
template
template
template
//--------------------------------------------------------------------
template
{ int j; // Loop counter
if ( front == -1 ) cout << "Empty queue" << endl; else { cout << "Front = " << front << " Back = " << back << endl; for ( j = 0 ; j < maxSize ; j++ ) cout << j << "\t"; cout << endl; if ( back >= front ) for ( j = 0 ; j < maxSize ; j++ ) if ( ( j >= front ) && ( j <= back ) ) cout << dataItems[j] << "\t"; else cout << " \t"; else for ( j = 0 ; j < maxSize ; j++ ) if ( ( j >= front ) || ( j <= back ) ) cout << dataItems[j] << "\t"; else cout << " \t"; cout << endl; } }
QueueLinked.h:
// QueueLinked.h
#include
using namespace std;
#include "Queue.h"
template
void enqueue(const DataType& newDataItem) throw (logic_error); DataType dequeue() throw (logic_error);
void clear();
bool isEmpty() const; bool isFull() const;
// Programming Exercise 2 void putFront(const DataType& newDataItem) throw (logic_error); DataType getRear() throw (logic_error); // Programming Exercise 3 int getLength() const;
void showStructure() const;
private: class QueueNode { public: QueueNode(const DataType& nodeData, QueueNode* nextPtr);
DataType dataItem; QueueNode* next; };
QueueNode* front; QueueNode* back; };
QueueArray.h:
// QueueArray.h
#ifndef QUEUEARRAY_H #define QUEUEARRAY_H
#include
using namespace std;
#include "Queue.h"
template
void enqueue(const DataType& newDataItem) throw (logic_error); DataType dequeue() throw (logic_error);
void clear();
bool isEmpty() const; bool isFull() const;
void putFront(const DataType& newDataItem) throw (logic_error); DataType getRear() throw (logic_error); int getLength() const;
void showStructure() const;
private: int maxSize; int front; int back; DataType* dataItems; };
#endif
Queue.h:
//-------------------------------------------------------------------- // // Laboratory 7 Queue.h // // Class declaration of the abstract class interface to be used as // the basis for implementations of the Queue ADT. // //--------------------------------------------------------------------
#ifndef QUEUE_H #define QUEUE_H
#include
using namespace std;
#pragma warning( disable : 4290 )
//--------------------------------------------------------------------
template
virtual ~Queue();
virtual void enqueue(const DataType& newDataItem) throw (logic_error) = 0; virtual DataType dequeue() throw (logic_error) = 0;
virtual void clear() = 0;
virtual bool isEmpty() const = 0; virtual bool isFull() const = 0;
// The conditional compilation tests below are very important. // Because the functions declared are pure virtual functions, if they // are declared in the base class, then they MUST be implemented in any // derived classes. But they are supposed to be optional implementations. // Consequently, they must only be declared here if they are being // implemented in the derived classes. #if LAB7_TEST2 virtual void putFront(const DataType& newDataItem) throw (logic_error) = 0; virtual DataType getRear() throw (logic_error) = 0; #endif #if LAB7_TEST3 virtual int getLength() const = 0; #endif
virtual void showStructure() const = 0; };
template
#endif // #ifndef QUEUE_H
config.h:
/** * Queue class (Lab 7) configuration file. * Activate test #N by defining the corresponding LAB7_TESTN to have the value 1. */
#define LAB7_TEST1 0 // 0 => use array implementation, 1 => use linked impl. #define LAB7_TEST2 0 // Programming exercise 2: putFront and getRear #define LAB7_TEST3 0 // Programming exercise 3: getLength
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
