Home » Rotate Operation in Linked List

Rotate Operation in Linked List

by Online Tutorials Library

Rotate Operation in Linked List

There are a lot of operations that can be performed on a Linked List, like insertion operation where we can add a new node to the already existing linked list, deletion operation where we can delete a node from the linked list, and displaying the data present in all the nodes of the Linked List.

In a singly linked list, the node of the singly linked list points to the address of the memory location where the next node is stored. Similarly, in this way, all the nodes are storing the address of their respective next nodes. Other than the last node, the address in the pointer of the last node is NULL representing it is the last node of the linked list.

There is one more operation that is supported by a Linked List which is the rotate operation. In this operation, the head node of the linked list is moved to the last element of the Linked List, and the first element of the Linked List becomes the last element that is present in the Linked List.

C Code

Now let’s see the code of rotating or reversing a linked list in different programming languages.

Output:

The above code gives the following output:

Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  15  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  13  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  11  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    10  Please enter a valid option from the menu to proceed further.       Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  5  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  3  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  2  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  1  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    3  Data in the Linked List is:  1 2 3 5 11 13 15   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    2  Linked List Rotated Successfully.  Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    3  Data in the Linked List is:  15 13 11 5 3 2 1   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  N  

C++ Code

Now let’s see the C++ code of rotating or reversing a linked list.

Output:

This C++ code gives the following output,

Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  190  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  70  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  55  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  40  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  30  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  20  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  10  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    3  Data in the Linked List is:  10 20 30 40 55 70 190   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    2  Linked List Rotated Successfully.  Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    3  Data in the Linked List is:  190 70 55 40 30 20 10   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  N  

Java Code

Now let’s see the Java code of rotating or reversing a linked list.

Output:

Above java code gives the following output,

Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  1  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  2  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  3  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  4  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  5  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    1  Enter the data that you want to add to the newly added node to the Linked List:  6  Data Added Successfully.    Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.        3  Data in the Linked List is:  1 2 3 4 5 6   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    2  Linked List Rotated Successfully.  Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  y  Please Choose one of the Operations::  1. To Insert Data in the Linked List.  2. To Rotate the Linked List.  3. To Display Data present in the Linked List.    3  Data in the Linked List is:  6 5 4 3 2 1   Type [N or n] to terminate the program.  Type [Y or y] to continue the program.  N  

So, in this article, we got a clear idea about a rotate operation in a linked list. We also saw how to write a code of rotate operation in the linked list in different programming languages like C, C++, and Java.


You may also like