m2skills

rotate ll java

May 8th, 2018
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. // program to rotate the linked list anti-clockwise
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  * Created by MOHIT on 24-01-2018.
  6.  */
  7.  
  8. class node{
  9.  
  10.     public int element;
  11.     public node link;
  12.  
  13.     //constructor that accepts only element
  14.     node(int element) {
  15.         this.element = element;
  16.         this.link = null;
  17.     }
  18.  
  19.     //constructor that accepts both link and element
  20.     node(int element, node link){
  21.         this.element = element;
  22.         this.link = link;
  23.     }
  24.  
  25.     //method to update the element
  26.     void updateData(int element){
  27.         this.element = element;
  28.     }
  29.  
  30.     //method to update or setup link
  31.     void updateLink(node link){
  32.         this.link = link;
  33.     }
  34.  
  35.     //method to get the element from the node
  36.     int getElement(){
  37.         return this.element;
  38.     }
  39.  
  40.     //method to get the next node
  41.     node getNextNode(){
  42.         return this.link;
  43.     }
  44. }
  45.  
  46. class LL {
  47.  
  48.     Scanner sc = new Scanner(System.in);
  49.     node head;
  50.  
  51.     void insertAtStart(int number){
  52.  
  53.         node newNode = new node(number);
  54.  
  55.         if(head == null){
  56.             head = newNode;
  57.         } else {
  58.             newNode.updateLink(head);
  59.         }
  60.         head = newNode;
  61.  
  62.         return;
  63.     }
  64.  
  65.     void insertAtEnd(int element){
  66.         if(head == null){
  67.             this.insertAtStart(element);
  68.             return;
  69.         }
  70.         node current = this.head;
  71.         while(current.getNextNode() != null){
  72.             current = current.getNextNode();
  73.         }
  74.  
  75.         node newNode = new node(element);
  76.         current.updateLink(newNode);
  77.         return;
  78.     }
  79.  
  80.     void display(){
  81.         node current = this.head;
  82.  
  83.         while(current!= null){
  84.             System.out.print(current.getElement());
  85.             if(current.getNextNode() != null){
  86.                 System.out.print("-->");
  87.             }
  88.             current = current.getNextNode();
  89.         }
  90.     }
  91.  
  92.     // rotate a linked list
  93.     // function removes nodes from the beginning and puts them to the end of the linked list;
  94.     void rotate(int shiftValue){
  95.         node current = this.head;
  96.         node tail = this.head;
  97.  
  98.         // make tail point the last element
  99.         while(tail.getNextNode() != null){
  100.             tail = tail.getNextNode();
  101.         }
  102.  
  103.         for(int i = 0; i < shiftValue; i++){
  104.             tail.updateLink(current);
  105.             current = current.getNextNode();
  106.             tail = tail.getNextNode();
  107.             tail.updateLink(null);
  108.         }
  109.  
  110.         // let head point to the new head node
  111.         this.head = current;
  112.     }
  113. }
  114. public class LinkedList {
  115.     public static void main(String arg[]) {
  116.         System.out.println("Program to rotate Linked List.");
  117.  
  118.         LL l1 = new LL();
  119.         l1.insertAtEnd(1);
  120.         l1.insertAtEnd(2);
  121.         l1.insertAtEnd(3);
  122.         l1.insertAtEnd(4);
  123.         l1.insertAtEnd(5);
  124.         l1.insertAtEnd(6);
  125.         l1.insertAtEnd(7);
  126.         l1.insertAtEnd(8);
  127.         l1.display();
  128.         System.out.println("\nRotating linked list by 4 places");
  129.         l1.rotate(4);
  130.         l1.display();
  131.     }
  132. }
  133.  
  134. /*
  135.  
  136. Program to rotate Linked List.
  137.  
  138. 1-->2-->3-->4-->5-->6-->7-->8
  139. Rotating linked list by 4 places
  140. 5-->6-->7-->8-->1-->2-->3-->4
  141.  
  142.  */
Add Comment
Please, Sign In to add comment