如何对通用链接列表进行排序

时间:2016-11-12 06:20:14

标签: java sorting generics bubble-sort doubly-linked-list

该程序的目标是将.txt文件中的任何数据类型读入双链表,使用bubble-sort对数据进行排序,然后打印出已排序的数据。

我似乎无法弄清楚为什么我的compareTo函数会对字符串排序很好,但是当我尝试对int,double或float进行排序时,compareTo将只按最高位数排序。这是我用Java编写的代码以及我的.txt文件内容和程序输出。

我需要我能得到的所有建议或答案,因为我很难过!

主要测试员类:

package test1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class DoublyLinkedList<E extends Comparable<E>>{

private Node<E> head;
private Node<E> tail;
private int size;

public DoublyLinkedList(){
    size = 0;
    head = new Node<E>(null, null, null);
    tail = new Node<E>(head, null, null);
    head.setNext(tail);
}

public void add(E e){
    Node<E> node = new Node<E>(tail.getPrevious(),e, tail);
    tail.getPrevious().setNext(node);
    tail.setPrevious(node);
    size++;
}
@SuppressWarnings("unchecked")
public void readData(String filename) throws IOException{
    E line = null;
    @SuppressWarnings("resource")
    BufferedReader buff = new BufferedReader(new FileReader(filename));
    while((line = (E)buff.readLine()) != null) {
        add(line);

    }
}
public void printList(){
    int i=0;
    Node<E> curr = head.getNext();
    while(i<size){
        System.out.println(curr.getData());
        curr = curr.getNext();
        i++;
    }
}
public void bubbleSort(){
    boolean sorted = false;
    int i=0;
    Node<E> curr;
    while(!sorted){
        sorted = true;
        curr = head.getNext();
        i=0;
        while(i<size-1){
            if(curr.getData().compareTo(curr.getNext().getData()) > 0){
                sorted = false;
                swap(curr);
            }
            curr = curr.getNext();
            i++;
        }
    }   
}
public void swap(Node<E> curr){
    E temp = curr.getData();
    curr.setData(curr.getNext().getData());
    curr.getNext().setData(temp);
}
}

双重链接列表类

package test1;

public class Node<E> {

private Node<E> next;
private Node<E> previous;
private E e;

public Node(){
    this.next = null;
    this.previous = null;
    this.e = null;
}
public Node(E e){
    this.e = e;
    this.next = null;
    this.previous = null;
}
public Node(E e, Node<E> next){
    this.next = next;
    this.e = e;
    this.previous = null;
}
public Node(Node<E> previous, E e, Node<E> next){
    this.next = next;
    this.e = e;
    this.previous = previous;
}
public void setNext(Node<E> next){
    this.next = next;
}
public void setData(E e){
    this.e = e;
}
public void setPrevious(Node<E> previous){
    this.previous = previous;
}
public Node<E> getNext(){
    return next;
}
public E getData(){
    return e;
}
public Node<E> getPrevious(){
    return previous;
}
}

简单节点类

12
123
321
2345
25
5423
2345
2
4

以下是我的.txt文件的内容

12
123
2
2345
2345
25
321
4 
5423

最后,这是我的程序的输出:

innodb_flush_log_at_trx_commit = 0

1 个答案:

答案 0 :(得分:0)

我不知道它是否确定这样做。首先尝试解析float,然后是int,然后是String(至少我可以按你的意愿排序)

如果我错了,请纠正我。

流程图:

FlowChart

冒泡:

try
{
    try
    {       
        float intCurr = Float.parseFloat ( curr.getData ( ).toString ( ) );

        if(intCurr - (Float.parseFloat ( curr.getNext().getData().toString ( ) )) > 0){
            sorted = false;
            swap(curr);
        }
        curr = curr.getNext();
        i++;
    }
    catch ( Exception e )
    {
        int intCurr = Integer.parseInt ( curr.getData ( ).toString ( ) );

        if(intCurr - (Integer.parseInt ( curr.getNext().getData().toString ( ) )) > 0){
            sorted = false;
            swap(curr);
        }
        curr = curr.getNext();
        i++;
    }

}
catch ( Exception e )
{
    if(curr.getData().compareTo(curr.getNext().getData()) > 0){
        sorted = false;
        swap(curr);
    }
    curr = curr.getNext();
    i++;
}

Test.txt的

  

2 15 1   3.3 10 5 6 4 1 1 1 1 1

输出:

  

1 1 1 1 1 1 2   3.3 4 5