I am failing a few tests with my Double Linked List program

I am failing a few tests with my Double Linked List program. Can someone show me the following methods for doubly linked lists? (In Java)

public void addAfter(Object element, Object target)

public int indexOf(Object element)

public void add(int index, Object element)

public boolean contains(Object target)

Thanks!

Solution


public class DoubleLinkedList {

   class Node{
       Node next;
       Node prev;
       Object val;
       Node(){
           next = null;
           prev = null;
           val = null;
       }
      
   }
  
   Node start = null;
  
  
  
   public DoubleLinkedList() {
       super();
       this.start = null;
   }

   public void addAfter(Object element, Object target){
      
       Node t = this.start;
      
       while(!t.val.equals(target) && t != null){
           t = t.next;
       }
      
       if(t == null) System.out.println(\"Target not found\");
       else{
           Node temp = t;
           Node n = new Node();
          
           n.val = element;
           n.prev = temp;
           n.next = temp.next;
          
           temp.next.prev = n;
           temp.next = n;
       }
   }

   public int indexOf(Object element){
       int i = 0;
      
       Node t = start;
      
       while(t!=null){
           i++;
           if(!t.val.equals(element)) t = t.next;
           else break;
       }
       if(t!=null) return i;
       else return -1;
      
      
       //return i;
   }
  
   public void add(int index, Object element){
       //assume index starts from 1
       Node t = start;
       int i = 0;
       if(index == 1)
       {
           Node n = new Node();
           n.prev = null;
           n.val = element;
           n.next = start;
           start.prev = n;
           start = n;
       } else {
       while(i<index-2 && t!=null){
           t = t.next;
          
       }
      
       addAfter(t.val,element);
       }
   }
  
   public boolean contains(Object target){
       boolean flag = false;
       Node t = start;
       while(t!=null){
           if(t.val.equals(target)){
               flag = true;
               break;
           }
       }
       return flag;
   }
}

I am failing a few tests with my Double Linked List program. Can someone show me the following methods for doubly linked lists? (In Java) public void addAfter(O
I am failing a few tests with my Double Linked List program. Can someone show me the following methods for doubly linked lists? (In Java) public void addAfter(O

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site