Advertisement
FahimFaisal

LinkedList

Feb 9th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package LinkedList;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12. public class LinkedList{
  13.   public Node head;
  14.  
  15.  
  16.   /* First Constructor:
  17.    * Creates a linked list using the values from the given array. head will refer
  18.    * to the Node that contains the element from a[0]
  19.    */
  20.   public LinkedList(Object [] a){
  21.     // TO DO
  22.  
  23.     head=new Node(a[0],null);
  24.     Node tail=head;
  25.     for(int i=1;i<a.length;i++){
  26.         Node n=new Node(a[i],null);
  27.         tail.next=n;
  28.         tail=n;
  29.   }
  30.   }
  31.  
  32.   /* Second Constructor:
  33.    * Sets the value of head. head will refer
  34.    * to the given LinkedList
  35.    */
  36.   public LinkedList(Node h){
  37.     // TO DO
  38.     head=h;
  39.   }
  40.  
  41.   /* Counts the number of Nodes in the list */
  42.   public int countNode(){
  43.     // TO DO
  44.     int count=0;
  45.     for(Node n=head;n!=null;n=n.next){
  46.         count++;
  47.     }
  48.     return count; // please remove this line!
  49.   }
  50.  
  51.   /* prints the elements in the list */
  52.   public void printList(){
  53.     // TO DO    
  54.     for(Node n=head;n!=null;n=n.next){
  55.         if(n.next==null){
  56.              System.out.print(n.element+".");
  57.         }else{
  58.         System.out.print(n.element+",");
  59.         }
  60.     }
  61.       System.out.println();
  62.   }
  63.  
  64.   // returns the reference of the Node at the given index. For invalid index return null.
  65.   public Node nodeAt(int idx){
  66.     // TO DO
  67.     if(idx<0){
  68.         throw new ArrayIndexOutOfBoundsException();
  69.     }
  70.     int count=0;
  71.     for(Node n=head;n!=null;n=n.next){
  72.         if(count==idx){
  73.             return n;
  74.         }
  75.         count++;
  76.     }
  77.     return null; // please remove this line!
  78.   }
  79.  
  80.  
  81. // returns the element of the Node at the given index. For invalid idx return null.
  82.   public Object get(int idx){
  83.     // TO DO
  84.     if(idx<0){
  85.         throw new ArrayIndexOutOfBoundsException();
  86.     }
  87.      int index=0;
  88.      for(Node n=head;n!=null;n=n.next){
  89.          if(index==idx){
  90.              return n.element;
  91.          }
  92.          index++;
  93.      }
  94.     return null; // please remove this line!
  95.   }
  96.  
  97.   /* updates the element of the Node at the given index.
  98.    * Returns the old element that was replaced. For invalid index return null.
  99.    * parameter: index, new element
  100.    */
  101.   public Object set(int idx, Object elem){
  102.     // TO DO
  103.     int index=0;
  104.     for(Node n=head;n!=null;n=n.next){
  105.         index++;
  106.         if(index==idx){
  107.             Object replaced=n.element;
  108.             n.element=elem;
  109.             return replaced;
  110.         }
  111.     }
  112.     return null; // please remove this line!
  113.   }
  114.  
  115.  
  116.   /* returns the index of the Node containing the given element.
  117.    if the element does not exist in the List, return -1.
  118.    */
  119.   public int indexOf(Object elem){
  120.     // TO DO
  121.     int index=0;
  122.     for(Node n=head;n!=null;n=n.next){
  123.         if(n.element.equals(elem)){
  124.             return index;
  125.         }
  126.         index++;
  127.        
  128.     }
  129.     return -1; // please remove this line!
  130.   }
  131.  
  132.   // returns true if the element exists in the List, return false otherwise.
  133.   public boolean contains(Object elem){
  134.     // TO DO
  135.     int index=0;
  136.     for(Node n=head;n!=null;n=n.next){
  137.         if(n.element.equals(elem)){
  138.             return true;
  139.         }
  140.         index++;
  141.        
  142.     }
  143.     return false; // please remove this line!
  144.   }
  145.  
  146.   // Makes a duplicate copy of the given List. Returns the reference of the duplicate list.
  147.   public Node copyList(){
  148.     // TO DO
  149.     Node newHead=new Node(head.element,null);
  150.     Node newTail=newHead;
  151.     for(Node n=head;n!=null;n=n.next){
  152.         Node mn=new Node(n.element,null);
  153.         newTail.next=mn;
  154.         newTail=mn;
  155.     }
  156.     return newHead; // please remove this line!
  157.   }
  158.  
  159.   // Makes a reversed copy of the given List. Returns the head reference of the reversed list.
  160.   public Node reverseList(){
  161.     // TO DO
  162.     Node rev=null;
  163.     for(Node n=head;n!=null;n=n.next){
  164.         Node mn=new Node(n.element,null);
  165.         mn.next=rev;
  166.         rev=mn;
  167.     }
  168.     return rev; // please remove this line!
  169.   }
  170.  
  171.   /* inserts Node containing the given element at the given index
  172.    * Check validity of index.
  173.    */
  174.   public void insert(Object elem, int idx){
  175.     // TO D
  176.     if(idx<0 ){
  177.         throw new ArrayIndexOutOfBoundsException();
  178.     }
  179.     Node mn=new Node(elem,null);
  180.     int index=0;
  181.         if(idx==0){
  182.             mn.next=head;
  183.             head=mn;
  184.         }
  185.         else{
  186.             Node pred=this.nodeAt(idx-1);
  187.         mn.next=pred.next;
  188.         pred.next=mn;
  189.            
  190.         }
  191.  
  192.    
  193.   }
  194.  
  195.  
  196.   /* removes Node at the given index. returns element of the removed node.
  197.    * Check validity of index. return null if index is invalid.
  198.    */
  199.   public Object remove(int index){
  200.     // TO DO
  201.    
  202.     Object removed=null;
  203.     Node rn=null;
  204.    
  205.     int size=this.countNode();
  206.     if(index<0 || index>size){
  207.         return null;
  208.     }
  209.     if(index==0){
  210.         rn=head;
  211.         head=head.next;
  212.     }else{
  213.     Node pred=nodeAt(index-1);
  214.     rn=pred.next;
  215.     pred.next=rn.next;
  216.     }
  217.    
  218.    removed=rn.element;
  219.    rn.element=null;
  220.    rn.next=null;
  221.     return removed; // please remove this line!
  222.   }
  223.  
  224.   // Rotates the list to the left by 1 position.
  225.   public void rotateLeft(){
  226.     // TO DO
  227.     Node tail=null;
  228.     for(tail=head;tail.next!=null;tail=tail.next){
  229.        
  230.     }
  231.    tail.next=head;
  232.    head=head.next;
  233.    tail.next.next=null;
  234.   }
  235.  
  236.   // Rotates the list to the right by 1 position.
  237.   public void rotateRight(){
  238.     // TO DO
  239.     Node preTail=null;
  240.     for(preTail=head;preTail.next.next!=null;preTail=preTail.next){
  241.        
  242.     }
  243.     Node tail=preTail.next;
  244.     tail.next=head;
  245.     head=tail;
  246.     preTail.next=null;
  247.   }
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement