【问题标题】:How to sort a generic Linked List如何对通用链表进行排序
【发布时间】:2017-03-26 09:38:47
【问题描述】:

此程序的目标是将任何数据类型从 .txt 文件读入双向链表,使用冒泡排序对数据进行排序,然后打印出排序后的数据。

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

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

主要测试类:

package test1;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        DoublyLinkedList<Integer> list = new DoublyLinkedList<>();
        list.readData("Test.txt");
        list.bubbleSort();
        list.printList();   
    }
}

双向链表类

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

这是我的 .txt 文件的内容

12
123
321
2345
25
5423
2345
2
4

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

12
123
2
2345
2345
25
321
4 
5423

【问题讨论】:

  • @SuppressWarnings("unchecked") - 是的,不要那样做。 Java 告诉你你做错了,你告诉 Java 保持安静而不是修复问题。 line = (E)buff.readLine() - 实际上并没有像你想象的那样做。
  • 不能只将字符串强制转换为整数,必须使用Integer.parseInt,否则会继续使用字符串的compareTo,因为值偷偷是字符串.
  • 还需要加上buff.close()或者使用try-with-resources,否则会出现内存泄漏,并且可能会出现无法编辑的文件。

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


【解决方案1】:

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

如果我错了,请纠正我。

流程图:

冒泡排序:

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

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2013-10-31
    • 2010-10-20
    相关资源
    最近更新 更多