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

This is a LinkedList class, but I am having a bit of trouble with some methods,

ID: 3726958 • Letter: T

Question

This is a LinkedList class, but I am having a bit of trouble with some methods, can you see the problem?

public class LinkedList<T> implements ListInterface<T> {

   private class Node<T> {

      

       private T data;

       private Node<T> link;

      

       public Node (T data) {

          

           this.data = data;

           this.link = link;

       }

   }

  

   private Node<T> firstNode;

   private Node<T> lastNode;

   private Node<T> targetNode;

   private Node<T> trailTargetNode;

   private boolean found;

   private int data;

   private LinkedList<Integer> next;

  

  

  

   private void find(T target) {

      

       targetNode = firstNode;

       trailTargetNode = null;

       found = false;

      

       while(targetNode !=null && !targetNode.data.equals(target)) {

           trailTargetNode= targetNode;

           targetNode= targetNode.link;

       }

       if(targetNode == null)

           return;

       else

           found = true;

       return;

   }

  

   public LinkedList() {

      

       firstNode = lastNode = null;

   }

  

   public LinkedList(LinkedList<T> otherList) {

      

       if(!otherList.isEmpty()) {

          

           this.reset();

      

           T dataValue;

           Node<T> currentNode = otherList.firstNode;

          

           while(currentNode != null) {

              

               dataValue = currentNode.data;

               this.insert(dataValue);

               currentNode = currentNode.link;

              

           }

      

       }

  

   }

  

  

  

   /**

   *

   * Reverse list.

   *

   */

/* Function to reverse the linked list */

   void reverse() {

       Node prev = null;

       Node current = firstNode;

       Node next = null;

      

       while (current != null) {

           next = current.link;

           current.link = prev;

           prev = current;

           current = next;

}

firstNode = prev;

}

   /*

   * N th Item on List

   */

  

  

public Object nthItem(int n)

{

Node current = firstNode;

int count = 0;

  

while (current != null)

{

if (count == n)

return current.data;

count++;

current = current.link;

}

  

if(count < n ){

System.out.println("There is no "+n+"th item on the list");

  

}

return 0;

}

  

  

   @Override

   public int size() {

      

       int nodeCount = 0;

       Node<T> currentNode = firstNode;

      

       if(isEmpty())

           return nodeCount;

      

       while(currentNode != null) {

           nodeCount++;

           currentNode = currentNode.link;

       }

       return nodeCount;

   }

   @Override

   public void reset() {

      

       firstNode = lastNode= null;

      

       return;

      

   }

   @Override

   public boolean isEmpty() {

      

       return (firstNode == null);

   }

  

   public boolean insert(T element) {

      

       Node<T> newNode = new Node<T> (element);

  

       if(this.isEmpty()) {

           firstNode = lastNode = newNode;

           return true;

       }

      

       lastNode.link = newNode;

       lastNode = lastNode.link;

       return true;

   }

   public void insertAtFront(T element ) {

       Node<T> newNode = new Node<T> (element);

      

       if(this.isEmpty()) {

           firstNode = lastNode = newNode;

           return;

       }

       newNode.link = firstNode;

       firstNode = newNode;

       return;

      

   }

  

   public Node sort(Node headOriginal) {

   if (headOriginal == null || headOriginal.link == null)

   return headOriginal;

   Node a = headOriginal;

   Node b = headOriginal.link;

   while ((b != null) && (b.link != null)) {

   headOriginal = headOriginal.link;

   b = (b.link).link;

   }

   b = headOriginal.link;

   headOriginal.link = null;

   return merge(sort(a), sort(b));

}

   public Node merge(Node a, Node b) {

   Node temp = new Node();

   Node head = temp;

   Node c = head;

   while ((a != null) && (b != null)) {

   if (a.data <= b.data) {

   c.link = a;

   c = a;

   a = a.link;

   } else {

   c.link = b;

   c = b;

   b = b.link;

   }

   }

   c.link = (a == null) ? b : a;

   return head.link;

}

     

   public void divide(LinkedList otherList, int value) {

   Node temp = this.getFirst();

   if (temp != null && temp.data == data) {

   this.setFirst(null);

   otherList.setFirst(temp);

   return;

   }

   while (temp.link != null) {

   if(temp.link.data == data) {

   otherList.setFirst(temp.link);

   temp.link = null;

   return;

   }

   temp = temp.link;

   }

}

   public Node getFirst() {

   return firstNode;

   }

   public void setFirst(Node first) {

   this.firstNode = firstNode;

   }

  

   @Override

   public boolean remove(T target) {

       find(target);

      

       if (!found)

       return false;

      

       if(target == firstNode) {

           firstNode = firstNode.link;

           return true;

       }

       if(size()==1) {

           reset();

           return true;

       }

       if(targetNode == lastNode) {

           lastNode = trailTargetNode;

           lastNode.link = null;

                   return true;

       }

       return false;

   }

