I want to remove all of the elements in my linked list without using the clear
method.
This is my code currently
//-----inner class-----
private static class Node <T> {
private T data;
//next just points
private Node<T> next;
//constructs a node
public Node(T data, Node<T> next){
this.data = data;
this.next = next;
}
public T getData(){ return data;}
public Node<T> getNext(){ return next;}
}
private LinkedList<T> theList = new LinkedList<T>();
private Node<T> head;
private Node<T> tail = null;
private int size=0;
public ListNoOrder() {
this.head = null;
size = 0;
}
//an add method
public void add(T newElt) {
//if the new element equals null
//catch the exception
try{ if (newElt==(null));}
catch (Exception illegalArgumentException){
throw new IllegalArgumentException();}
//if it doesn't catch an exception it adds the element to the list
//and increment the size by one
//what does the head = new Node<T>(newElt, head) mean???
head = new Node<T>(newElt, head);
size++;
}
The reset method that I want to implement If my current list has four objects after calling this method I want that list to have 0 objects
public void reset() {
head = null;
}
It should work but everytime I test it says nothing's been deleted. This is just bits and pieces of the complete code.