// =========================================================================== // Data Structures // =========================================================================== #include #include #include using namespace std; const int MAX_JOBS = 100; class Job { private: string name; int hour; public: Job() { this->name = ""; this->hour = 0; } Job(string name, int hour) { this->name = name; this->hour = hour; } void setHour(int hour) { this->hour = hour; } string getName() { return this->name; } double getHour() { return this->hour; } void print(); }; void Job::print() { cout << "(" << this->name << ", " << this->hour << ")"; } class JobList { private: Job jobs[MAX_JOBS]; int count; public: JobList() { this->count = 0; } int getCount() { return this->count; } bool insert(Job job, int index); // if index < 0 or this->count == MAX_JOBS, return false. // if 0 <= index <= this->count, put job at jobs[index]. Return true. // if index > this->count, put job at jobs[this->count]. Return true. Job remove(int index); void print(); }; bool JobList::insert(Job job, int index) { if(index < 0 || this->count == MAX_JOBS) return false; else if(index > this->count) this->jobs[this->count] = job; else { for(int i = this->count - 1; i >= index; i--) this->jobs[i+1] = this->jobs[i]; this->jobs[index] = job; } this->count++; return true; } Job JobList::remove(int index) { Job toRemove; if(index < 0 || this->count == 0) return toRemove; else if(index > this->count) toRemove = this->jobs[this->count]; else { toRemove = this->jobs[index]; for(int i = index; i < this->count - 1; i++) this->jobs[i] = this->jobs[i+1]; } this->count--; return toRemove; } void JobList::print() { for(int i = 0; i < this->count; i++) { cout << "Job " << i + 1 << ": "; this->jobs[i].print(); cout << endl; } } int main() { JobList l; Job j1("Programming", 2); Job j2("Sleep", 1); Job j3("Calculus", 3); l.insert(j1, 0); l.insert(j2, 1); l.insert(j3, 2); l.print(); cout << endl; Job temp; temp = l.remove(1); temp.print(); cout << " is removed" << endl; temp = l.remove(1); temp.print(); cout << " is removed" << endl; cout << endl; l.print(); cout << endl; return 0; } // ======================================= class Job { friend class JobLinkedList; private: string name; int hour; Job* next; public: Job() { this->name = ""; this->hour = 0; this->next = NULL; } Job(string name, int hour) { this->name = name; this->hour = hour; this->next = NULL; } void setHour(int hour) { this->hour = hour; } string getName() { return this->name; } double getHour() { return this->hour; } void print(); }; void Job::print() { cout << "(" << this->name << ", " << this->hour << ")"; } class JobLinkedList { private: int count; Job* head; public: JobLinkedList() { this->count = 0; this->head = NULL; } ~JobLinkedList(); int getCount() { return this->count; } bool insert(Job job, int index); /* insert(): if index < 0, return false. if 0 <= index <= this->count, put job at the (index+1)-th place. Return true. if index > this->count, put job at the (this->count+1)-th place. Return true. */ Job remove(int index); void print(); }; JobLinkedList::~JobLinkedList() { /* // ==== version 1 ==== Job* temp = this->head; Job* tempNext = NULL; for(int i = 0; i < this->count; i++) { temp->print(); cout << " is destructed" << endl; tempNext = temp->next; delete temp; temp = tempNext; } */ // ==== version 2 ==== while(this->count > 0) { Job job = this->remove(0); // job.print(); // cout << " is destructed" << endl; } } bool JobLinkedList::insert(Job job, int index) { Job* toInsert = new Job(job.name, job.hour); if(index > this->count) index = this->count; if(index < 0) return false; else if(index == 0) { if(this->count > 0) toInsert->next = this->head; this->head = toInsert; } else { Job* temp = this->head; for(int i = 0; i < index - 1; i++) temp = temp->next; // insert job between temp and temp->next toInsert->next = temp->next; temp->next = toInsert; } this->count++; return true; } Job JobLinkedList::remove(int index) { Job toRemove; if(index < 0 || this->count == 0) return toRemove; else if(index == 1) { toRemove = *(this->head); Job* temp = this->head; this->head = temp->next; delete temp; } else { Job* temp = this->head; for(int i = 0; i < index - 1; i++) temp = temp->next; // remove temp->next Job* temp2 = temp->next; temp->next = temp2->next; toRemove = *temp2; delete temp2; } this->count--; toRemove.next = NULL; return toRemove; } void JobLinkedList::print() { Job* temp = this->head; for(int i = 0; i < this->count; i++) { cout << "Job " << i + 1 << ": "; temp->print(); cout << endl; temp = temp->next; } } // ======================================= class JobStack : protected JobLinkedList { public: JobStack(); ~JobStack(); void push(Job cur); Job pop(); void print(); }; JobStack::JobStack() : JobLinkedList() { ; } JobStack::~JobStack() { // cout << "Before parent's destructor" << endl; } /* insert at top (end) */ void JobStack::push(Job job) { JobLinkedList::insert(job, this->count); } /* remove the one at top (end) */ Job JobStack::pop() { return JobLinkedList::remove(this->count); } void JobStack::print() { JobLinkedList::print(); }