Question
C Language Problem Code structure is given below you just need to complete that. Please try to do it within 2 hours. Problem Statement Given
C Language Problem
Code structure is given below you just need to complete that.
Please try to do it within 2 hours.
Problem Statement
Given a doubly-linked list (say of n nodes where n > 0) and a positive integer k, rearrange the links between the nodes of the linked list in the following way. The nodes of the linked list are divided into segments of k nodes. If k does not divide n, then the final segment is of n % k nodes. The order of nodes in alternate segments (1st, 3rd, 5th, ...) are to be reversed. An example is described below.
Initial linked list: 1 2 3 4 5 6 7 8 9 0
Final linked list after rearrangement with k = 3: 3 2 1 4 5 6 9 8 7 0
Note:
- The nodes in the linked list are of the following structure.
struct node {
int data;
struct node* next;
struct node* prev;
};
- No marks will be given if doubly-linked list is not used.
- Arrays are prohibited. No marks will be given if arrays are used.
- Make sure that you rearrange the links in the list. A penalty will be imposed if the data field of any node is changed at any point after taking input.
- Do not modify the print() function given in the template. Use this function to print the output by providing as an argument, the pointer to the head node of the final doubly-linked list. Do not use any other printf() statement in the program.
- Make sure that the prev field of the head and the next field of the tail take NULL values.
- It is guaranteed that the linked list given is not empty. Therefore, the head passed to print() should not be NULL.
Input Format:
The input consists of two lines. The first line is a stream of integers that ends with -1 (not to be included), representing the linked list. It is guaranteed that the linked list contains at least one node. The second line is a single positive integer representing k.
Output Format:
The output consists of two lines. The first line is a stream of integers separated by spaces representing the final linked list after transformation. The second line is also a stream of integers that represents the same final linked list but in reverse order. You will only need to call the print() function defined in the template with the pointer to the head of the final doubly-linked list as an argument. This function prints both lines in the required format.
Example
Input:
1 2 3 4 5 6 7 8 9 0 -1
3
Output:
3 2 1 4 5 6 9 8 7 0
0 7 8 9 6 5 4 1 2 3
Explanation: Segment 1: 1 2 3 3 2 1 Segment 2: 4 5 6 4 5 6 Segment 3: 7 8 9 9 8 7 Segment 4: 0 0
- No marks to be given if arrays are used. - 50% penalty if the `data` field of any node is changed at any point after being input. Public Test Cases (1 * 3 = 3 marks) Hidden Test Cases (6 * 7 = 42 marks)
//complete the code.
#include #include
struct node { int data; struct node* next; struct node* prev; };
void print(struct node* head) { if(head == NULL) { printf("ERROR - Head is never NULL"); return; } while(head->next != NULL) { printf("%d ", head->data); head = head->next; } printf("%d ", head->data); while(head->prev != NULL) { printf("%d ", head->data); head = head->prev; } printf("%d", head->data); return; }
int main() { return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started