/* INPUT: LINKED LIST OF CHARS AND TWO POINTERS p and q: b--------->k--------->r--------->m--------->c--------->j--------->d ^ ^ | | p q OUTPUT: SAME LIST BUT NODES AFTER p AND q HAVE BEEN SWAPPED: b--------->k--------->j--------->m--------->c--------->r--------->d Perform code walkthrough (desk check) by examining each line of code and its affect on the list. For each of the 6 swap functions, indicate CORRECT/INCORRECT. If INCORRECT, then indicate the resultant linked list. You should draw it in the fashion above on your own paper by hand. The final answer is what a PRINTOUT of the list would yield. The first list above: b k r m c j d; the second list: b k j m c r d. You just need to turn-in this type of sequence of letters representing the list. As a reminder about C pointers, in the above: p points to the node containing k p->element equals k p->next points to the node containing r p->next->element equals r p->next->next->element equals m p = q makes p point to the same node as q but DOES NOT change the list p = something is the ONLY way to change the value of p to memory address something but it has no affect on the actual nodes within the list. p->next = q DOES change the list; the c node follows the k node EXAMPLE 1: b--------->k--------->r--------->m--------->c--------->j--------->d ^ ^ | | p q Execute: line 1: p->next = q->next This means that the k node has a next pointer that points one door down from c, hence j: line 1 caused this +--------------------------------------+ | | | v b--------->k----+ r--------->m--------->c--------->j--------->d ^ ^ | | p q Note that the k node now has a next pointer that points to the j node. Start from b, the printout would be: b k j d The rest of the list (r, m, c) is just out in memory by itself, unreachable. EXAMPLE 2: Execute: 1: p->next->next = q // node r points to c node 2: q->next = q->next->next // node c points to d node 1 2 +----------------+ +----------------+ | | | | | v | v b--------->k--------->r----+ m--------->c----+ j--------->d ^ ^ | | p q Start from b, the printout would be: b k r c d Nodes m and j are unreachable. See swap.pdf for a detailed example. */ #include typedef struct node *node_ptr; struct node { char element; node_ptr next; }; void swap1(node_ptr p, node_ptr q) { 1. node_ptr temp = q->next; 2. temp->next = p->next->next; 3. q->next = p->next; 4. p->next = temp; } /* HINTS: Assign numbers to the lines of the source code. Draw the input list, as given above. On entry, p points to k node, q points to c node. 1. temp points to node after q, hence j 2. j's next points backwards to the node two doors down from p, hence m 3. since q points to c, c's next points backwards to one door down from p, hence r 4. since p points to k, k's next points to temp, which is pointing to j As you draw these new arrows that go all over the place, number them 1, 2, 3, 4. In all cases, if a next pointer in the list gets reassigned, it lets go of the original node it was pointing to. Now, read-off the list, start from b: b k j (and I'll let you finish the rest) */ void swap2(node_ptr p, node_ptr q) { 1. node_ptr temp = p->next; 2. p->next = q->next; 3. q->next = temp; } /* HINTS: 1. temp points to node one door down from p, hence r 2. k's next points to one door down from q, hence j 3. c's next points to the same thing a temp, hence r Read-off the list: b k j (and I'll let you finish the test) */ void swap3(node_ptr p, node_ptr q) { 1. node_ptr temp1 = q->next; 2. node_ptr temp2 = p->next; 3. node_ptr temp3 = temp1->next; 4. p->next = q->next; 5. temp1->next = temp2; 6. q->next = temp2; 7. temp2->next = temp3; } void swap4(node_ptr p, node_ptr q) { 1. node_ptr temp1 = p->next; 2. node_ptr temp2 = q->next; 3. node_ptr temp3 = temp2; 4. temp2 = temp1; 5. temp1 = temp3; } /* HINT: All of the above are of the form: temp = And what do the notes above say about this form. */ void swap5(node_ptr p, node_ptr q) { 1. node_ptr temp1 = p->next; 2. node_ptr temp2 = q->next; 3. temp1->next = temp2->next; 4. temp2->next = p->next->next; 5. p->next = temp2; 6. q->next = temp1; } /* HINT: The above is tricky. Some students think that it is CORRECT, but it isn't. What does the printout yield? */ void swap6(node_ptr p, node_ptr q) { 1. node_ptr temp1 = p->next; 2. node_ptr temp2 = q->next->next; 3. p->next = q->next; 4. q->next = temp1; 5. p->next->next = temp1->next; 6. temp1->next = temp2; }