First, use code tags and indentation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void LinkedJobList::push( Job *j )
{
JobNode* newjob = new JobNode;
JobNode* ptr= new JobNode;
ptr=list;
newjob->Item=*j;
newjob->next=NULL;
for(int i=0; i<count; i++)
{
ptr=ptr->next;
}
ptr->next= newjob;
list=ptr;
count++;
}
struct JobNode
{
Job Item;
JobNode * next;
};
int count;
JobNode* list;
|
Lines 4 doesn't make sense. You create a JobNode object, storing its address on ptr, then on line 5 you overwrite this address by assigning list to ptr. Note that by writing
JobNode * ptr;
you already allocate space for the pointer. You could merge the two lines:
JobNode * ptr = list;
Your loop is correct, but it could be better written as:
1 2
|
while(ptr->next != NULL)
++ptr;
|
That way you don't even need the counter variable. Then on line 13, what you have to see is that your list is implemented this way:
list-->|node|--next-->|node|--next-->|node|--next-->0 |
So what you need to store on the pointer list is the first element, so that you can iterate over it by using each node's next pointer. So, the final code would be something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void LinkedJobList::push( Job * j)
{
// Find last node.
JobNode * ptr = list;
while(ptr->next != 0)
++ptr;
// Create new node and append to list.
ptr->next = new JobNode;
JobNode * newjob = ptr->next;
newjob->Item = *j;
newjob->next = 0;
}
|
Finally, if developing the list isn't the exercise itself, consider using
std::list
:
http://cplusplus.com/reference/stl/list/