Node cnode head while cnode0 NULL cnode0 key oldKey cnode cnode0 next if cnode0

Node cnode head while cnode0 null cnode0 key oldkey

This preview shows page 2 - 3 out of 3 pages.

Node **cnode = &head; while (cnode[0] != NULL && cnode[0]->key != oldKey){ cnode = &cnode[0]->next; } if (cnode[0] == NULL) return; delete *cnode; *cnode = cnode[0]->next; } /** * Insert a new Node (with key=newKey) after an existing Node (with key=oldKey) * If there is no existing Node with key=oldKey, then no action. * PRE: head is the first Node in a linked_list (if NULL, linked_list is empty) * PRE: oldKey is the value to look for (in the key of an existing Node) * PRE: newKey is the value of the key in the new Node (that might be inserted) * POST: If no Node with key=oldKey was found, then the linked_list has not changed * POST: Else a new Node (with key=newKey) is right after the Node with key = oldKey. */ void insert_after(Node* head, int oldKey, int newKey){ // ******** WRITE YOUR CODE HERE ******** Node *cnode = head; Node *newNode = new Node; newNode->key = newKey; while (cnode->next != NULL && cnode->key != oldKey){ cnode = cnode->next; } if (cnode->key == oldKey){ newNode->next = cnode->next; cnode->next = newNode; } return; } /** * Create a new linked_list by merging two existing linked_lists. * PRE: list1 is the first Node in a linked_list (if NULL, then it is empty) * PRE: list2 is the first Node in another linked_list (if NULL, then it is empty) * POST: A new linked_list is returned that contains new Nodes with the keys from
Image of page 2
* the Nodes in list1 and list2, starting with the key of the first Node of list1, * then the key of the first Node of list2, etc. * When one list is exhausted, the remaining keys come from the other list. * For example: [1, 2] and [3, 4, 5] would return [1, 3, 2, 4, 5] */ Node* interleave(Node* list1, Node* list2){ // ******** WRITE YOUR CODE HERE ******** Node *newList = new Node; if (list1 != NULL) { newList->key = list1->key; if (list2 != NULL) { newList->next = interleave(list2, list1->next); } else { newList->next = interleave(list1->next, NULL); } } else { if (list2 != NULL) { newList->key = list2->key; newList->next = interleave(NULL, list2->next); } else { newList = NULL; } } return newList; }
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture