Advertisement
FahimFaisal

DoublyList_RAK_new

Feb 16th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 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 dummyHead_RAK;
  7.  
  8. public class DoublyList {
  9.  
  10.   public DNode head;
  11.  
  12.   /* Build a Dummy Headed Circular List from the given circular array
  13.    */
  14.   public void buildFromArray(int[]cir,int st,int size){
  15.     // TO DO
  16.     head=new DNode(null,null,null);
  17.    
  18.     DNode tail=head;
  19.      int k=st;
  20.     for(int i=0;i<size;i++){
  21.        
  22.         DNode n=new DNode(cir[k],null,null);
  23.         k=(k+1)%cir.length;
  24.         tail.next=n;
  25.         n.prev=tail;
  26.         tail=n;
  27.     }
  28.      tail.next=head;
  29.      head.prev=tail;
  30.  
  31.    
  32.   }
  33.   /* prints the elements in the list */
  34.   public void forwardprint(){
  35.     // TO DO    
  36.     for(DNode n=head.next;n!=head ;n=n.next){
  37.     System.out.print(n.element+" ");
  38.     }
  39.       System.out.println();
  40.   }
  41.  
  42.   public void backwardprint(){
  43.     // TO DO
  44.    for(DNode n=head.prev; n!=head ;n=n.prev){
  45.     System.out.print(n.element+" ");
  46.     }  System.out.println();
  47.   }
  48.  
  49.   /* Build a Dummy Headed Circular List from the given Non Dummy Headed Non Circular List
  50.    */
  51.   public void buildFromList(Node h){
  52.     // TO DO
  53.     head=new DNode(null,null,null);
  54.     DNode tail=head;
  55.     for(Node n=h;n!=null;n=n.next){
  56.     DNode mn=new DNode(n.element,null,null);
  57.     tail.next=mn;
  58.     mn.prev=tail;
  59.     tail=mn;
  60.     }
  61.     tail.next=head;
  62.     head.prev=tail;
  63.    
  64.    
  65.   }
  66.  
  67.   /* Build a Dummy Headed Circular List from the given Non Dummy Headed Non Circular List
  68.    * The elements of the new Dummy Headed list must in reverse order
  69.    */
  70.   public void buildReverse(Node h){
  71.     // TO DO
  72.     DNode rev=new DNode(h.element,null,null);
  73.      DNode tail=rev;
  74.      for(Node n=h.next;n!=null;n=n.next){
  75.          DNode mn=new DNode(n.element,null,null);
  76.          mn.next=rev;
  77.          rev.prev=mn;
  78.          rev=mn;
  79.      }
  80.      DNode newHead=new DNode(null,null,null);
  81.      newHead.next=rev;
  82.      rev.prev=newHead;
  83.      rev=newHead;
  84.      tail.next=rev;
  85.      rev.prev=tail;
  86.      head=rev;
  87.   }
  88.   /* Insert the element in the given index.
  89.    * Index 0 is the index after the dummy head
  90.    */
  91.   public void addElement(int element, int index){
  92.     // TO DO
  93.     DNode succ=null;
  94.     DNode pred=null;
  95.     if(index>=0 && index<countNode()){
  96.         if(index==countNode()){
  97.             succ=head;
  98.         }
  99.         else{
  100.             succ=nodeAt(index);
  101.            
  102.         }
  103.         DNode mn=new DNode(element,null,null);
  104.         pred=succ.prev;
  105.         mn.next=succ;
  106.         mn.prev=pred;
  107.         pred.next=mn;
  108.         succ.prev=mn;
  109.     }
  110.   }
  111.  
  112.   /* Delete the element from the given index.
  113.    * Index 0 is the index after the dummy head
  114.    */
  115.   public void deleteElement(int index){
  116.     // TO DO
  117.     DNode succ=null;
  118.     DNode pred=null;
  119.     Object elem=null;
  120.     if(index>=0 && index<countNode()){
  121.     DNode n=nodeAt(index);
  122.     succ=n.next;
  123.     pred=n.prev;
  124.     pred.next=succ;
  125.     succ.prev=pred;
  126.     elem=n.element;
  127.     n.next=null;
  128.     n.prev=null;
  129.      System.out.println(elem+" removed");
  130.     }
  131.   }
  132.  
  133.   public int countNode(){
  134.       int count=0;
  135.       for(DNode n=head.next;n!=head;n=n.next){
  136.          count++;
  137.       }
  138.       return count;
  139.   }
  140.  
  141.   public DNode nodeAt(int idx){
  142.       int count=0;
  143.       for(DNode n=head.next;n!=null;n=n.next){
  144.           if(idx==count){
  145.               return n;
  146.           }
  147.           count++;
  148.       }
  149.       return null;
  150.   }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement