Doubly Linked List Rotation 2023 (Optional)
Time limit: 5000ms
Memory limit: 256mb
Description:
This problem is built on top of Linked List Rotation 2023 (Required). In this exercise, we practise rotation on a doubly linked list. For example: rotating a linked list 1 <-> 2 <-> 3 <-> 4 'clockwise' by one time turns it to 2 <-> 3 <-> 4 <-> 1, and 'anti-clockwise' by one time to 4 <-> 1 <-> 2 <-> 3.
The user will provide m positive integers, each to be prepended to the linked list sequentially. Your task is find out the value n >= 0, the minumum number of rotations required and the direction d so that the elements of the list are in ascending order. If there does not exist such an n, output '-1, 0'. If no rotation is needed, output '0, 0'. Otherwise, show the rotated linked list step-by-step and output n and d (1 for clockwise, -1 for anti-clockwise).
You will complete the five functions below.
void prepend(List head, int value);
- This function prepends value to the list pointed by head.
void print(List head);
- Print all integers of linked list p from head to tail in one line.
- Every two integers are separated by " -> ". Print a '\n' in the end.
- If the list contains no integers, output a blank line.
int getN(List* l, d);
- Given the direction of rotation d, return the number of rotations needed for the linked list l.
int getD(List* l);
- Return the number of rotations needed given the linked list l.
void rotateNTimesInD(List head, int n, int d);
- This function rotates the list pointed by head for n times in direction d so that the resulting list is sorted in ascending order.
Sample Input 1:
4
3 9 10 4
Sample Output 1:
Input the number of integers to be inserted to the linked list:
Prepend the following numbers to the list sequentially:
Original linked list:
4 <-> 10 <-> 9 <-> 3
n, d:
-1, 0
Sample Input 2:
3
2 1 1
Sample Output 2:
Input the number of integers to be inserted to the linked list:
Input the orginal linked list:
Original linked list:
1 <-> 1 <-> 2
Rotated linked lists:
1 <-> 1 <-> 2
n, d:
0, 0
Sample Input 3:
5
1 5 4 3 2
Sample Output 3:
Input the number of integers to be inserted to the linked list:
Input the orginal linked list:
Original linked list:
2 <-> 3 <-> 4 <-> 5 <-> 1
Rotated linked lists:
2 <-> 3 <-> 4 <-> 5 <-> 1
1 <-> 2 <-> 3 <-> 4 <-> 5
n, d:
1, -1
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct linked_list_node{
int value;
struct linked_list_node* next;
struct linked_list_node* prev;
} Node;
typedef struct linked_list{
Node* head;
Node* tail;
} List;
void initList(List* l){
l->head = NULL;
l->tail = NULL;
}
void prepend(List* l, int value){
// code here
}
void print(List* l){
// code here
}
int getD(List* l){
// code here
}
int getN(List* l, d){
// code here
}
void rotateNTimesInD(List* l, int n, int d){
int i;
if (n >= 0){
printf("Rotated linked lists:\n");
print(l);
for (i=0; i<n; i++){
// code here
print(l);
}
}
}
int main(){
List* l = (List*) malloc(sizeof(List));
initList(l);
int i, m;
int value;
printf("Input m:\n");
scanf("%d", &m);
printf("Input the numbers to be prepended sequentially:\n");
for(i=0; i<m; i++){
scanf("%d", &value);
prepend(l, value);
}
printf("Original linked list:\n");
print(l);
int d = getD(l);
int n = getN(l, d);
rotateNTimesInD(l, n, d);
printf("n, d:\n%d, %d\n", n, d);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit