-3

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.

lionbear28
  • 23
  • 8
  • 2
    Is this `java.util.LinkedList` or your own implementation? – Jeremy Mar 08 '17 at 17:11
  • 1
    Your question text says the exact opposite of your title?! – GhostCat Mar 08 '17 at 17:14
  • @Jeremy my own implementation – lionbear28 Mar 08 '17 at 17:50
  • @GhostCat my error oops – lionbear28 Mar 08 '17 at 17:51
  • @lionbear28, you need to show more code and example input and output. Right now, there's no way for us to figure out what you're trying to do. – Jeremy Mar 08 '17 at 17:52
  • @Jeremy what more specifically? if my list had say 4 elements in it I want my list to have 0 elements after calling my reset method. – lionbear28 Mar 08 '17 at 17:55
  • 1
    @lionbear28 Show us how you're determining that. Right of the bat, you're not changing the `size` variable to `0`... – Jeremy Mar 08 '17 at 17:57
  • @lionbear28 if thats your own implementation I doubt anyone would know better off but you as to which method should be really used. You left us with mere details in such case. Please share the implementaion atleast. – Naman Mar 08 '17 at 17:57
  • @Jeremy I've added more info! – lionbear28 Mar 08 '17 at 18:08
  • @nullpointer I've shared the implementation...I know it should be as simple as changing the head.next reference to null but it's still not working – lionbear28 Mar 08 '17 at 18:09
  • Sorry, your input is still confusing. Please read about [mcve] and then provide one. I really don't get for example why you have that theList member field, but then you are not using that but fully implementing your own list. – GhostCat Mar 08 '17 at 18:19

1 Answers1

0
public void reset() {
     head = null;
 }

(You)

It should work but everytime I test it says nothing's been deleted. This is just bits and pieces of the complete code

(My comment)

You remove the reference to the first element of the list so then if are no reference this list exist only in memory of JVM and Garbage Colletor will remove it from memory, but you don't set the size to zero (size=0;) and when test or somthing else will check the list it stil informs that have for eg "size = 6". From the other side you have function that is reset your list

public ListNoOrder() {
   this.head = null; 
   size = 0; 
}

All comments written in BrokenEnglish :)

Mbb
  • 9
  • 2