Hello,
I am trying to solve this problem but I don't really know where to start.
My task is to fill the empty methods in the class SimpleLinkedList
I started by trying to fill the method insert (the empty one) but I couldn't find how to fill it on the given index. I know that if I can if and else in order to insert the node either at the head or the tail of the list but since I need to insert it on the given index I do not know how to solve this.
I have tried to write a code in the insert method but it is not working properly. My indexes are wrong, when I type index 1 it writes the name on the first position instead of the second position and so on. Also I need it to show me an error if the index is larger than the list's size.
I need to fill the methods size, remove and toString as well but I haven't started yet because I don't understand where to start.
Thanks in advance for your advice and help.
import java.sql.SQLOutput; import java.util.*; /** * The class SimpleLinkedList is a linked list that includes nodes of objects. * The first node in the list includes no objects. */ public class SimpleLinkedList { private ListNode header; public SimpleLinkedList( ) { header = new ListNode( null ); } /** Creates a new ListNode with a new object and adds it to the list. */ public void insert(String theobj) { ListNode nynode = new ListNode(theobj); ListNode temp = header; while(temp.next != null) { temp = temp.next; } temp.next = nynode; } /* Removes the node that includes the respective object. */ public void remove(String theobj) { ListNode node = header; while(node.next != null) { if(node.next.element.equals(theobj)) { node.next = node.next.next; break; } node = node.next; } } /** Prints the content of the list. */ public void print() { ListNode node = header.next; while(node != null) { System.out.println(node.element); node = node.next; } } public void insert2(String theobj) { ListNode nynode = new ListNode(theobj, header.next); header.next = nynode; } // This method has to be filled. // Adds an object at the given index (position). The index must not be negative or bigger than the list's size. public void insert(String obj, int index) { ListNode node = new ListNode(obj); if (header == null) { if (index != 0) { return; } else { header = node; } } if (header != null && index == 0) { node.next = header; header = node; return; } ListNode current = header; ListNode previous = null; int i = 0; while (i < index) { previous = current; current = current.next; if (current == null) { break; } i++; } node.next = current; previous.next = node; } // Returns and removes the object from the given index in the list. public String remove(int index) { // temporary return return null; } //Returns the size of the list. //Task: Add a variable in the class and update it when needed. public int size() { // temporary return return 0; } public String toString() { return null; } public static void main (String[] cmdLn) { SimpleLinkedList klassLista = new SimpleLinkedList(); klassLista.insert("Olle"); klassLista.insert2("Nina"); klassLista.insert("Mario"); //klassLista.print(); //klassLista.remove("Nina"); //klassLista.print(); klassLista.insert("Stef", 0); klassLista.print(); } }
This is the ListNode class that I am using:
import java.util.*; class ListNode { public String element; public ListNode next; // Constructors public ListNode( String theElement ) { this( theElement, null ); } public ListNode( String theElement, ListNode n ) { element = theElement; next = n; } public static void main (String [] a) { ListNode n1=new ListNode("Nina"); ListNode n2=new ListNode("Kalle",n1); ListNode n3=new ListNode("Olle",n2); ListNode n4=new ListNode("Mio",n3); ListNode n=n4; while(n!=null) { System.out.println(n.element); n=n.next; } } }