按名称按字母顺序排列链表

时间:2013-11-04 20:39:58

标签: java linked-list nodes

我遇到按字母顺序组织链接列表的问题。我正在从文本文件中读取名称并将它们存储到链接列表中。我遇到的问题是如何按字母顺序排序。如果有人能指出我正确的方向,这将是惊人的。我们的想法是获取每个名称中前3个字母的值,并将它们与下一个名称中的前3个字母进行比较。但我会在哪里比较这些字母?

这是LinkedListNode类:

public class LinkedListNode
{

    private String data;
    private LinkedListNode next;


    public LinkedListNode(String data)
    {
        this.data = data;
        this.next = null;
    }

    public String getData()
    {
        return data;
    }

    public LinkedListNode getNext()
    {
        return next;
    }

    public void setNext(LinkedListNode n)
    {
        next = n;
    }
}

这是使用main方法的LinkedList文件:

import java.io.*;
import java.util.Scanner;

public class LinkedList {

    public LinkedListNode head;
    String fname;

    public static void main(String[] args) throws FileNotFoundException{
            Scanner scan = new Scanner(new File("Names.txt"));
            LinkedList l = new LinkedList();    
            int i = 1;
            while(scan.hasNext()) {
                String s = scan.nextLine();
                l.insertBack(s);
                i++;
            }
            System.out.print(l.showList());
        }


    public LinkedList() {
        this.head = null;
    }

    public void insertBack(String data){

        if(head == null){
            head = new LinkedListNode(data);
        }else{
            LinkedListNode newNode = new LinkedListNode(data);
            LinkedListNode current = head;
            while(current.getNext() != null){
                current = current.getNext();
            }
            current.setNext(newNode);
        }
    }

    public String showList(){
        int i = 0, j;
        String retStr = "List nodes:\n";
        LinkedListNode current = head;
        while(current != null){
            i++;
            retStr += "Node " + i + ": " + current.getData() + "\n";
            current = current.getNext();

        }

        return retStr;
    }
}

3 个答案:

答案 0 :(得分:2)

一些伪代码:

OUTER:
for word in file
    node = head
    while node.next
        if word > node.word
             node.next
        else
             Node temp = new Node(word)
             temp.next = word.next
             node.next = temp
             continue OUTER
    node.next = new Node(word)

这是一个即插即用的插入排序。每次插入后,文件都将被排序。或者,在阅读完所有数据后,您可以使用其他排序算法

如果if word > node.word这部分您遇到问题,那么String#compareTo方法会很有用

答案 1 :(得分:0)

要进行简单的比较,您的节点应该实现Comparable。基础Java库倾向于依赖于此以便于排序。

Comaprable接口将要求您实现compareTo(见下文)。

public int <LinkedListNode> compareTo(LinkedListNode n){
  //Case insensitively compare the first 3 characters of the two nodes
  String myHead = data.substring(0,3).toLowerCase();
  String comparableHead = n.data.substring(0,3).toLowerCase();

  return (myHead.compareTo(comparableHead));

}

如果使用像ArrayList这样的标准List结构,Collections.sort(列表)将能够使用此方法来排序列表。

这是一个基于插入排序的“插入”功能,用于你的runTime,使用这个可比较的。

public void insert(String data){
    LinkedListNode newNode = new LinkedListNode(data);
    if(head == null){
        head = newNode;
    }else{
        LinkedListNode current = head;
        LinkedListNode prev;

        //This is missing some key edge cases, but it inserts properly in the general case.  You'll have to add to it to handle things like running off the list, or this needing to be inserted before the head.
        while(current.getNext() != null){
            if(current.compareTo(newNode)<0){
              newNode.setNext(current);
              prev.setNext(newNode);
              break;
            }
            prev = current;
            current = current.getNext();

        }

    }
}

答案 2 :(得分:0)

尝试使用 Collections.sort(list)

另外,为了进行比较,您可以在Comparable Interface下使用 compareTo 函数

相关问题