I'm trying to insert an item into a linked list. I used code that I wrote last year for a similar program, but the code doesn't want to work now. I keep getting a "bus error", and I believe that it has something to do with something being set to zero when it should be set to NULL.
//here curptr == NULL
curptr -> next = new NodeType; // NULL -> next == crash
I think you have found out the reason yourself. currptr == NULL and then you attempt to reference the -> next member of cuz it crashes.
If I understand correctly, you want to add a new node right AFTER the last node is it ? Then in your earlier while loop you should keep a prevPtr instead.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
NodeType* curptr = firstptr;
NodeType* prevPtr = NULL;
while (curptr != NULL && curptr != 0)
{
prevPtr = currPtr;
curptr = curptr -> next;
}
//here currptr = NULL but prevPtr point to the last node
if (prevPtr!=NULL) prevPtr->next = new NodeType;
...