Question
I have a working project. I need help converting some header and source files into template. My files are: node.h, node.cpp, queue.h, queue.cpp, priorityqueue.h, priorityqueue.cpp.
I have a working project. I need help converting some header and source files into template. My files are: node.h, node.cpp, queue.h, queue.cpp, priorityqueue.h, priorityqueue.cpp. In all of those I need to combine node.h and node.cpp into a single .h file, using template. Same for queue.h and queue.cpp. As well as priorityqueue.h and priorityqueue.cpp. My code works as I need it to, but the final step is to convert them into templates. Need as soon as possible. At the end I should have only 3 template files, wherease now I have 6 untemplated ones. Thanks in advance! Here are the files:
*Edit*: I've added the queue.cpp file as well
//Node.h
#ifndef Node_h #define Node_h #include "Event.h"
class Node { public: //Declare a variable for node data. Event nData;
//Declare a pointer for next node. Node* nNext;
//Default Constructor. Node();
//Custom Constructor 1. Node(Event inData); //Custom Constructor 2. Node(Event inData, Node* inNextNode);
//Destructor. ~Node(); }; #endif
//Node.cpp
#include #include "Node.h"
//Default constructor. Node::Node() { nNext = NULL; //Assign next pointer as null. }
//Custom Constructor 1. Node::Node(Event inData) { //Initialize inData as iData. nData = inData; //Initialize null as nNext. nNext = NULL; } //Custom Constructor 2. Node::Node(Event inData, Node* inNextNode) { //Assign inData as nData. nData = inData; //Assign inNextNode as nodeNext. nNext = inNextNode; }
//Definition for destructor. Node::~Node() {}
//Queue.h
#ifndef Queue_h #define Queue_h #include "Event.h" #include "EmptyDataCollectionException.h" //Declare a constant variable. Can adjust this during testing. const unsigned int QUEUEMAX = 101;
class Queue { private: //Declare a variable for starting and ending position. unsigned int start; unsigned int end; Event* qData; //pointer for queue data public: //Default constructor. Queue();
//Deconstructor. ~Queue();
//isEmpty() method - checks if queue is empty. bool isEmpty() const;
//Enqueue() method - inputs data into queue at the back. bool Enqueue(const Event& newqData);
//Dequeue() method - removes data at front of queue. bool Dequeue();
//Peek() method - peek the data at front of queue and throws exception if queue is empty. Event Peek() const throw(EmptyDataCollectionException); }; #endif
//Queue.cpp
#include "Queue.h" #include
//Constructor. Queue::Queue() : start(0), end(0), qData(NULL) { //New queue. qData = new Event[QUEUEMAX]; }
//Deconstructor. Queue::~Queue() { delete[] qData; //Clean memory. qData = NULL; //Free data. }
//Definition for isEmpty(). bool Queue::isEmpty() const { //Check whether the queue is empty or not. if (start == end) { //Empty. return true; } //Otherwise, not empty. return false; }
//Definition for Enqueue(). bool Queue::Enqueue(const Event& newqData) { //Check whether the queue is full or not. if ((start % QUEUEMAX) == (end + 1) % QUEUEMAX) { //If full returns false, exits function. return false; } //If not full, following occurs: //Copy the data into array. qData[(end) % QUEUEMAX] = newqData; //Increment end position. end++; return true; }
//Definition for Dequeue(). bool Queue::Dequeue() { //Check whether the queue is empty or not. if (isEmpty()==true) { return false; } start++; return true; }
//Definition for Peek(). Event Queue::Peek() const throw(EmptyDataCollectionException) { //Check whether the queue is empty or not. if (start == end) { //Throw an exception. throw EmptyDataCollectionException("Queue is Empty"); }
//Return data at top of queue. return qData[start % QUEUEMAX]; }
//myPriorityQueue.h
#ifndef PriorityQueue_h #define PriorityQueue_h #include "Event.h" #include "EmptyDataCollectionException.h" #include "Node.h"
class PriorityQueue { private: //Declare a pointer for pqHead node. Node* pqHead;
public: //Constructor. PriorityQueue();
//Destructor. ~PriorityQueue();
//isEmpty() method - checks if priority queue is empty. bool isEmpty() const;
//Enqueue() method - puts next data in prio queue based on priority. bool Enqueue(const Event& newqData);
//Dequeue() method - removes the data at front of queue. bool Dequeue();
//Peek() method - peeks the front of queue, if empty throws an exception. Event Peek() const throw(EmptyDataCollectionException); }; #endif
//PriorityQueue.cpp
#include "PriorityQueue.h" #include
//Constructor. PriorityQueue::PriorityQueue() : pqHead(NULL) {}
//Destructor. PriorityQueue::~PriorityQueue() { //Declare a pointer for pqTail node. Node* pqTail; //Declare a pointer for current node. Node* current = pqHead; //Runs on loop while current !=NULL, ie stops when current = NULL while (current != NULL) { pqTail = current; //Update pqTail current node. current = current->nNext; //Update current node as current node next. delete pqTail; //Remove the pqTail node. } pqHead = NULL; //Update pqHead node as null. }
//Definition for isEmpty(). bool PriorityQueue::isEmpty() const { //Check whether the priority queue is empty or not. if (pqHead == NULL) { return true; //If empty return true } return false; //Otherwise return false }
//Definition for Enqueue(). bool PriorityQueue::Enqueue(const Event& incData) { //Check whether the incData is a valid data, ie if its NULL. if (&incData == NULL) { // if its NULL, the if statement returns false and the function is exited return false; } // Check for validity of pqHead and compare time value of incData vs pqHead.nData if (pqHead == NULL || incData.getTime() < pqHead->nData.getTime()) { //Allocate memory for temp node. Node* tempNode = new Node(incData, pqHead); //Update pqHead node as temp node. pqHead = tempNode; //Return true. return true; } //Update current node as pqHead node. Node* current = pqHead; //Update previous node as pqHead node. Node* previous = pqHead;
//Next line executes until all data is arranged in order of priority while (current != NULL && (incData.getTime() > current->nData.getTime())) { //Swap previous and current previous = current; current = current->nNext; }
//Condition check. if (current != NULL && (incData.getType() == 'D' && current->nData.getType() == 'A')) { //Condition check. if (incData.getTime() == current->nData.getTime()) { //Update previous node as current node. previous = current;
//Update current node as current node next. current = current->nNext; } } // Make a tempNode to store data Node* tempNode = new Node(incData, previous->nNext); //Update previous node.next as temp node. previous->nNext = tempNode; return true; }
//Definition for Dequeue(). bool PriorityQueue::Dequeue() { //Check whether the priority queue is empty or not. if (isEmpty() == true) { return false; } else { //Allocate memory for pqHead node next node. Node* headNext = pqHead->nNext; delete pqHead; //Delete the pqHead node. pqHead = headNext; //Update headNext as pqHead. } return true; }
//Definition for Peek(), throws exception when prio queue is empty Event PriorityQueue::Peek() const throw(EmptyDataCollectionException) { //Check whether the priority queue is empty or not. if (isEmpty() == true) { //Throw an exception. throw EmptyDataCollectionException("PriorityQueue is Empty"); } return pqHead->nData; //Return the data in front of priority queue }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started