// implements functions in list.h #include #include #include #include "list.h" // ======= W13 Tut Problems ======== // returns a copy of the linked list struct node *copy(struct node *head) { // replace this! return head; } // returns 1 if the two lists are identical, otherwise returns 0 int identical(struct node *head1, struct node *head2) { // replace this! return 0; } // returns 1 if list is in strictly increasing order, otherwise returns 0 int ordered(struct node *head) { return 0; // replace this } // given two lists in strictly increasing order, // return a new linked list (in strictly increasing order) of the elements in both set1 and set2 struct node *set_intersection(struct node *set1, struct node *set2) { return NULL; // replace this } // given two linked lists in strictly increasing order, // return a new linked list (in strictly increasing order) of the elements in either set1 or set2 // no duplicates (only include them once) struct node *set_union(struct node *set1, struct node *set2) { return NULL; // replace this } // ================================= // given an array of integer values for data items // returns the head of a linked list with these values // in the order as they appear in the array // size gives the size of the array struct node *create_list(int values[], int size) { int i = 0; if (size <= 0) { // empty linked list (or invalid size) return NULL; } struct node *head = NULL; // head of the linked list struct node *tmp = NULL; // temporary item we're working with struct node *curr = NULL; // current node we're looking at in the linked list while (i < size) { tmp = create_node(values[i]); // link this node onto our list if (head == NULL) { // this is the first node in the list head = tmp; curr = head; } else { // this is not the first node in the list // add it to the end, i.e. afer curr curr->next = tmp; // reset curr to point at the new last node curr = tmp; } i++; } return head; } // creates a struct node with the given data value // returns a pointer to this node struct node *create_node(int dat) { struct node *new = malloc(sizeof(struct node)); assert(new != NULL); new->data = dat; new->next = NULL; return new; } // prints out the list given the head of a list void print_list(struct node *head) { struct node *curr; curr = head; while (curr != NULL) { printf("[ %d ]-->", curr->data); curr = curr->next; } printf("NULL\n"); } // frees all the elements in a linked list void free_list(struct node *head) { if (head == NULL) { return; } free_list(head->next); free(head); } // return the number of items in the linked list int num_items(struct node *head) { struct node *curr = head; int count = 0; while (curr != NULL) { count++; curr = curr->next; } return count; }