m2skills

middleLL java

Mar 29th, 2017
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. // Program to find the middle node in the Linked List in a single traversal
  2. import java.io.*;
  3. import java.util.Scanner;
  4. /**
  5.  * Created by MOHIT on 25-07-2016.
  6.  */
  7. class ListElement{
  8.  
  9.     public int data;
  10.     public ListElement link;
  11.     public ListElement list1,list2;
  12.  
  13.     Scanner sc = new Scanner(System.in);
  14.     //construtor
  15.     ListElement(int data){
  16.         this.data = data;
  17.         this.link = null;
  18.     }
  19.  
  20.     //method to update data in List element
  21.     void updatedata(int data){
  22.         this.data = data;
  23.     }
  24.  
  25.     //method to setup the Link
  26.     void setLink(ListElement node){
  27.         this.link = node;
  28.     }
  29.  
  30.     int getdata(){
  31.         return this.data;
  32.     }
  33.  
  34.     ListElement getNextNode(){
  35.         return this.link;
  36.     }
  37.  
  38.     //method that returns length of list
  39.         int length(){
  40.             ListElement node = this;
  41.             int length_of_list = 1;
  42.  
  43.             if(node == null){
  44.                 return 0;
  45.             }
  46.  
  47.             while(node.link != null){
  48.                 length_of_list++;
  49.                 node = node.getNextNode();
  50.             }
  51.  
  52.             return length_of_list;
  53.         }
  54.  
  55.     ListElement insert(ListElement head,ListElement node,int position){
  56.  
  57.         ListElement previousNode = new ListElement(head.data);
  58.         previousNode.setLink(head.link);
  59.  
  60.         if(head == null){
  61.             return node;
  62.         }
  63.         else{
  64.             int size = length();
  65.             if(position > size+1){
  66.                 System.out.println("Invalid position.");
  67.                 System.out.println("Valid positions are from 0 to " + size);
  68.                 return head;
  69.             }
  70.             if(position == 1){
  71.                 node.setLink(head);
  72.                 return node;
  73.             }
  74.             else if(position == 2){
  75.                 ListElement temp = new ListElement(-11);
  76.                 temp.setLink(head.link);
  77.                 head.setLink(node);
  78.                 node.setLink(temp.getNextNode());
  79.             }
  80.             else{
  81.                 int pos = 1;
  82.                 while(pos != position-1){
  83.                     previousNode = previousNode.getNextNode();
  84.                     pos++;
  85.                 }
  86.                 //ListElement temp = head.getNextNode();
  87.  
  88.                 previousNode.setLink(node);
  89.                 node.setLink(null);
  90.             }
  91.         }
  92.         return head;
  93.     }
  94.  
  95.     ListElement insertAtEnd(ListElement node){
  96.         ListElement head = this;
  97.         int size = head.length();
  98.  
  99.         ListElement temp = this;
  100.         while(temp.link!=null){
  101.             temp = temp.getNextNode();
  102.         }
  103.         temp.setLink(node);
  104.         return head;
  105.     }
  106.  
  107.  
  108.     void display(ListElement head){
  109.         ListElement node = head;
  110.         int position = 1;
  111.         while(node!= null){
  112.             System.out.println("Position " + position + " = " + node.data);
  113.             position++;
  114.             node = node.getNextNode();
  115.         }
  116.     }
  117.  
  118.     int middleNode(){
  119.         ListElement Node = this;
  120.         ListElement fast = this;
  121.         ListElement slow = this;
  122.        
  123.         while (true){
  124.             if(fast.getNextNode() != null){
  125.                 fast = fast.getNextNode();
  126.                 slow = slow.getNextNode();
  127.             }else{
  128.                 break;
  129.             }  
  130.             if(fast.getNextNode() != null){
  131.                 fast = fast.getNextNode();
  132.             }else{
  133.                 break;
  134.             }  
  135.         }
  136.         return slow.getdata();
  137.     }
  138.    
  139.     ListElement makeDummyList() {
  140.         ListElement head = this;
  141.         for(int i=2 ; i < 10 ; i++){
  142.             ListElement node = new ListElement(i);
  143.             head.insert(head,node,i);
  144.         }
  145.         return head;
  146.     }
  147.  
  148. }
  149. public class middleLL {
  150.     public static void main(String args[]){
  151.         Scanner sc = new Scanner(System.in);
  152.         ListElement head = new ListElement(1);
  153.         head.setLink(null);
  154.        
  155.         head = head.makeDummyList();
  156.         System.out.println("The Elements of the Linked List");
  157.         head.display(head);
  158.        
  159.         System.out.println("The Middle Element of the linked list is : " + head.middleNode());
  160.        
  161.     }
  162. }
Add Comment
Please, Sign In to add comment