Question: struct node { int data; struct node * next; }; typedef struct node * link; 1. (15 points) list sublist(list A, list pos_list). This function
struct node { int data; struct node * next; };
typedef struct node * link;
1. (15 points) list sublist(list A, list pos_list). This function returns a list that contains the values that appear in list A at positions given in pos_list Requirement 1: the values should appear in the result in the order given by pos_list. For example if A: 15 -> 100 -> 7 -> 5 -> 100 -> 7 -> 30 and pos_list : 3 -> 0 -> 6 -> 4 The resulting list will be A[3] -> A[0] -> A[6] -> A[4] , which gives values: 5 -> 15 -> 30 -> 100. Requirement 2: the result should be a deep copy, so that any future changes in list A cannot possibly affect the result, and any future changes in the result cannot possibly affect list A. (List A should remain as it was after building the sublist.) Requirement 3: DO not copy the nodes in an array and then build a list from an array. When found the node in A, make a new node with the same value and insert it in the result list. Requirement 4: DO not make a copy of a list and change the data there. Create new nodes and insert them at the end. The list pos_list CAN have repetitions. E.g.: list A: 10 -> 5 pos_list: 0->0->1->-0->1->1->0. produces list: 10->10->5->10->5->5->10 implicitly the size of A is unrelated to the size of pos_list.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
