I want to learn this

My teacher gave me this code but I don't know exactly what the code is doing:



newPtr->nextPtr = tempPtr->nextPtr;





I was asking if someone could please explain this code to e so i can understand it better.
Last edited on
What variable type is newPtr and tempPtr?
Do they have a structure definition or is there a class definition?
Can you give the context of when this code is executed?
This looks to be part of an iteration program designed to add or remove a node in some sort of data structure.

There is a pointer that holds another pointer that is being assigned the address of a pointer that is being pointed to by another pointer.

Without the full code its difficult to decipher.
ListNode <NODETYPE> *tempPtr = firstPtr;
for (int i =0; i< n -1 ;i++)
tempPtr= tempPtr ->nextPtr;


newPtr->nextPtr = tempPtr->nextPtr;
tempPtr->nextPtr = newPtr;


more of the code
from my opinion:

firstPtr and newPtris Listnode type / class.

1
2
newPtr->nextPtr = tempPtr->nextPtr;
tempPtr->nextPtr = newPtr;


this simply assign the member of tempPtr i.e. nextPtr to the newPtr member (nextPtr). and so more alike...

note:
learn class and pointer

CMIIW
Looks to me like it is inserting a new node at index n-2. So if there were n nodes in the list, then the code would insert newPtr as the second to last element in the list.
Last edited on
Topic archived. No new replies allowed.