linked_list.cc - linked_list.cc functions for linked_list lab(cs221#include\"linked_list.h Inserts a new Node(with key=newKey at the head of the

linked_list.cc - linked_list.cc functions for linked_list...

This preview shows page 1 - 2 out of 4 pages.

// linked_list.cc -- functions for linked_list lab (cs221) #include "linked_list.h" /** * Inserts a new Node (with key=newKey) at the head of the linked_list. * PRE: head is the first node in a linked_list (if NULL, linked_list is empty) * PRE: newKey is the value for the key in the new Node * POST: the new Node is now the head of the linked_list */ void insert(Node*& head, int newKey) { Node * curr = new Node; curr->key = newKey; curr->next = head; head = curr; } /** * Print the keys of a linked_list in order, from head to tail * PRE: head is the first node in a linked_list (if NULL, linked_list is empty) */ void print(Node* head) { std::cout << "["; for (Node* curr = head; curr != NULL; curr = curr->next){ std::cout << curr->key; if (curr->next != NULL) std::cout << ", "; } std::cout << "]" << std::endl; } /** * Returns the size (number of Nodes) of the linked_list * PRE: head is the first node in a linked_list (if NULL, linked_list is empty) */ int size(Node* head){ if (! head) return 0; return 1 + size(head->next); } /** * Copies the keys in a linked list to a vector. * 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.
Image of page 1
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture