如何在Java中的排序链表中插入字符串?

时间:2018-11-12 14:17:48

标签: java

我正在尝试创建一个排序的链表。 addElement和removeElement方法几乎可以正常工作,只是addElement方法并不总是按正确的顺序插入元素。

到目前为止,这是我的代码:

public class SortedLinkedList {

// Node class
class Node {
    public String data;
    public Node next;
}

// attributes
private Node first;
private int size;

public SortedLinkedList() {
    first = null;
}

public void addElement(String element) {
    if (first == null) {
        Node newNode = new Node();
        newNode.data = element;
        newNode.next = null;
        first = newNode;

        size++;

    } else if (first.toString().compareTo(element) > 0) {
        Node newNode = new Node();
        newNode.data = element;
        newNode.next = first;
        first = newNode;

        size++;

    } else {
        Node before = first;
        Node after = first.next;

        while (first.next != null) {
            Node newNode = new Node();
            newNode.data = element;

            if (after.toString().compareTo(element) > 0) {

                before.next = newNode;
                newNode.next = after;
            }

            before = after;
            after = after.next;
        }

        size++;
    }
}

public boolean removeElement(String element) {
    boolean removed = false;

    try {
        if (first.data.toString().equals(element)) {
            first = first.next;
            removed = true;

        } else {
            Node before = first;
            Node after = first.next;

            while (first.next != null) {
                if (after.data.toString().compareTo(element) == 0) {

                    before.next = after.next;
                    break;
                }

                before = after;
                after = after.next;
            }

            size--;
            removed = true;
        }
    } catch (NullPointerException e) {
        removed = false;
    }
    return removed;
}

public int countElements() {
    return size;
}

@Override
public String toString() {

String result = "{";
Node current = this.first;

while (current != null) {
    if (current.next != null) {
        result = result + current.data.toString() + ", ";
    } else {
        result = result + current.data.toString();
    }
    current = current.next;
}

result = result + "}";

return result;
}

例如,我尝试插入值:“ b,a,d,c”,输出为:“ c,d,a,b”,但应为“ a,b,c,d” 。 我真的看不到我在做什么错。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您遇到各种小问题。

  1. 您正在比较Node而不是Node的数据。
  2. 您的while循环正在循环while (first.next != null)。这不是应该检查before还是after吗?
  3. 为什么要在Node循环中创建新的while

您可以这样更改Node

class Node implements Comparable<Node> {
    private final String data; //once initialised shouldn't change
    private Node next; //this can change

    public Node(String data) {
       this.data = data;
    }

    public Node getNext() {
      return next;
    }

    public void setNext(Node next) {
      this.next = next;
    }

    public int  compareTo(Node that) {
      return this.data.compareTo(that.data);
    }
}

在您的addElement(String element)条件之前,我会立即执行以下操作,完全一样:

if

在最后一种情况下,您的while循环可以是:

Node newNode = new Node(element);
size++;

您可能还可以包括将新节点也插入列表顶部的情况……但我留给您。

答案 1 :(得分:0)

如果要排序列表,可以使用普通的LinkedList库并在其上调用List#sort(Comparator)

例如,如果我们要按字母顺序对字符串进行排序:

LinkedList<String> list = new LinkedList<>();
list.addAll(Arrays.asList("b","d","c","a"));
System.out.println(list);  // ["b","d","c","a"]
list.sort(new Comparator<String>() {
    //This sorts the values by unicode value. In most cases, this results in sorting by alphabetical order
    @Override
    public int compare(String s1, String s2){
        return s1.compareToIgnoreCase(s2);
    }
});
System.out.println(list);  // ["a","b","c","d"]