简体   繁体   中英

Last Node not tracked, Linked List Java

My problem occurs when I track lastC, the last node in my linked list. For some reason, outside of the method it seems to be null constantly. I want to see if there is a better way to track it to improve run time on my program.

Append:

public MyStringBuilder append(String s)
{

    if(this.length == 0) {
        firstC = new CNode(s.charAt(0));
        length = 1;
        CNode currNode = firstC;
        // Create remaining nodes, copying from String.  Note
        // how each new node is simply added to the end of the
        // previous one.  Trace this to see what is going on.
        for (int i = 1; i < s.length(); i++)
        {
            CNode newNode = new CNode(s.charAt(i));
            currNode.next = newNode;
            currNode = newNode;
            length++;
        }
        lastC = currNode;
        //System.out.println(lastC.data);
        //lastC.next = null;//new
    } else {
        if(lastC == null) {
            lastC = getCNodeAt(length - 1);
            //System.out.println("no");
        }
        //System.out.println(lastC.data);
        //CNode currNode = lastC;
        //CNode currNode = firstC;//changed
        //for(int j = 0; j < length - 1; j++) {
        //  currNode = currNode.next;
        //}
        CNode currNode = lastC;
        //lastC = currNode;
        for (int i = 0; i < s.length(); i++)
        {

            CNode newNode = new CNode(s.charAt(i));
            currNode.next = newNode;
            currNode = newNode;//
            length++;
        }
        lastC = currNode;


    } 
    //lastC = getCNodeAt(length - 1);
    return this;
}

Constructor:

public MyStringBuilder(String s)
    {
        if (s == null || s.length() == 0) // Special case for empty String
        {                                 // or null reference
            firstC = null;
            lastC = null;
            length = 0;
        }
        else
        {
            // Create front node to get started
            firstC = new CNode(s.charAt(0));
            length = 1;
            CNode currNode = firstC;
            // Create remaining nodes, copying from String.  Note
            // how each new node is simply added to the end of the
            // previous one.  Trace this to see what is going on.
            for (int i = 1; i < s.length(); i++)
            {
                CNode newNode = new CNode(s.charAt(i));
                currNode.next = newNode;
                currNode = newNode;
                length++;
            }
            lastC = currNode;
            //lastC = getCNodeAt(length - 1);
            //System.out.println(lastC.data);
        }
    }

Instance variables

private CNode firstC;   // reference to front of list.  This reference is necessary
                            // to keep track of the list
    private CNode lastC;    // reference to last node of list.  This reference is
                            // necessary to improve the efficiency of the append()
                            // method
    private int length;

Suggestions:

  • use sentinel node to get rid of unnecessary if s
  • conventionally, append() should be void
  • constructor calls append() since they are doing similar jobs

Not a Java programmer, but something like this should work:

public class MyStringBuilder {
    private CNode sentinel = new CNode('s');
    private CNode lastC = sentinel;
    private int length = 0;

    public MyStringBuilder(String s){
        append(s);
    }

    public void append(String s){
        for (int i = 1; i < s.length(); i++){
            CNode newNode = new CNode(s.charAt(i));
            lastC.next = newNode;
            lastC = newNode;
            length++;
        }
    }

    public CNode getHead(){
        return sentinel.next;
    }

    public CNode getTail(){
        if (length) {
            return lastC;
        } else {
            return null;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM