The Student Room Group

JAVA linked list constructor always returns null

I've been working on a character sequence (analogous to a string) implemented as a linked list. Each node in the list consists of three fields: data (i.e., what character it represents), value (i.e., how many times it is repeated in a row) and next (i.e., what node it is followed by). One of the constructors "translates" a string into a list as such. However, it happens to always return a null list, as the output of the code

private static void StringToNode (String s, int i, int j,CharNode head)
{
head=new CharNode(s.charAt(i),j,null);
if (i+1<s.length()&&s.charAt(i+1)==s.charAt(i))
{
StringToNode(s, i+1, j+1, head);
}
else if (i+1<s.length()&&s.charAt(i+1)!=s.charAt(i))
{
StringToNode(s,i+1,1,head.getNext());
}
}
public StringList(String s)
{
if (s.length()==0)
_head=null;
else
StringToNode(s,0,1,_head);
if (_head==null)
System.out.println("Rats!");
}


is Rats! I can't come up with any reason for this code to fail. So help will be greatly appreciated.
Hi there,

While you're waiting for an answer, did you know we have 300,000 study resources that could answer your question in TSR's Learn together section?

We have everything from Teacher Marked Essays to Mindmaps and Quizzes to help you with your work. Take a look around.

If you're stuck on how to get started, try creating some resources. It's free to do and can help breakdown tough topics into manageable chunks. Get creating now.

Thanks!

Not sure what all of this is about? Head here to find out more.

Quick Reply

Latest

Trending

Trending