Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 ) (List Implementation) (35 points): Suppose that you want an operation for th

ID: 3821522 • Letter: 1

Question

1 ) (List Implementation) (35 points): Suppose that you want an operation for the ADT List that returns the first position of a given object in the list. The header of the method is as follows: public int getFirstPosition(T anObject) where T is the general type of the objects in the list. Write an implementation of this method for the LList class. Include testing of the method in the main method of the LList class.

2) (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class.

Explanation / Answer

Hi You have not posted LList class.

So, I can not test my method.

Please find my method implementation and add this method in your LList class and can test in main method.

public int getFirstPosition(T anObject){
  
   // linked list is empty
   if(head == null)
       return -1;

   int position = -1;

   Node temp = head;

   while(temp != null){

       position++;

       if(temp.getData().compareTo(anObject) == 0)
           return position;
   }

   return -1; // did not find in list
}

2)

Same here also, you did not post DoublyLinkedList class.

public boolean removeValue(T aValue){

   // linked list is empty
   if(head == null)
       return false;

   // if head value is equal to aValue
   if(head.getData().compareTo(aValue) == 0){

       // if list has only one node
       if(head.getNext() == null){
           head = null;
       }{
           head = head.getNext();
           head.setPrevious(null);
       }

       return true;
   }

   Node temp = head;

   while(temp.getNext() != null){

       if(temp.getNext().getData().compareTo(aValue) == 0){

           temp.setNext(temp.getNext().getNext());

           if(temp.getNext() != null){
               temp.getNext().setPrevious(temp);
           }

           return true;
       }

       temp = temp.getNext();
   }

   return false; // did not find


}