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

IN JAVA- Please Answer question fully- If not answer I-L Create a generic class

ID: 3889352 • Letter: I

Question

 IN JAVA- Please Answer question fully- If not answer I-L 
 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

Hi Let me know if you need more information:-

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

import java.util.ArrayList;
import java.util.List;

public class GLList<T> {
   private Node<T> head;

   private static class Node<T> {
       public T value;
       public Node<T> next;

       public Node(T value) {
           this(value, null);
       }

       public Node(T value, Node<T> next) {
           this.value = value;
           this.next = next;
       }
   }

   /*
   * a. addFront receives an item to add as a parameter, and adds to the front of
   * the list.
   */
   public void addFront(T item) {
       head = new Node<T>(item, head);
   }

   /*
   * b. addEnd receives an item to add as a parameter, and adds to the end of the
   * list.
   */
   public void addEnd(T item) {
       if (head == null) {
           head = new Node<T>(item);
           return;
       }
       Node<T> end = head;
       while (end.next != null)
           end = end.next;
       end.next = new Node<T>(item);
   }

   /*
   * c. removeFront removes a node from the front of the list.
   */
   public T removeFront() {
       if (head == null)
           return null;
       Node<T> front = head;
       head = head.next;
       return front.value;
   }

   /*
   * d. removeEnd removes a node from the end of the list.
   */
   public T removeEnd() {
       if (head == null)
           return null;
       Node<T> prev = null, end = head;
       while (end.next != null) {
           prev = end;
           end = end.next;
       }
       if (prev != null) {
           prev.next = null;
           return end.value;
       } else {
           head = null;
           return end.value;
       }
   }

   private Node<T> getToEnd() {
       Node<T> prev = head;
       while (prev != null && prev.next != null && prev.next.next != null)
           prev = prev.next;
       return prev;
   }

   private Node<T> getEndNode() {
       Node<T> end = head;
       while (end != null && end.next != null)
           end = end.next;
       return end;
   }

   private Node<T> getNode(int index) throws IndexOutOfBoundsException {
       return getNext(index, head);
   }

   private Node<T> getNext(int index, Node<T> start) {
       if (index < 0 || start == null)
           throw new IndexOutOfBoundsException("Given index: " + index);

       int pos = 0;

       while (index != pos) {
           if (start.next == null)
               throw new IndexOutOfBoundsException("Given index: " + index);
           start = start.next;
           pos++;
       }
       return start;
   }

   /*
   * e. set receives a position and item as parameters, sets the element at this
   * position, provided it is within the current size
   */
   public void set(int index, T item) {
       getNode(index).value = item;
   }

   /*
   * f. get receives a position as a parameter, returns the item at this position,
   * provided it is within the current size
   */

   public T get(int index) {
       return getNode(index).value;
   }

   /*
   * g. swap receives two index positions as parameters, and swaps the nodes at
   * these positions, provided both positions are within the current size
   */
   public void swap(int index1, int index2) throws IndexOutOfBoundsException {
       int firstIndex = Math.min(index1, index2), secondIndex = Math.max(index1, index2);
       Node<T> first = getNode(firstIndex);
       Node<T> second = getNext(secondIndex - firstIndex, first);
       T temp = first.value;
       first.value = second.value;
       second.value = temp;
   }

   /*
   * 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
   */
   public void shift(int numShifts) {
       if (numShifts == 0 || head == null || head.next == null)
           return;
       else if (numShifts < 0) {
           Node<T> oldHead = head;
           head = head.next;
           getEndNode().next = oldHead;
           oldHead.next = null;
           shift(numShifts + 1);
       } else {
           Node<T> oldHead = head, prev = getToEnd();
           head = getEndNode();
           head.next = oldHead;
           prev.next = null;
           shift(numShifts - 1);
       }
   }

   /*
   *
   */

   /*
   * 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
   */
   public void erase(int index, int num) {
       if (num <= 0)
           return;
       if (index == 0)
           try {
               head = getNode(num);
           } catch (IndexOutOfBoundsException e) {
               head = null;
           }
       else {
           Node<T> justBefore = getNode(index - 1);
           justBefore.next = getNext(num + 1, justBefore);
       }
   }

   /*
   * 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
   */
   public void insertList(List<T> list, int index) {
       if (list.isEmpty())
           return;
       if (head == null) {
           Node<T> node = new Node<T>(list.get(0));
           head = node;
           for (int k = 1; k < list.size(); k++) {
               node.next = new Node<T>(list.get(k));
               node = node.next;
           }
       } else if (index == 0) {
           Node<T> node = new Node<T>(list.get(0));
           Node<T> oldHead = head;
           head = node;
           for (int k = 1; k < list.size(); k++) {
               node.next = new Node<T>(list.get(k));
               node = node.next;
           }
           node.next = oldHead;
       } else {
           Node<T> begin = getNode(index - 1);
           Node<T> oldNext = begin.next;
           for (T item : list) {
               Node<T> node = new Node(item);
               begin.next = node;
               begin = node;
           }
           begin.next = oldNext;
       }
   }

   /* To diaplay the list */
   public String toString() {
       StringBuffer out = new StringBuffer("[");
       Node<T> temp = head;
       while (temp != null) {
           if (temp != head)
               out.append(',');
           out.append(temp.value.toString());
           temp = temp.next;
       }
       out.append(']');
       return out.toString();
   }

   /*
   * l. main add code to the main method to demonstrate each of your methods
   */
   public static void main(String[] args) {
       GLList<Integer> list = new GLList<Integer>();
       System.out.println("List: " + list);

       list = new GLList<Integer>();
       list.addFront(1);
       System.out.println("List: " + list);

       list = new GLList<Integer>();
       for (int k = 0; k < 10; k++)
           list.addEnd(k);

       for (int k = 0; k < 5; k++)
           list.addEnd(k);
       System.out.println("List: " + list);
       for (int k = 0; k < 5; k++)
           System.out.println(" " + list.removeEnd());
       System.out.println("List: " + list);

       for (int k = 0; k < 5; k++)
           list.addFront(k);
       System.out.println("List: " + list);
       for (int k = 0; k < 5; k++)
           System.out.println(" " + list.removeFront());
       System.out.println("List: " + list);

       list.set(0, 99);
       System.out.println("List: " + list);
       list.swap(0, 1);
       System.out.println("List: " + list);
       list.shift(3);
       System.out.println("List: " + list);
       ArrayList<Integer> arrayList = new ArrayList<Integer>();
       for (int k = -5; k < 0; k++)
           arrayList.add(k);
       System.out.println("Adding arrayList 0");
       list.insertList(arrayList, 0);
       System.out.println("List: " + list);
       System.out.println("Erasing 2 elements");
       list.erase(0, 2);
       System.out.println("List: " + list);

   }

}

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

List: []
List: [1]
List: [0,1,2,3,4,5,6,7,8,9,0,1,2,3,4]
   4
   3
   2
   1
   0
List: [0,1,2,3,4,5,6,7,8,9]
List: [4,3,2,1,0,0,1,2,3,4,5,6,7,8,9]
   4
   3
   2
   1
   0
List: [0,1,2,3,4,5,6,7,8,9]
List: [99,1,2,3,4,5,6,7,8,9]
List: [1,99,2,3,4,5,6,7,8,9]
List: [7,8,9,1,99,2,3,4,5,6]
Adding arrayList 0
List: [-5,-4,-3,-2,-1,7,8,9,1,99,2,3,4,5,6]
Erasing 2 elements
List: [-3,-2,-1,7,8,9,1,99,2,3,4,5,6]


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

Thanks