rimbleysucksatjava
rimbleysucksatjava

Reputation: 13

I'm having a hard time implementing my linked list class

My goal is to make a linked list where each link is a char. I want it to take a string in as an argument, take the first letter and turn it into a char, and then pass the rest of the string onto the next link until the whole string is stored. This is what I have so far, although I'm not really sure which parts of it are correct or incorrect. I looked up a bunch of examples and this seemed to be the default setup.

public class linkedChar{

    char data;
    linkedChar head;
    linkedChar next;

    //This is the empty link constructor
    public linkedChar(){
        next = null;
    }
    //This is the constructor that takes a string
    public linkedChar(String input){
        if(input.length() > 0){
            data = input.charAt(0);
            next = new linkedChar(input.substring(1));
        }
    }
}

This code compiles but it's not working with my other manipulation methods. For example, my length method.

public int length(){
    int length = 0;
    linkedChar curr = head;
    while(curr != null){
        curr = curr.next;
        length++;
    }
    return length;
}

When used, the length returned is always 0. I'm not sure which section of code has the error and I don't know how to fix it. Any help would be great, thanks.

Upvotes: 1

Views: 87

Answers (3)

SSDong
SSDong

Reputation: 145

The problem you have is due to the linkedChar head; as Java compiler will zero out value(i.e set it as null) for you. Hence, your length() function will always stop at the first round.

A quick fix is to simply abandon the linkedChar head field and set linkedChar curr in your length() function to be next. This will fix your problem.

i.e. Make your code as following

class Linked{

  char data;
  Linked next;

  //This is the empty link constructor
  public Linked(){
    next = null;
  }
  public int length(){
    int length = 0;
    Linked curr = next;
    while(curr != null){
      curr = curr.next;
      length++;
    }
    return length;
  }

  //This is the constructor that takes a string
  public Linked(String input){
    if(input.length() > 0){
      data = input.charAt(0);
      next = new Linked(input.substring(1));
    }
  }
}

public class LinkedChar {
  public static void main(String[] args) {
    Linked l = new Linked("abcd");
    // Here it will print out 4
    System.out.println(l.length());
  }
}

Good luck.

Upvotes: 0

Nitin V Pujari
Nitin V Pujari

Reputation: 51

In the constructor head=null, then in length method linkedChar curr = null; Hence length never gets incremented and remains at zero. Because the while loop does not satisfy the entry condition.

Upvotes: 1

mcmathews
mcmathews

Reputation: 48

In your constructors, you're never initializing head to anything, so in your length method when you set linkedChar curr = head; you're setting curr to null and thus length never gets incremented in your while loop.

Upvotes: 0

Related Questions