#include "link2.h" #include size_t list_length(Node* head_ptr) // Precondition: head_ptr is the head pointer of a linked list // Postcondition: the value returned is the number of nodes in the linked // list // The list itself is unaltered - libraries used: stdlib.h { Node* cursor; size_t answer; answer = 0; for(cursor = head_ptr; cursor != NULL; cursor = cursor->link) answer++; return answer; } void list_head_insert(Node*& head_ptr, const Node& entry) /* Precondition: head_ptr is the head pointer of a linked list. Postcondition: a new node containing the given entry is added to the head of the list head_ptr now points to the head of the new, longer list Note: if there is not enough dynamic memory for the new list, then new_handler is called before changing the list */ { Node*insert_ptr; insert_ptr = new Node; insert_ptr->data = entry.data; insert_ptr->link = head_ptr; head_ptr = insert_ptr; } Node* list_locate(Node* head_ptr, size_t position) /* Precondition: head_ptr is the head pointer of a linked list and position > 0 Postcondition: the pointer returned points to the node at the specified position in the list. (the head node is poisiton 1. the next node is 2 and so on) If there is no such position then the null pointer is returned and the list itself is undeclared. Library facilities used: assert.h stdlib.h */ { Node *cursor; size_t i; assert(0link; return cursor; } void list_copy(Node* source_ptr, Node*& head_ptr, Node*& tail_ptr) /* Precondition: source_ptr is the head pointer of a linked list Postcondition: head_ptr and tail_ptr are the head and tail pointers for a new list that contains the same Cards as the list pointed to by source_ptr. THe original list is unaltered. Note: if there is insufficient dynamic memory to create the new list, then new_handler is called Library facilities used: stdlib.h */ { head_ptr = NULL; tail_ptr = NULL; // Handle the case of the empty list. if(source_ptr == NULL) return; // Make the head node for the newly created list, and put data into it list_head_insert(head_ptr, *source_ptr); tail_ptr = head_ptr; //Copy the rest of the nodes one at a time, adding the tail of a new list for(source_ptr = source_ptr->link; source_ptr!=NULL; source_ptr = source_ptr->link) { list_head_insert(tail_ptr, *source_ptr); tail_ptr = tail_ptr->link; } } void list_head_remove(Node*& head_ptr) /* Precondition: head_ptr is the head pointer of a linked list, with at least one node Postcondition: the head node has been removed and returned to the head head_ptr is now the head pointer of the new, shorter linked list */ { Node *remove_ptr; remove_ptr = head_ptr; head_ptr = head_ptr -> link; delete remove_ptr; } void list_remove(Node* previous_ptr) /* Precondition: previous_ptr points to a node in a linked list, and this is not the tail node of the list. Postcondition: the node after previous_ptr has been removed from the linked list */ { Node *remove_ptr; remove_ptr = previous_ptr->link; previous_ptr->link = remove_ptr->link; delete remove_ptr; } void list_clear(Node*& head_ptr) /* Precondition: head_ptr is the head pointer of a linked list Postcondition: all nodes of the list have been returned to the heap, and heap_ptr is now NULL Library function used: stdlib.h */ { while(head_ptr != NULL) list_head_remove(head_ptr); }