   @Override

   public boolean contains(T target) {

       find(target);

      

       return found;

   }

  

  

   public String toString() {

       String str = "";

      

       if (isEmpty()) {

           str = "List is Empty";

           return str;

       }

       Node<T> current = firstNode;

       while(current!=null) {

           str = str + current.data + " ";

                   current = current.link;

       }

       return str;

   }

}

Explanation / Answer

You have not indicated which one's not working. I have fixed reverse(), setFirst(), remove().


public class LinkedList<T> implements ListInterface<T> {

private class Node<T> {

private T data;

private Node<T> link;

public Node(T data) {

this.data = data;

this.link = link;

}

}

private Node<T> firstNode;

private Node<T> lastNode;

private Node<T> targetNode;

private Node<T> trailTargetNode;

private boolean found;

private int data;

private LinkedList<Integer> next;

private void find(T target) {

targetNode = firstNode;

trailTargetNode = null;

found = false;

while (targetNode != null && !targetNode.data.equals(target)) {

trailTargetNode = targetNode;

targetNode = targetNode.link;

}

if (targetNode == null)

return;

else

found = true;

return;

}

public LinkedList() {

firstNode = lastNode = null;

}

public LinkedList(LinkedList<T> otherList) {

if (!otherList.isEmpty()) {

this.reset();

T dataValue;

Node<T> currentNode = otherList.firstNode;

while (currentNode != null) {

dataValue = currentNode.data;

this.insert(dataValue);

currentNode = currentNode.link;

}

}

}

/**
*
*
*
* Reverse list.
*
*
*
*/

/* Function to reverse the linked list */

void reverse() {

Node prev = null;
Node current = firstNode;
Node next = null;

while (current != null) {
next = current.link;
current.link = prev;
prev = current;
current = next;
}

lastNode = firstNode;
firstNode = prev;

}

/*
*
* N th Item on List
*
*/

public Object nthItem(int n)
{

Node current = firstNode;
int count = 0;

while (current != null)
{
if (count == n)
return current.data;
count++;
current = current.link;

}

if (count < n) {
System.out.println("There is no " + n + "th item on the list");
}

return null;

}

@Override

public int size() {

int nodeCount = 0;

Node<T> currentNode = firstNode;

if (isEmpty())

return nodeCount;

while (currentNode != null) {

nodeCount++;

currentNode = currentNode.link;

}

return nodeCount;

}

@Override

public void reset() {

firstNode = lastNode = null;

return;

}

@Override

public boolean isEmpty() {

return (firstNode == null);

}

public boolean insert(T element) {

Node<T> newNode = new Node<T>(element);

if (this.isEmpty()) {

firstNode = lastNode = newNode;

return true;

}

lastNode.link = newNode;

lastNode = lastNode.link;

return true;

}

public void insertAtFront(T element) {

Node<T> newNode = new Node<T>(element);

if (this.isEmpty()) {

firstNode = lastNode = newNode;

return;

}

newNode.link = firstNode;

firstNode = newNode;

return;

}

public Node sort(Node headOriginal) {

if (headOriginal == null || headOriginal.link == null)

return headOriginal;

Node a = headOriginal;
Node b = headOriginal.link;

while ((b != null) && (b.link != null)) {

headOriginal = headOriginal.link;

b = (b.link).link;

}

b = headOriginal.link;

headOriginal.link = null;

return merge(sort(a), sort(b));

}

public Node merge(Node a, Node b) {

Node temp = new Node();

Node head = temp;

Node c = head;

while ((a != null) && (b != null)) {

if (a.data <= b.data) {

c.link = a;

c = a;

a = a.link;

} else {

c.link = b;

c = b;

b = b.link;

}

}

c.link = (a == null) ? b : a;

return head.link;

}

public void divide(LinkedList otherList, int value) {

Node temp = this.getFirst();

if (temp != null && temp.data == data) {

this.setFirst(null);

otherList.setFirst(temp);

return;

}

while (temp.link != null) {

if (temp.link.data == data) {

otherList.setFirst(temp.link);

temp.link = null;

return;

}

temp = temp.link;

}

}

public Node getFirst() {
return firstNode;

}

public void setFirst(Node first) {

this.firstNode = first;
if(lastNode == null)
lastNode = first;
}

@Override

public boolean remove(T target) {

find(target);

if (!found)
return false;

if (target == firstNode) {

firstNode = firstNode.link;
if(firstNode == null)
lastNode = null;
return true;

}

trailTargetNode.link = targetNode.link;

if (targetNode == lastNode) {
lastNode = trailTargetNode;
}

return true;

}

@Override

public boolean contains(T target) {

find(target);

return found;

}

public String toString() {

String str = "";

if (isEmpty()) {

str = "List is Empty";

return str;

}

Node<T> current = firstNode;

while (current != null) {

str = str + current.data + " ";

current = current.link;

}

return str;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote