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

IN JAVA IN JAVA Create a generic class called GenLinkedList. GenLinkedList will

ID: 3888954 • Letter: I

Question

 IN JAVA  
 IN JAVA  Create a generic class called GenLinkedList.  GenLinkedList will use nodes    that store a value of the generic type to store its contents.     It should have the following methods.  The methods should     all operate on the object making the call (none are static).       Perform checking of the parameters and throw exceptions where     appropriate.     The linked list should be singly-linked.     It should not use sentinel nodes (empty header and tail nodes).       You should strive for an efficient implementation of each method.   7 points each (a-h)     a.  addFront         receives an item to add as a parameter, and adds to the front of the list.     b.  addEnd         receives an item to add as a parameter, and adds to the end of the list.     c.  removeFront         removes a node from the front of the list.     d.  removeEnd         removes a node from the end of the list.     e.  set         receives a position and item as parameters, sets the element at this         position, provided it is within the current size     f.  get         receives a position as a parameter, returns the item at this position,         provided it is within the current size     g.  swap         receives two index positions as parameters, and swaps the nodes at         these positions, provided both positions are within the current size     h.  shift         receives an integer as a parameter, and shifts the list forward or         backward this number of nodes, provided it is within the current size   11 points each (i-l)    i.  removeMatching         receives a value of the generic type as a parameter and removes all         occurrences of this value from the list.     j.  erase          receives an index position and number of elements as parameters, and         removes elements beginning at the index position for the number of          elements specified, provided the index position is within the size         and together with the number of elements does not exceed the size     k.  insertList         receives a generic List (a Java List) and an index position as parameters,          and copies each value of the passed list into the current list starting         at the index position, provided the index position does not exceed the size.         For example, if list has a,b,c and another list having 1,2,3 is inserted at         position 2, the list becomes a,b,1,2,3,c     l.  main         add code to the main method to demonstrate each of your methods 

Explanation / Answer

public class GenLinkedList<AnyType>

{

private int theSize;

private Node<anyType> beginMarker;

private Node<AnyType> endMarker;

public static class Node<AnyType>

{

public Node(AnyType d, Node<AnyType> p, Node<AnyType> n)

{

data = d;

previous = p;

next = n;

}

AnyType next;

Node<AnyType> previous;

Node<AnyType> next;

}

public void addFront(AnyType d)

{

if(theSize ==0)

beginmarker = new Node<AnyType> (d,null,null);

else

  

beginMarker = new Node<AnyType>(d,null,beginMarker.next);

}