2025 CSCI2100E Lab Session #2

Doubly Linked List Rotation 2025 (Optional)

Time limit: 5000ms
Memory limit: 256mb

Description:

This problem is built on top of Linked List Rotation 2024 (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 minimum number of rotations required and the direction d so that the sum of the first half elements is greater or equal to the sum of the second half, if the number of elements in the linked list is odd, the central elements belong to neither half. 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). If it can be done using n operations either clockwise or anti-clockwise, always answer with the 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 so that the sum of the first half elements is greater or equal to the sum of the second half, if the number of elements in the linked list is odd, the central elements belong to neither half.

Sample Input 1:
4
3 9 10 4

Sample Output 1:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
4 <-> 10 <-> 9 <-> 3
n, d:
0, 1

Sample Input 2:
7
11 13 9 7 5 3 1

Sample Output 2:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
1 <-> 3 <-> 5 <-> 7 <-> 9 <-> 13 <-> 11
Rotated linked lists:
3 <-> 5 <-> 7 <-> 9 <-> 13 <-> 11 <-> 1
5 <-> 7 <-> 9 <-> 13 <-> 11 <-> 1 <-> 3
n, d:
2, 1

Sample Input 3:
7
25 13 9 7 5 3 2

Sample Output 3:
Input m:
Input the numbers to be prepended sequentially:
Original linked list:
2 <-> 3 <-> 5 <-> 7 <-> 9 <-> 13 <-> 25
Rotated linked lists:
25 <-> 2 <-> 3 <-> 5 <-> 7 <-> 9 <-> 13
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