// debug_remove_nth.c // This program removes the nth node of a linked list // Written by YOUR-NAME (YOUR-ZID), on TODAYS-DATE #include #include #include #include struct node { struct node *next; int data; }; struct node *delete_nth(struct node *head, int index); struct node *array_to_list(int len, char *array[]); void print_list(struct node *head); int get_list_length(struct node *head); // DO NOT CHANGE THIS MAIN FUNCTION int main(int argc, char *argv[]) { // create linked list from command line arguments excluding the program name struct node *head = NULL; if (argc > 0) { // list has elements head = array_to_list(argc - 1, &argv[1]); } int index; printf("What is the position of the node you wish to remove: "); scanf("%d", &index); printf("Original list: "); print_list(head); head = delete_nth(head, index); printf("Modified list: "); print_list(head); return 0; } // Deletes the second last node in a linked list // The deleted node is freed. // The head of the list is returned. struct node *delete_nth(struct node *head, int index) { struct node *current = head; int list_length = get_list_length(head); if (index > list_length) { printf("Index out of range, no node deleted\n"); return head; } int i = 0; while (current->next != NULL && i < index) { current = current->next; i++; } free(current->next); current->next = current->next->next; return head; } // DO NOT CHANGE THIS FUNCTION // create linked list from array of strings struct node *array_to_list(int len, char *array[]) { struct node *head = NULL; int i = len - 1; while (i >= 0) { struct node *n = malloc(sizeof(struct node)); assert(n != NULL); n->next = head; n->data = atoi(array[i]); head = n; i -= 1; } return head; } // DO NOT CHANGE THIS FUNCTION // print linked list void print_list(struct node *head) { printf("["); struct node *n = head; while (n != NULL) { // If you're getting an error here, // you have returned an invalid list printf("%d", n->data); if (n->next != NULL) { printf(", "); } n = n->next; } printf("]\n"); } // DO NOT EDIT // Gets the length of a linked lists int get_list_length(struct node *head) { int length = 0; struct node *current = head; while (current != NULL) { length++; current = current->next; } return length; }