Search code examples
javahead

Keeping track of head of ListNode in Java


I am currently working on my own Java class called LString, which is meant to convert back and forth between linked lists of characters and Strings.

I am having issues with my toString() method, specifically keeping track of the "head" of the linked list in order to loop through it and concatenate the characters into a new string. While researching, I read that I am supposed to keep track of the head of the list somehow, but I can't figure out how to implement it.

Any help would be greatly appreciated!

Edit: The error message I am receiving is:

LString.java:79: error: cannot find symbol
ListNode current = this.front;

public class LString{


   private static int length;
   // ListNode constructors

   // Creates a new ListNode with characters stored in variable "data" and 
   // Node named next
   private class ListNode{
      char item;
      ListNode next;


      private ListNode(){
      }

      // creates a new ListNode that has the value and links to the specified ListNode
      private ListNode(char item, ListNode next){
         this.item = item;
         this.next = next;
      }

      // given a character, creates a new ListNode that doesn't link to anything
      private ListNode(char item){
         this.item = item;
         this.next = null;
      }


   }


   public LString(){
      this.length = 0;
      ListNode front = new ListNode();
   }

   //LString
   // Takes in a String object and loops until it has added all characters to a new linked list
   public LString(String original){

      ListNode front;
      this.length = 1;                           // length keeps track of number of nodes

      if (original.charAt(0) == 0){             // creates a new ListNode if it is an empty string
         front = new ListNode();       
      }
      else {
         front = new ListNode(original.charAt(0));
      }


      //System.out.println("this is happening " + front.item);

      //ListNode current = front;  
      for (int index = 1; index < original.length(); index++) {
         front.next = new ListNode(original.charAt(index), front.next);
         front = front.next;
         //System.out.println("strings: " + front.item);
         length++;
      }
         //System.out.println("length: " + length);
   }

   // returns length of the LString object
   public int length(){
      return this.length;
   }

   // toString takes an LString object and converts it to a string
   public String toString(){
      StringBuilder newString;

      ListNode current = this.front;
      while (current.next != null){
         newString.append(current.item);
         current = current.next; 
      }

      return newString.toString();
   }

   public static void main(String[] args){
      LString stuffTest = new LString("hello");
      int valueOf = stuffTest.length();
      System.out.println(stuffTest.length());
      String testMeWhy = stuffTest.toString();

   }





}

Solution

  • The general pattern for building a linked list by appending to the end is:

    At the beginning:

    head = null;
    tail = null;
    

    To append newNode to the list:

    if (head == null) {
        head = newNode;
    } else {
        tail.next = newNode;
    }
    tail = newNode;
    

    I think you're trying to do this by keeping just one pointer in the list class, which doesn't work very well. Also, doing things using this pattern means you don't have to have a "special" node at the front of the list, unless there's some other good reason to. It looks like you were trying to use new ListNode() with no arguments to create some kind of special node, but only sometimes. It's unnecessary and just makes things more complicated.