【问题标题】:How to sort a linked list in alphabetical order using Java?如何使用Java按字母顺序对链表进行排序?
【发布时间】:2016-11-19 09:28:19
【问题描述】:

我正在做一个图书馆库存系统,所以我应该按字母顺序对节点内的名称进行排序。我有书名、作者、isbn 编号、副本数和流派,所有这些信息都存储在一个类中。

我确实为它编写了按字母顺序排序的代码,但它不起作用。 有人能告诉我我的代码有什么问题吗?

这是我的链接列表类包含插入和显示方法:

 public class LinkedList
{
Node node = new Node();
static Node head;

public LinkedList()
{
    head=null;
}

public Node getHead()
{
    return head;
}

public static void addNode(Data data)
{
     Node newNode = new Node(data, head);

    if (head == null) {
        head = newNode;
        newNode.setNext(null);
    } else {
        Node next = head;
        Node prev = next;
    do {
        if (data.name.compareTo(next.data.name) < 0) {
            break;
        }
        prev = next;
        next = next.getNext();
    } while (next != null);

    newNode.setNext(next);
    if (data.name.compareTo(next.data.name) < 0) {
        head = newNode;
    } else prev.setNext(newNode);
}
}

public static String displayNode()
{
    Node current = head;
    String output = "";
    while(current != null){       
        output+=current.data.toString();
        current = current.next;  
    }
    return output;
}

这是我的节点类:

public class Node 
{
Data data;
Node next;

public Node()
{
    next = null;
}

Node(Data data, Node next)
{
    this.data = data;
    this.next = next;
}

public Object getData()
{
    return data;
}

public Node getNext()
{
    return next;   
}

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

这是我的数据类:

public class Data {
LinkedList list;
String name;
String author;
int isbn;
int number;
String genre;

public Data(String name, String author, int isbn, int number, String genre)
{
    this.name = name;
    this.author = author;
    this.isbn = isbn;
    this.number = number;
    this.genre = genre;
}

public String toString()
{
    return("Book Name: "+name+"\nAuthor: "+author+"\nISBN Number: "+isbn+"\nNumber of Copies: "+number+"\nGenre: "+genre+"\n\n");
}

public String getName()
{
    return name;
}

这是我用来显示列表的迭代器类:

public class DisplayIterator
{
LinkedList list;
static Node current;
static Node newNode;

DisplayIterator(Node newNode)
{
    this.newNode = newNode;
    current = list.head;
}

public static boolean hasNext()
{
    if(current == null){
        return false;
    }
    else if (current.next == null){
        return false;
    }
    return true;
}

public static Node next()
{
    if(hasNext()){
        current = current.next;
    }
    return current;
}

public static void remove(){
    throw new UnsupportedOperationException("It is read-only.");        
}

}

谢谢。

【问题讨论】:

  • 您将 not 添加到列表顶部,没有任何顺序。您的 while 循环只是在链接列表中运行,直到它被排序。如果你想对链表进行排序,你应该在正确的位置添加新节点,而不是总是在顶部。但是,如果我可以问,你为什么不使用 Collection
  • 你为什么要重新发明轮子?使用 TreeSet 和 Comparable 或 Comparator 接口。它们的作用相同,但它们是 Java Lib 的一部分。
  • 他当然应该使用Collection,但我猜这个任务需要编写一个自定义的链表实现。
  • @TimBiegeleisen 是的,这个练习确实需要编写一个自定义链表实现。
  • 希望您的教授不了解 Google 或 Stackoverflow...

标签: java sorting linked-list


【解决方案1】:

以下代码实现了基于顺序的向链表中的插入。这当然假设列表已经排序。做出这种假设是安全的,因为在您的界面中将节点添加到链表的唯一方法是通过此方法。

public static void addNode(Data data) {
    Node newNode = new Node(data, head);
    if (head == null) {
        head = newNode;
        return;
    }
    Node current = head;
    while (current.next != null && data.name.compareTo(current.data.name) >= 0) {
        current = current.next;
    }
    if (current == head && data.name.compareTo(current.data.name) < 0) {
        newNode.next = head;
        head = newNode;
    }
    else {
        newNode.next = current.next;
        current.next = newNode;
    }
    JOptionPane.showMessageDialog(null,"Book Information has been added to the inventory.");
}

【讨论】:

  • 当我输入第二个数据时,它显示错误:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  • @TimBiegeleisen 我认为这不能处理只有一个项目的情况,而要插入的第二个项目插入到头部。
  • 不,仍然没有排序...我按顺序输入 d、a、c、b,然后它向我显示 d、a、c、b。我也使用迭代器实现来显示它。
  • @RafiduzzamanSonnet 我更新了我的答案以处理这种极端情况。这样做可能有更好的方法。
  • 它没有对我认为的第一个节点进行排序,我按顺序输入 d,a,c,b 它给了我 a,d,b,c ..
【解决方案2】:

我假设,您的Node 不是来自 java util LinkedList 的那个,对吧?你能提供它的实现吗?为什么它的构造函数是head

您在开头插入新元素并尝试向前遍历。在您的current 中的循环结束时是第一个较大的previous 最后一个较小的。到现在为止都是正确的。

但之后,您永远不会使用Previous,并将新元素设置为您的。您需要在previouscurrent 之间插入它。类似的东西:

    if (previous == null)
      head = newData;
    else
      previous.next = newData;

    newData.next = current;

【讨论】:

  • 我明白了。谢谢!
  • 不客气。但是作为存放已排序元素的结构,堆更合适。在 java 中,我会使用 PriorityQueue 并为您的数据实现一个比较器。效率更高。
【解决方案3】:

Java 有一个丰富的库,可以对任何数据集合进行排序

java.util.Collections.sort(YOURLIST_valiable)

【讨论】:

    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 2021-12-07
    • 2016-10-12
    • 2015-12-08
    • 1970-01-01
    • 2021-11-20
    • 2015-07-26
    相关资源
    最近更新 更多