Blueyu
Blueyu

Reputation: 91

Failed to skip duplicated data in a sorted linked list in toString method

First things first, I am not allowed to use the Java API for linked list, the Linked List and List Node codes are given by my teacher, so it may or may not be the same as the stuff you see in the API.

Secondly, I will have to read from a file and input the contents of the file into the linked list.

With those out of the way, I would like to skip duplicated data (Not deleting) and return it as a string with a method called toString.

My thought process is this:

I start from the head (current) and I check the data of each subsequent list node one by one (checker). If the data is the same, go to the next node, otherwise go to the data that has a different data and append it to the string.

The code below is what I am trying to do.

public String toString() {
        ListNode current = head;
        ListNode checker = current.next;
        String s = "\nIdentifiers found:\n" + current.data;
        iterateLL:
        while (current.next != null) {
            // start from head, check data one by one until the data is not the same,
            // then go to that data and input it into String s
            checkRepeated:
            while(checker != null){ // still does not work
                if(checker.data == current.data){
                    if(checker.next == null){
                        break iterateLL;
                    }
                    else{
                        checker = checker.next;
                    continue checkRepeated;
                    }
                }
                else{
                    current = checker;
                    checker = current.next;
                    s += "\n" + current.data;
                    break checkRepeated;
                }
            }
        }
        return s;
    }

The original toString method given by my teacher:

public String toString2() { // original toString method, will not be changed, for backup
        String s = "[ ";
        ListNode current = head;
        while (current != null) {
            s += current.data + " ";
            current = current.next;
        }
        return s + "]";
    }

Assuming the text file (test.txt) is like this: (5 As, 5 Bs and 5 Cs) (1 letter for each line)

a
b
c
a
b
c
a
b
c
a
b
c
a
b
c

And thus the sorted linked list is this:

a a a a a b b b b b c c c c c

What I want to output is this:

a
b
c

But it always outputted the entire linked list without skipping duplicated data when using my modified toString method:

a
a
a
a
a
b
b
b
b
b
c
c
c
c
c

If I do not use a text file and insert the values into a Linked List directly, it works(Code below):

public class Test {
    public static void main(String[] args) {
        Comparator c = new StringComparator();
        LinkedList lls = new LinkedList(c);

        lls.insertInOrder("a");
        lls.insertInOrder("b");
        lls.insertInOrder("c");
        lls.insertInOrder("a");
        lls.insertInOrder("b");
        lls.insertInOrder("c");
        lls.insertInOrder("a");
        lls.insertInOrder("b");
        lls.insertInOrder("c");
        lls.insertInOrder("a");
        lls.insertInOrder("b");
        lls.insertInOrder("c");
        lls.insertInOrder("a");
        lls.insertInOrder("b");
        lls.insertInOrder("c");
        lls.insertInOrder("d");

        System.out.println(lls.toString());
    }
}

Output:

a
b
c
d

I don't know what is causing it to not skip duplicated data if it is read from a text file instead of inputting it directly

Upvotes: 0

Views: 21

Answers (0)

Related Questions