0

I need to make a singly-linked list for my array of integers, but, I don't know what is currently wrong with my code right now.

This is the code to create the nodes. (the data)

package sllset;

class SLLNode {
    int value;
    SLLNode next;

public SLLNode(int i, SLLNode n){
    value = i;
    next = n   
    }   
}

My other class which has my methods and constructors look like this.

package sllset;


public class SLLSet {
    private int setSize;
    private SLLNode head;

public SLLSet(){
    head = null;
    setSize = 0;
}

public SLLSet(int[] sortedArray){ //second constructor
    setSize = sortedArray.length;
    int i;
    head=null;
    for(i=0;i<setSize;i++){
        head.next = head;
        head = new SLLNode(sortedArray[i],head.next);    
    }
}


public String toString(){
    SLLNode p;
    String result = new String();
    for(p=head ; p!=null ; p=p.next)
        result += p.value;
    return result;
}

public static void main(String[] args) {
int[] A = {2,3,6,8,9};
SLLSet a = new SLLSet(A);
System.out.println(a.toString());

    }

}

My problem is that my second constructor doesn't work and I dont really have a clue as to why. I have been following a guide on how to make most of these functions so my knowledge of the code I guess isn't great enough to decipher the problem.

EDIT: So someone told me to specify the problem which I get a NULLPointerException at line 19; where I code head.next = head; . However, when I remove that part to test, line 20 gets the error message

progyammer
  • 1,498
  • 3
  • 17
  • 29
joeymed
  • 13
  • 1
  • 5

1 Answers1

1

Let's look at this

head=null;     // you are setting head to null
for(i=0;i<setSize;i++){
    head.next = head;  // see two lines up, head is null, it can not have next

Your constructor has some problems. Try using this version:

public SLLSet(int[] sortedArray){ //second constructor
    head = null;
    if (sortedArray == null || sortedArray.length == 0) {
        setSize = 0;
    }
    setSize = sortedArray.length;
    head = new SLLNode(sortedArray[0], null);
    SLLNode curr = head;

    for (int i=1; i < setSize; ++i) {
        curr.next = new SLLNode(sortedArray[i], null);
        curr = curr.next;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Doesn't the code mean that before the loop my head = null (nothing in the list yet besides null). Then when I start looping in integers the value null would be moved to the right? OHH or does it mean that my next head (head means the beginning of the list right?) Im basically saying the next value is null again? – joeymed Nov 15 '16 at 05:16
  • Yes, when you create each new node, the next value is by default `null`. This is how linked lists work, it has to end somewhere. Try the code and come back here with comments. – Tim Biegeleisen Nov 15 '16 at 05:17
  • I tried the code that was given but there seems to be a error: uncomplilable source code - Erroneous sym type: sllset.SLLSet.SLLNode. at curr.next = SLLNode(sortedArray[i],null). EDIT: Nevermind, i fixed it by adding new in front of it. Seems to have fixed it – joeymed Nov 15 '16 at 05:24
  • I think i fixed it by allocating memory for it. – joeymed Nov 15 '16 at 05:27
  • `curr.next = new SLLNode(sortedArray[i],null);` – Scary Wombat Nov 15 '16 at 05:27
  • Feel free to mark this answer correct if it solved your problem, thanks. – Tim Biegeleisen Nov 15 '16 at 05:29