please help

I'm trying to add the job to the end of the linked list . but i get this error
Unhandled exception at 0x00b28770 in Assignment2.exe: 0xC0000005: Access violation writing location 0x00000040.

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++;
}

also my here is my JobNode definition in my class
struct JobNode
{ Job Item;
JobNode * next;
};
int count;
JobNode* list; };
Any idea??? I tried to debug but as soon as I reach the line ptr->next= newjob; I get this error ..why??
Last edited on
closed account (zb0S216C)
Check for null.

Wazzak
check what for NULL?
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/
thank you so much for your help. I did the above modification but as soon as it reaches line 7(ptr=ptr->next) it gives this error Unhandled exception at 0x00b61226 in Assignment2.exe: 0xC0000005: Access violation reading location 0xcdcdce0d.
Oh right, I forgot something. If your list is empty, you have to create it. In your initialization:

1
2
// Your global list pointer.
list = 0;

Then your push function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void LinkedJobList::push( Job * j)
{
	JobNode * ptr = list;

	if(ptr == 0)
	{
		// If list is empty, create first node.
		list = new JobNode;
		ptr = list;
	}
	else {
		// Find last node and append a new node to it.
		while(ptr->next != 0)
			++ptr;

		ptr->next = new JobNode;
		ptr = ptr->next;
	}

	// Initialize node.
	newjob->Item = *j;
	newjob->next = 0;
}

Sorry.
closed account (D80DSL3A)
The next problem will occur because ++ptr; won't increment ptr to ptr->next, it will be incremented by sizeof(JobNode) instead.
Last edited on
Thank you so much for your help. I added list=NULL in the constructor yet as soon as it reaches line 16 it shows that error..Im going insane!!!
Did you get the access viloation using ++ptr? As fun2code has pointed out, this is wrong in this case.

But I can see the correct code earlier in this thread, too.

1
2
    while(ptr->next != 0)
        ptr = ptr->next;
Last edited on
Topic archived. No new replies allowed.