#include #include using namespace std ; #include "Time.h" #include "PrintJob.h" #include "node.h" #include "PrinterQueue.h" // Member Functions // Pre - none // Post - create empty list / queue // Constructor PrinterQueue::PrinterQueue() { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ this->front = (node*) NULL ; } // Pre - other queue exists // Post - copy 'other' queue into 'this' queue // Copy Constructor PrinterQueue::PrinterQueue(const PrinterQueue & other) { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ if(other.front == NULL) { this->front = NULL ; } else { node * curInOther = other.front ; node * prevInThis = NULL ; while(curInOther != NULL) { node * newnode = new node() ; newnode->data = curInOther->data ; if(prevInThis != NULL) { prevInThis->next = newnode ; prevInThis = newnode ; } else { this->front = newnode ; prevInThis = newnode ; } curInOther = curInOther->next ; } if(prevInThis != NULL) { prevInThis->next = NULL ; } } } // Pre - none // Post - delete all nodes still in queue (don't need to delete data cause it's not a pointer) // Destructor PrinterQueue::~PrinterQueue() { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ node * cur = this->front ; node * prev = NULL ; while(cur != NULL) { prev = cur ; cur = cur->next ; delete prev ; } } // Pre - 'addme' print job is filled // Post - returns true if was able to Enqueue 'addme' print job // we allow duplicates, so this should just always return true bool PrinterQueue::Enqueue(const PrintJob & addme) { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ node * newnode = new node() ; newnode->data = addme ; if(this->IsEmpty() == true) { // empty list newnode->next = NULL ; this->front = newnode ; } else { node * cur = this->front ; node * prev = NULL ; // go until we find the same or smaller priority job while(cur != NULL && cur->data.GetPriority() > newnode->data.GetPriority()) { prev = cur ; cur = cur->next ; } // sort based on time while(cur != NULL && cur->data.GetPriority() == newnode->data.GetPriority() && cur->data.GetPrintTime() <= newnode->data.GetPrintTime()) { prev = cur ; cur = cur->next ; } if(prev == NULL) { // new front this->front = newnode ; newnode->next = cur ; } else { prev->next = newnode ; newnode->next = cur ; } } return true ; } // Pre - none // Post - returns true if was able to Dequeue a print job // only time it would return false is if the Printer Queue is empty // also modified the 'printedme' job so it returns a COPY of the job that was printed bool PrinterQueue::Dequeue(PrintJob & printedme) { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ if(this->IsEmpty() == true) { return false ; } else { printedme = this->front->data ; node * oldfront = this->front ; this->front = this->front->next ; delete oldfront ; return true ; } } // Pre - none // Post - returns true if Printer Queue is empty, false if it is not bool PrinterQueue::IsEmpty( ) const { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ return this->front == NULL ; } // Pre - none // Post - returns true if it was able to find the next print job // only time it would return false is if the Printer Queue is empty // also modifies the 'nexttoprint' job so it returns a COPY of the job that was printed bool PrinterQueue::NextToPrint(PrintJob & nexttoprint) const { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ if(this->IsEmpty() == true) { return false ; } else { nexttoprint = this->front->data ; return true ; } } // Friend Functions // Pre - none // Post - prints whats in the Printer Queue, sorted by Priority (highest first) // the Queue should already be sorted, so this just needs to walk through and print the list ostream & operator<<(ostream & out, const PrinterQueue & printme) { /* ********************* */ /* YOU CODE THIS FOR HW3 */ /* ********************* */ node * cur = printme.front ; int counter = 0 ; while(cur != NULL) { counter++ ; cout << counter << ".) " << cur->data << endl ; cur = cur->next ; } return out ; }