PRE head is the first node in a linkedlist if NULL linkedlist is empty POST a

Pre head is the first node in a linkedlist if null

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

* PRE: head is the first node in a linked_list (if NULL, linked_list is empty) * POST: a new vector<int> containing the keys in the correct order has been returned. */ std::vector<int> to_vector(Node* head) { std::vector<int> result; for (Node* curr = head; curr != NULL; curr = curr->next){ result.push_back(curr->key); } return result; } /** * Delete the last Node in the linked_list * PRE: head is the first Node in a linked_list (if NULL, linked_list is empty) * POST: the last Node of the linked_list has been removed * POST: if the linked_list is now empty, head has been changed * POST: else head remains the first Node in the linked_list */ void delete_last_element(Node*& head){ // ******** WRITE YOUR CODE HERE ******** Node* curr = head;
Image of page 1
Node* temp = head; // Stores the previous Node if (curr == NULL) return; if (curr->next == NULL) { delete curr; curr = NULL; return; } while (curr->next != NULL) { temp = curr; curr = curr->next; } delete curr; // Delete the last element temp->next = NULL; // set the previous one's next to NULL } /** * Removes an existing Node (with key=oldKey) from the linked_list. * PRE: head is the first node in a linked_list (if NULL, linked_list is empty) * PRE: oldKey is the value of the key in the Node to be removed * PRE: if no Node with key=oldKey exists, the linked_list has not changed
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 3 pages?

  • Spring '19
  • The Key, #include, new node

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture