100%(1)1 out of 1 people found this document helpful
This preview shows page 1 - 2 out of 2 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.