Advertisement
Guest User

LinkedList

a guest
Oct 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. //Class Node
  2. public class Node {
  3.     public int data;
  4.     public Node next;
  5.    
  6.     public Node(int d){
  7.         data=d;
  8.         next=null;
  9.     }
  10.    
  11. }
  12.  
  13.  
  14. //Class LinkedList
  15.  
  16. public class LinkedList {
  17.    
  18.     Node head;
  19.    
  20.    
  21.     public void push(int new_data){
  22.         if(head==null){
  23.             head=new Node(new_data);
  24.             return;
  25.         }
  26.        
  27.         Node newNode=new Node(new_data);
  28.         newNode.next=head;
  29.         head=newNode;
  30.     }
  31.    
  32.     public int getCount(){
  33.         if(head==null) return 0;
  34.        
  35.         Node temp=head;
  36.        
  37.         int count=0;
  38.        
  39.         while(temp!=null){
  40.        
  41.             //System.out.println(temp.data);
  42.             count++;
  43.             temp=temp.next;
  44.            
  45.         }
  46.        
  47.         return count;    
  48.     }
  49.    
  50.     //Bonus task
  51.     public Node getMiddleNode() throws Exception{
  52.        
  53.         //Throw exception if the list is empty
  54.         if(head==null) throw new Exception("The list is empty.");
  55.        
  56.         //If the list contains just one element (head), return that element
  57.         if(head.next==null) return head;
  58.        
  59.         /*Initalize 2 'support' nodes that represent the pointers to elements of the list
  60.         and which start at the first element of the list*/
  61.         Node temp1=head;
  62.         Node temp2=head;
  63.        
  64.         /*Move the temp1 node for 1 and temp2 for 2 places until temp2 is null
  65.         or temp2 is the last element of the list. When temp2 comes to the end of the list, the
  66.         temp1 node will be in the middle.
  67.         */
  68.         while(temp2!=null){
  69.             if(temp2.next==null) return temp1;
  70.            
  71.             temp1=temp1.next;
  72.             temp2=temp2.next.next;
  73.        
  74.         }
  75.        
  76.         return temp1;
  77.    
  78.     }
  79.    
  80.     public static void main(String[] args){
  81.    
  82.         LinkedList llist=new LinkedList();
  83.         llist.push(1);
  84.         llist.push(3);
  85.         llist.push(1);
  86.         llist.push(2);
  87.         llist.push(1);
  88.        
  89.        
  90.         System.out.println("Count of nodes is: "+ llist.getCount());
  91.        
  92.         //Bonus task
  93.         try {
  94.             Node node=llist.getMiddleNode();
  95.             System.out.println("Middle node is: " +node.data);
  96.         } catch (Exception ex) {
  97.             System.out.println(ex.getMessage());
  98.         }
  99.    
  100.     }
  101.    
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement