Here's a copy of what I sent my cousin's husband, who's going to try and help me with this:
I worked on the program a bit more tonight. Pretty much the only thing I haven't really completed in the program is the insert and delete functions. I'll include some attachments with my header file and definition file.
Here's a few insecurities with the program that I am having:
1. in my copy constructor - is this line correct?
lastPtr->next = new Scheduler::Task(origPtr->PT);
should it be pointing to PT?
2. With the afterme function, what I'm trying to do is return a pointer to point to the location where the new task should be inserted, but the problem is I haven't figured out how to correctly implement this. I'm assuming I need to have some sort of if statement or while loop that will cycle through each task, until it finds one who's nextlink is pointing to a value with a lower PT, and then the insert function should insert the new task wherever afterme is pointing to. I think my logic is okay, but I still haven't figured this one out, I'll try to work on it tomorrow.
3. I'm sort of concerned about my counter and whether i'm using the increment functions correctly.
//----Definition of incCounter()
void Scheduler::incCounter()
{
counter++;
}
//-----Definition of decCounter()
void Scheduler::decCounter()
{
counter--;
}
since counter is a private member, am I allowed to mess with it like this?
Also, I haven't implemented anything for ID yet, but what the plan is is to just start with 1 as the ID for the first task, and just increase it by 1 and set that ID to the task as I keep inserting tasks. It wouldn't be the same as counter because the ID wouldn't be decremented at any point, only assigned a new ID that is 1 greater than the last task that was created.
Another thing I'm not too sure about - with the way I'm writing this program so far, it seems as though I wouldn't be able to place a new task at the beginning of the list? Another little nuissance I probably have to take care of.
I originally told you it was a doubly linked list, but upon some consideration I decided it might be easier to keep it singly linked for now. Maybe if I get some of the other functionalities working, I can use some time and make it doubly linked.
Please let me know if you spot any inconsistencies or weird shit in my code. I'm pretty new to classes so I wouldn't be surprised if I did something incorrectly syntactically or logically.
I also haven't started the driver program to test out all the functions. I figure I'll wait till I have the scheduler atleast somewhat functional before I start testing it.
scheduler.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*----scheduler.h-----------------------------------------------------
This header file defines a Scheduler data type.
Basic Operations include:
constructor: Constructs a schedule
empty: checks if there are no tasks scheduled
display: Displays tasks in the schedule
first: accesses first task
copy constructor: copies the given schedule
destructor: Deletes the schedule after execution of program
insert: inserts a task into the schedule
delete: deletes a task in the schedule
---------------------------------------------------------------------*/
#include <iostream>
using namespace std;
#ifndef SCHEDULER
#define SCHEDULER
typdef int Element
class Scheduler
{
public:
//function members
//constructor
Scheduler();
/*-------------------------------------------------------------------
Constructs scheduler object. first_task initialized to a null pointer
--------------------------------------------------------------------*/
~Scheduler(); //Class Destructor
/********Copy Constructor******/
Scheduler(const Schedule & origSched);
/******Assignment******/
const Schedule & operator=(const Scheduler & rightHandSide);
/******Empty Function****/
bool empty() const; //checks if stack is empty. Returns true/false.
/*******Insert Function*****/
void insert(Task afterme, const Element &value_ID, const Element &value_PT);
/********First Function*******/
Element first() const;
/********Delete Function********/
void
/*********Display Function*****/
void display(ostream &out) const;
/***********Accessor Function****/
int getCounter() const;
/***********Mutator Function*****/
void incCounter(); //increments counter by 1 when adding a task
void decCounter(); //decrements counter by 1 when removing a task
TaskPointer afterme(TaskpPointer first_task, int target); //Will locate where to place new task based on PT
private:
int counter = 0; // Counts the number of tasks in the Scheduler
class Task
{
public:
/**********Accessor Functions*****/
int getID() const{
return ID;}
int getPT() const{
return PT;}
Task* getLink() const {
return next;}
//Task constructor
Task(Element ID_val, Element PT_val, Task *nextLink = 0);
: ID(ID_val), PT(PT_val), next(nextLink)
{}
void setNextTask(Task *pointer){
nextLink = pointer;
}
Element ID;
Element PT;
Task *next;
Task *previous;
};
typedef Task *TaskPointer;
//Data Members
TaskPointer first_task; //points to first task
};
//------ Prototype of output operator
ostream & operator<< (ostream & out, const Schedule & aSchedule);
#endif
|
scheduler.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//------Scheduler.cpp-----------------------------
#include <iostream>
using namespace std;
#include "scheduler.h"
#define MAX_TASK 15
//-----Definition of Scheduler Constructor
Scheduler::Scheduler()
: first_task(0)
{}
//------Definition of Scheduler copy constructor
Scheduler(const Schedule & origSched);
{
first_task = 0;
if(!origSched.empty()){
//Copy first task
first_task = new Scheduler::Task(origSched.first());
// Sets points to run through linked list
Scheduler::TaskPointer lastPtr = first_task,
origPtr = origSched.first->next;
while (origPtr != 0)
{
lastPtr->next = new Scheduler::Task(origPtr->PT);
lastPtr = lastPtr->next;
origPtr = origPtr->next;
}
}
}
//----Definition of Stack Destructor
Scheduler::~Scheduler()
{
// Sets pointers to run through the scheduler
Scheduler::TaskPointer currPtr = first_task
nextPtr;
while(currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
//------- Definition of afterme function
TaskPointer afterme(TaskPointer first_task, target)
{
TaskPointer here = first_task;
if(here == NULL) //if empty list
{
return NULL;
}
else
{
while(here->getPT() != target && here->getLink() != NULL)
here = here->getLink();
if(here->getPT() == target)
return here;
else
return NULL;
}
}
//---------Definition of insert function
void insert(TaskPointer afterme, int thePT)
{
if(counter == MAX_TASK)
cerr >> "Cannot add another task. Maximum task limit reached."<<endl;
else
{
afterme->setNextTask(new Task(thePT, afterme->getLink()))
}
}
//------Definition of assignment operator
const Scheduler &Scheduler::operator=(const Scheduler &rightHandSide)
{
if (this != &rightHandSide) //Checks to make sure both sides are not the same
{
this -> ~Scheduler(); //Destroys current schedule
if(rightHandSide.empty())
first_task = 0;
else
{
//Copy first task
first_task = new Scheduler::Task(rightHandSide.first());
//Set points to run through scheduler
Scheduler::TaskPointer lastPtr = myTop
rhsPtr = rightHandSide.first->next;
while (rhsPtr != 0)
{
lastPtr->next = new Stack::Node(rhsPtr->PT);
lastPtr = lastPtr->next;
rhsPtr = rhsPtr->next;
}
}
}
return *this;
}
//------- Definition of output operator
ostream & operator<< (ostream & out, const Scheduler & aScheduler)
{
aScheduler.display(out);
return out;
}
//----Definition of getCounter()
int Scheduler::getCounter() const
{
return counter;
}
//----Definition of incCounter()
void Scheduler::incCounter()
{
counter++;
}
//-----Definition of decCounter()
void Scheduler::decCounter()
{
counter--;
}
//----Definition of empty()
bool Scheduler::empty() const
{
return (first_task == 0);
}
//----Definition of display()
void Scheduler::display(ostream &out) const
{
Scheduler::TaskPointer ptr;
for (ptr = first_task; ptr != 0; ptr = ptr->next)
out << ptr -> ID << "("<<ptr -> PT <<")"<<endl;
}
//----Definition of first()
Element Scheduler::first() const
{
if(!empty())
return(first_task ->PT);
else
{
cerr <<"The Stack is empty"<<endl;
Element *temp = new(Element);
Element garbage = *temp;
delete temp;
return garbage;
}
}
|