如何使用Java按字母顺序对链表进行排序?

时间:2016-11-19 09:28:20

标签: java sorting linked-list

我正在做一个图书馆库存系统,所以我应该把Node里面的名字按字母顺序排序。我有书名,作者,伊斯兰号,副本和流派,所有这些信息都存放在课堂上。

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

这是我的Linked List类包含insert和display方法:

 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;
}

这是我的Node类:

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;
}

这是我用来显示列表的Iterator类:

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.");        
}

}

谢谢。

3 个答案:

答案 0 :(得分: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.");
}

答案 1 :(得分:0)

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

您在开头插入新元素并尝试向前移动。在current循环结束时,第一个更大previous最后一个更小。到现在为止。

但之后,你永远不会使用Previous,并将新元素设置为你的元素。您需要在previouscurrent之间插入它。这样的事情:

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

    newData.next = current;

答案 2 :(得分:0)

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

<?php 
                foreach ($result_cat as $key => $cat) {


                    $cat_name='';
                    if($cat->category_parent_id!=0)
                    {
                        $cnt=0;
                        $cat_name='';
                        $r_cat=$cat->category_parent_id;


                        do{
                            $cnt++;
                            $qur='SELECT category_parent_id FROM #__virtuemart_category_categories WHERE id='.$r_cat;
                            $db->setQuery($qur);
                            $r_cat=$db->loadResult();

                        }while($r_cat!=0);


                        for($i=0;$i<$cnt;$i++)
                        {
                            $cat_name=$cat_name.'-';
                        }
                        $cat_name=$cat_name.$cat->category_name;
                    }
                    else
                    {
                        $cat_name=$cat->category_name;
                    }
                    $sel_c='';
                    if(in_array((string)$cat->id,$sel_cat_arr))
                    {
                        $sel_c='selected="true"';
                    }
        echo '<option   value=\"'.$cat->id.'">'.$cat_name.'</option>';

                ?>