Your objective for this project is to update a Singly-Linked list class to support inversion and rotation functionality.

computer science

Description

Project 4   Recursion, Inversion, and Rotation: The single life of a Singly-Linked List

Your objective for this project is to update a Singly-Linked list class to support inversion and rotation functionality. In order to successfully complete this project, you mustunderstand the prerequisite material from all previous projects, and you must understand the concept of a Linked List ADT.

Some additional resources

  • Linked List:
    Geeks for Geeks
    CMU
    edspresso

Implementation

Work incrementally! Work through the tasks sequentially (implement and test). Only move on to a task when you are positive that the previous one has been completed correctly. Remember that the names of function prototypes and member variables must exactly match those declared in the respective header file when implementing a class.


Starterp Code:  starter_code.zip

Task 1: All roads lead to Rome and back home

“A singly-linked list can be inverted” was once a song of dreams, a tale from a time before the invention of the doubly-linked list. Then, the budding programmer could iterate from head to tail, but they could not iterate from tail to head - that is, until one thought of a way to take a singly-linked list and invert it in order to make all of the links point in the opposite direction. Your task, should you choose to accept it, is to implement this inversion within the provided LinkedList files. Your implementation must be in O(n) time using O(1) extra space. This can be acheived by splitting your list into two parts, the first and the rest. Here is an example:

Example of Singly-Linked List Inversion

Write a public and non-recursive function invert()that calls a private and recursive invertRest()invertRest() must be implemented recursively and you may not create additional lists, containers, or copies of nodes to aid you in your venture.

/** 
    A wrapper to a recursive method that inverts the contents of the list

    @post the contents of the list are inverted such that:
        the item previously at position 1 is at position item_count_,
        the item previously at position 2 is at position item_count_-1 ...
        the item previously at position ⌊item_count/2⌋ is at position 
            ⌈item_count_/2⌉
*/
void invert();


/**
    private function to invert, used for safe programming to avoid 
    exposing pointers to list in public methods

    @post the contents of the list are inverted such that:
        the item previously at position 1 is at position item_count_,
        the item previously at position 2 is at position item_count_-1 ...
        the item previously at position ⌊item_count/2⌋ is at position
            ⌈item_count_/2⌉
*/
void invertRest(Node<T>* current_first_ptr); 

Task 2: An end is just a new beginning

Your task, should you choose to accept it, is to implement a rotate function that, given any k, will shift all items in the caller LinkedList k positions to the right. Items that move beyond the final position must wrap around to the beginning. Here is an example:

Example of Singly-Linked List Inversion

rotate() is a public and recursive function that should be implemented with O(1) extra space, which means no additional lists, containers, or nodes are needed to implement it. This can be acheived by relinkingthe nodes currently in the list.

/**
    @pre k >= 0
    @post the contents of the list are rotated to the right by k places, 
        so that every element at position i shifts to position i+k % item_count_
*/
void rotate(int k);

Hint: Think of the problem as “perform the smallest rotation (i.e. k = 1) by moving the last node to be the first and then rotate the rest recursively (k-1).”



Related Questions in computer science category