Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Student Name Honor Pledge Exercises We would like to insert a node into a double

ID: 3881998 • Letter: S

Question

Student Name Honor Pledge Exercises We would like to insert a node into a double-linked list and we are given references to the node to be inserted (newNode) and the node that will be after it in the list after the insertion (currNode), which is not the head of the list. Each node that is already in the list has references to the next and previous nodes. Which references have to be changed for the insertion to be successful and what should their new vals be Write the corresponding assignments. Do not assume any knowledge other than the references above Drawing the list is optional, but you may do it if it helps.

Explanation / Answer

Reference to be changed:

Sample Code:

void insert(Node *currNode, Node *newNode)

{

    // The next pointer of the node previous to the currNode. It should now point to newNode.

    currNode->getPrev()->setNext(newNode);

   

    // The prev pointer of newNode. It should now point to the node previous to the currNode.

    newNode->setPrev(currNode->getPrev());

   

    // The next pointer of the newNode. It should now point to currNode.

    newNode->setPrev(currNode);

   

    //The prev pointer of the currNode. It should now point to newNode.

    currNode->setPrev(newNode);

}