lundi 2 avril 2018

Simple Linked List interchanging with next->next

I've bumped in a strange occurrence while I was trying to construct a interchange function for two nodes in a Simple Linked List.

A List with 5 nodes. I passed the first node, and the 3rd:

//list is the head of the linked list
interchange(list, list->pNext->pNext);

Below is the only instruction that causes the problem, to make it simpler:

void interchange(SList*& p, SList*& q) {
    p->pNext->pNext = q->pNext->pNext;
}

My problem is that, from my understanding of coding, the left member in an equal operations gets the value of the right member. p->pNext->pNext takes the correct value as it should. But the pointer named "q" will point to q->pNext->pNext after this single instruction.

Can someone brilliant shed some light on this? Thank you in advance guys.

Aucun commentaire:

Enregistrer un commentaire