简体   繁体   中英

How to sort a generic Linked List

The goal of this program is to read in any datatype from a .txt file into a Doubly Linked List, sort the data using bubble-sort, then print out the sorted data.

I can't seem to figure out why my compareTo function will sort Strings just fine, but when I try to sort int, double, or float the compareTo will only sort by the most significant digit. Here is my code written in Java along with my .txt file contents and program output.

Please I need all suggestions or answers I can get, because I am stumped!

Main tester class:

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

Doubly Linked List class

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

Simple Node class

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

Here is the contents of my .txt file

12
123
321
2345
25
5423
2345
2
4

Lastly, here is the output from my program:

12
123
2
2345
2345
25
321
4 
5423

I dont know if it's ok to do this. First try to parse float, then int, and then String (At least i could sort as u wanted)

Please, correct me if i'm wrong.

FlowChart:

的FlowChart

bubbleSort:

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

Output:

1 1 1 1 1 1 2 3.3 4 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM