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

lList - print linkedlist: lList.size() - print linkedlist size: 1 lList.size() -

ID: 3542617 • Letter: L

Question

lList - print linkedlist: lList.size() - print linkedlist size: 1 lList.size() - print linkedlist size: 1 lList - print linkedlist: My list wont display, can someone point whats wrong
public interface ListInterface { //List operations public boolean isEmpty(); public int size(); public void add(int index, Object item); public void remove(int index); public void removeAll(); }// end interface public class Node { Object item; Node next;
Node(Object newItem) { item = newItem; next = null;
}
Node(Object newItem, Node nextNode) { item = newItem; next = nextNode; } public void setItem(Object newItem) { item = newItem; } // end setItem
public Object getItem() { return item; } // end getItem
public void setNext(Node nextNode) { next = nextNode; } // end setNext
public Node getNext() { return next; } // end getNext // end class Node } public class ListReferencedBased implements ListInterface {
private Node head; private int numItems;

public ListReferencedBased(){ numItems = 0; head = null; }

@Override public boolean isEmpty() { // TODO Auto-generated method stub
return numItems == 0; }


public int size() { // TODO Auto-generated method stub return numItems; }
private Node find(int index){ Node curr = head; for(int skip = 0;skip < index;skip++){ curr = curr.next; } return curr;
}
public void add(int index, Object item) { // TODO Auto-generated method stub if(index == 0 && index < numItems + 1){ if(index ==0){ // insert in begning Node newNode = new Node(item, head); head = newNode; } else{ Node prev = find(index - 1);
Node newNode = new Node(item,prev.next); prev.next = newNode; } numItems++; } }



@Override public void remove(int index) { // TODO Auto-generated method stub if (index == 0){ head = head.next; } else{ Node prev = find(index -1);
Node curr = prev.next; prev.next = curr.next; } numItems--; }

@Override public void removeAll() { head = null; numItems = 0;


} public String toString() { Node crunchifyCurrent = head.getNext(); String output = ""; while (crunchifyCurrent != null) { output += "[" + crunchifyCurrent.toString() + "]"; crunchifyCurrent = crunchifyCurrent.getNext(); } return output; } } public class Nodemain {
public static void main(String[] args) { ListReferencedBased lList = new ListReferencedBased();
// add elements to LinkedList lList.add(0,"1"); lList.add(1,"2"); lList.add(2,"3"); lList.add(3,"4"); lList.add(4,"5");
/* * Please note that primitive values can not be added into LinkedList * directly. They must be converted to their corresponding wrapper * class. */
System.out.println("lList - print linkedlist: " + lList); System.out.println("lList.size() - print linkedlist size: " + lList.size()); // System.out.println("lList.get(3) - get 3rd element: " + lList.get(3)); // System.out.println("lList.remove(2) - remove 2nd element: " + lList.remove(2)); // System.out.println("lList.get(3) - get 3rd element: " + lList.get(3)); System.out.println("lList.size() - print linkedlist size: " + lList.size()); System.out.println("lList - print linkedlist: " + lList); } } lList - print linkedlist: lList.size() - print linkedlist size: 1 lList.size() - print linkedlist size: 1 lList - print linkedlist: My list wont display, can someone point whats wrong
public interface ListInterface { //List operations public boolean isEmpty(); public int size(); public void add(int index, Object item); public void remove(int index); public void removeAll(); }// end interface public class Node { Object item; Node next;
Node(Object newItem) { item = newItem; next = null;
}
Node(Object newItem, Node nextNode) { item = newItem; next = nextNode; } public void setItem(Object newItem) { item = newItem; } // end setItem
public Object getItem() { return item; } // end getItem
public void setNext(Node nextNode) { next = nextNode; } // end setNext
public Node getNext() { return next; } // end getNext // end class Node } public class ListReferencedBased implements ListInterface {
private Node head; private int numItems;

public ListReferencedBased(){ numItems = 0; head = null; }

@Override public boolean isEmpty() { // TODO Auto-generated method stub
return numItems == 0; }


public int size() { // TODO Auto-generated method stub return numItems; }
private Node find(int index){ Node curr = head; for(int skip = 0;skip < index;skip++){ curr = curr.next; } return curr;
}
public void add(int index, Object item) { // TODO Auto-generated method stub if(index == 0 && index < numItems + 1){ if(index ==0){ // insert in begning Node newNode = new Node(item, head); head = newNode; } else{ Node prev = find(index - 1);
Node newNode = new Node(item,prev.next); prev.next = newNode; } numItems++; } }



@Override public void remove(int index) { // TODO Auto-generated method stub if (index == 0){ head = head.next; } else{ Node prev = find(index -1);
Node curr = prev.next; prev.next = curr.next; } numItems--; }

@Override public void removeAll() { head = null; numItems = 0;


} public String toString() { Node crunchifyCurrent = head.getNext(); String output = ""; while (crunchifyCurrent != null) { output += "[" + crunchifyCurrent.toString() + "]"; crunchifyCurrent = crunchifyCurrent.getNext(); } return output; } } public class Nodemain {
public static void main(String[] args) { ListReferencedBased lList = new ListReferencedBased();
// add elements to LinkedList lList.add(0,"1"); lList.add(1,"2"); lList.add(2,"3"); lList.add(3,"4"); lList.add(4,"5");
/* * Please note that primitive values can not be added into LinkedList * directly. They must be converted to their corresponding wrapper * class. */
System.out.println("lList - print linkedlist: " + lList); System.out.println("lList.size() - print linkedlist size: " + lList.size()); // System.out.println("lList.get(3) - get 3rd element: " + lList.get(3)); // System.out.println("lList.remove(2) - remove 2nd element: " + lList.remove(2)); // System.out.println("lList.get(3) - get 3rd element: " + lList.get(3)); System.out.println("lList.size() - print linkedlist size: " + lList.size()); System.out.println("lList - print linkedlist: " + lList); } }

Explanation / Answer

And there seems to be small mistakes in code like add method is implemented wrong i corrected it and for printing list i am pasting while loop here and also we access find method outside the class scope so remove private access modifier


removing access modifier to find method :

Node find(int index){
Node curr = head;
for(int skip = 0;skip < index;skip++){
    System.out.println(curr.next);;
curr = curr.next;
}
return curr;

}


Changed add method :
public void add(int index, Object item) {
// TODO Auto-generated method stub
//if(index == 0) {
    System.out.print("here 1");
if(index ==0){
    System.out.print("here 2");
// insert in begning
Node newNode = new Node(item, head);
head = newNode;
}
else{
    System.out.print("here 3");
Node prev = find(index - 1);

Node newNode = new Node(item,prev.next);
prev.next = newNode;
}
numItems++;

}


Printig List Implementation :

int i=0;

System.out.println("lList - print linkedlist: " );
while(lList.size()>i){
       
    System.out.println(lList.find(0).getItem().toString());
    i++;
   
}


I checked it gives correct result with these changes