Can anyone tell me where I'm going wrong with this method? LLhead points to the head of a linked list that has ten elements in it. I am trying to push one onto it. The problem is that hwen I send LLhead to the print function it only prints out the value of temp. I know that the print function is working just fine, because I already tested it with a different function. I'm guessing that I am not linking temp together with head right, but I can't see what I'm doing wrong. Oh, and z is just a counter. Thanks in advance for any help.
z is a reference variable and as I said isn't the problem, because I already know it works from another function. I'm supposed to be building a queue and I am pushing the value of 33 onto the top.
I'm supposed to be building a queue and I am pushing the value of 33 onto the top.
You want to push it onto the top, so that temp becomes the new head? If that's the case, I don't think that's what the above code is doing. It looks to me like this push method is inserting temp between LLhead and the next 'links' object in the list.
If you want to push it onto the top, I think this could work (I'm no expert though):
1 2 3 4 5 6 7 8 9
// Create a new links object and set its value
links *temp = new links;
temp->value = 33;
// The new links object needs to be at the top, so it will point to the OLD head object
temp->next = LLhead;
// Make LLhead point to our new head (temp)
LLhead = temp;
You should then be able to pass LLhead to your print function to print the list. If that still doesn't work, perhaps you could post your print function. I know you said it worked in other cases, but that doesn't necessarily mean it's bulletproof...