Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are given a linked list. Your task is to create two new linked lists, the rs

ID: 3639697 • Letter: Y

Question

You are given a linked list. Your task is to create two new linked lists, the rst of which should contain the
1st, 3rd, 5th, : : : elements and the second the 2nd, 4th, 6th, : : : elements of the input list. The following
code segment provides a solution. Fill in the blanks to complete the segment. Evaluation of your answer
will depend on overall correctness.
The function createLists assumes that there is a dummy node at the beginning of each list (input as well
as output). The input list is headed by the pointer head. Odd-numbered elements are to be stored in the
list headed by the pointer oddhead, and the even-numbered elements are to be stored in the list headed by
the pointer evenhead. Assume that the input pointer head already points to a properly allocated list with a
dummy node at the beginning. Assume also that both oddhead and evenhead are already allocated memory
only for the dummy nodes. We number the elements of the input list from 1.

Explanation / Answer

/* First define a structure for a node in the list */ typedef struct nodeTag { int data; /* Declare the self-referencing pointer */ next; } node; void createLists ( node *head , node *oddhead , node *evenhead ) { node *src, *dest1, *dest2; int flag = 1; /* Initialize the source and destination pointers to point to the dummy nodes */ src = head; dest1 = oddhead; dest2 = evenhead; /* As long as the source list is not fully traversed */ while ( ) { if (flag == 1) { /* Insert in the odd list */ /* Allocate memory for a new node */ /* Advance the destination pointer by one node */ /* Copy data from source */ } else { /* Insert in the even list */ /* Allocate memory for a new node */ /* Advance the destination pointer by one node */ /* Copy data from source */ } src = src -> next; /* Look at the next node in the input */ if (flag == 1) flag = 2; else flag = 1; /* Switch the destination */ } dest1 -> next = dest2 -> next = NULL; /* Terminate the destination lists

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote