Question

public class myLinkedList<E extends Comparable<E>>{       private Node<E> head = null;                    private Node<E>...

public class myLinkedList<E extends Comparable<E>>{
      private Node<E> head = null;             
      private Node<E> tail = null;             
  
      public myLinkedList() {
       head = null;
      }

public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList
      }
  
      public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element  
      
      }
  
      public void removeElement(E targetElement){//remove all target element that can be found on the list

      }
  
      public E middleElement(){//find and return the element that stored at the middle node of a linkedList

      }

=====================================================================================

Node class

public class Node<E extends Comparable<E>> {

   private E item;            // reference to the element stored at this node
    private Node<E> next;         // reference to the subsequent node in the list
    /**
     * Creates a node with the given element and next node.
     *
     * @param e the element to be stored
     * @param n reference to a node that should follow the new node
     */
    public Node(E e, Node<E> n) {
       item = e;
      next = n;
    }
  
    public Node(E e) {
       item = e;
      next = null;
    }
    // Accessor methods
    public E getItem() { return item; }
    public Node<E> getNext() { return next; }
    // Modifier methods
    public void setNext(Node<E> n) { next = n; }
} //----------- end of nested Node class -----------

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//attach another linkedList refered by M to the end of this linkedList

public void concatenateList (myLinkedList<E> M) {
   //To concatenate two linked list we have to make the last node of first list to point to first node of second list
   if (head == null) {
       head = tail;
       tail.setNext(head);
   } else {
       Node temp = head;
       while (temp.next != null)
           temp = temp.next;

       temp.next = tail;
       tail.setNext(temp.next);
   }
  
}

//test if a target element can be found on the list, return the occurrences of the target element
public int searchElement(E targetElement){

   int index=list.indexOf(targetElement);   
   return index;
  
}

//remove all target element that can be found on the list
public void removeElement(E targetElement){
  
   int index=list.indexOf(targetElement);
  
   if(index==0)
{
head=head.next;
}
else
{
Node n=head;
Node n1=null;
for(int i=0;i<index-1;i++)
{
n=n.next;
}
n1=n.next;
n.next=n1.next;

}

}

//find and return the element that stored at the middle node of a linkedList
public E middleElement(){
   //take two nodes pointing to the first node
   //Increment first with two nodes and second with one, at a time.
   //Loop until the p1 loop reaches the end. At this point, the p2 will be at the middle.
  
  
   Node p1=head;
   Node p2=head;
     
   while ( p2.next != null ) {
   p2 = p2.next;
   if (p2.next != null) {
   p2 = p2.next;
   p1 = p1.next;
   }
   }
   return (E) p1;
  
}

Add a comment
Know the answer?
Add Answer to:
public class myLinkedList<E extends Comparable<E>>{       private Node<E> head = null;                    private Node<E>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT