java - I'm having a hard time implementing my linked list class -
my goal make linked list each link char. want take string in argument, take first letter , turn char, , pass rest of string onto next link until whole string stored. have far, although i'm not sure parts of correct or incorrect. looked bunch of examples , seemed default setup.
public class linkedchar{ char data; linkedchar head; linkedchar next; //this empty link constructor public linkedchar(){ next = null; } //this constructor takes string public linkedchar(string input){ if(input.length() > 0){ data = input.charat(0); next = new linkedchar(input.substring(1)); } } }
this code compiles it's not working other manipulation methods. example, length method.
public int length(){ int length = 0; linkedchar curr = head; while(curr != null){ curr = curr.next; length++; } return length; }
when used, length returned 0. i'm not sure section of code has error , don't know how fix it. great, thanks.
in constructors, you're never initializing head
anything, in length method when set linkedchar curr = head;
you're setting curr
null , length
never gets incremented in while loop.
Comments
Post a Comment