// =========================================================================== // Job and JobList // =========================================================================== #include using namespace std; // =========================================================================== //dataStructures(p. 8) class Job { // nothing special private: string name; int hour; public: Job(); Job(string name, int hour); void print(); }; Job::Job() : name(""), hour(0) { } Job::Job(string name, int hour) : name(name), hour(hour) { } void Job::print() { cout << "(" << this->name << ", " << this->hour << ")"; } // =========================================================================== //dataStructures(p. 9) const int MAX_JOBS = 100; // a global variable class JobList { private: Job jobs[MAX_JOBS]; // where we store the data int count; // other attributes public: JobList(); // interfaces int getCount(); // should we have a setter? void print(); bool insert(Job job, int index); bool remove(int index); }; JobList::JobList() : count(0) { } int JobList::getCount() { return this->count; } void JobList::print() { for(int i = 0; i < this->count; i++) { cout << "Job " << i + 1 << ": "; this->jobs[i].print(); cout << endl; } } // =========================================================================== //dataStructures(p. 10) bool JobList::insert(Job job, int index) { if(index < 0 || this->count == MAX_JOBS)// fail to insert return false; else if(index > this->count) // fail to insert return false; else // usual insertion { for(int i = count - 1; i >= index; i--) this->jobs[i+1] = this->jobs[i]; this->jobs[index] = job; } this->count++; return true; } // =========================================================================== //dataStructures(p. 11) bool JobList::remove(int index) { if(index < 0 || this->count == 0 || index > this->count) return false; // nothing to remove else // usual removal { for(int i = index; i < this->count - 1; i++) this->jobs[i] = this->jobs[i+1]; this->count--; // the effective action of removal return true; } } // =========================================================================== //dataStructures(p. 12) int main() { JobList l; Job j1("Programming", 10); Job j2("Accounting", 20); l.insert(j1, 0); l.insert(j2, 1); l.print(); l.remove(0); l.print(); return 0; } // =========================================================================== // End of Job and JobList // =========================================================================== // =========================================================================== // Job and JobLinkedList // =========================================================================== #include using namespace std; // =========================================================================== //dataStructures(p. 17) class Job { friend class JobLinkedList; // discussed later private: string name; int hour; Job* next; // pointing to the next job public: // has the next job only if put in a list Job(); Job(string name, int hour); void print(); }; Job::Job() : name(""), hour(0), next(nullptr) { } Job::Job(string name, int hour) : name(name), hour(hour), next(nullptr) { } void Job:: print() { cout << "(" << this->name << ", " << this->hour << ")"; } // =========================================================================== //dataStructures(p. 18) class JobLinkedList { protected: int count; Job* head; // address of the first Job public: JobLinkedList(); ~JobLinkedList(); // same interfaces int getCount(); bool insert(Job job, int index); bool remove(int index); void print(); }; JobLinkedList::JobLinkedList() : count(0), head(nullptr) { } int JobLinkedList::getCount() { return this->count; } // =========================================================================== //dataStructures(p. 22) void JobLinkedList::print() { Job* temp = this->head; for(int i = 0; i < this->count; i++) { cout << "Job " << i + 1 << ": ";// print out one job temp->print(); cout << endl; temp = temp->next; // move to the next job } } // =========================================================================== //dataStructures(p. 43) JobLinkedList::~JobLinkedList() // version 1 { Job* temp = this->head; Job* tempNext = nullptr; // Do not write "Job* tempNext = this->head->next;" // If we do so, what happens on an empty list? for(int i = 0; i < this->count; i++) { tempNext = temp->next; delete temp; // release memory temp = tempNext; } } // =========================================================================== //dataStructures(p. 29) bool JobLinkedList::insert(Job job, int index) { if(index < 0) // fail-safe return false; else if(index == 0) // insert it as the head { Job* toInsert = new Job(job.name, job.hour); if(this->count > 0) toInsert->next = this->head; this->head = toInsert; } else // insert it somewhere in the list { if(index > this->count) // fail-safe return false; Job* toInsert = new Job(job.name, job.hour); Job* temp = this->head; // find the place for(int i = 0; i < index - 1; i++) temp = temp->next; toInsert->next = temp->next; // insertion temp->next = toInsert; } this->count++; return true; } // =========================================================================== //dataStructures(p. 36) bool JobLinkedList::remove(int index) { if(index < 0 || this->count == 0) return false; // return an empty job else if(index <= 1) { Job* temp = this->head; // removal this->head = temp->next; delete temp; } else { Job* temp = this->head; // find the place for(int i = 0; i < index - 2; i++) temp = temp->next; Job* tempNext = temp->next; // removal temp->next = tempNext->next; delete tempNext; } this->count--; return true; } // =========================================================================== int main() { JobLinkedList l; Job j1("Programming", 10); Job j2("Accounting", 20); l.insert(j1, 0); l.insert(j2, 1); l.print(); l.remove(0); l.print(); return 0; } // =========================================================================== // End of Job and JobLinkedList // =========================================================================== // =========================================================================== // JobStack // =========================================================================== //dataStructures(p. 50) class JobStack : protected JobLinkedList // protected: we want to hide insert() // and remove() inherited from JobLinkedList { public: JobStack(); ~JobStack(); bool push(Job job); bool pop(); void print(); }; JobStack::JobStack() : JobLinkedList() { } JobStack::~JobStack() { } // =========================================================================== //dataStructures(p. 51) void JobStack::print() // You need print() due to protected inheritance { JobLinkedList::print(); } bool JobStack::push(Job job) // insert at top (end) { return JobLinkedList::insert(job, this->count); } bool JobStack::pop() // remove the one at top (end) { return JobLinkedList::remove(this->count); } // =========================================================================== //dataStructures(p. 52) int main() { JobStack s; Job j1("Programming", 10); Job j2("Accounting", 20); s.push(j1); s.push(j2); s.print(); s.pop(); s.print(); return 0; }