簡體   English   中英

按名稱按字母順序排列鏈表

[英]Order a linked list alphabetically by name

我按字母順序組織鏈表有問題。 我正在從文本文件中讀取名稱並將它們存儲到鏈接列表中。 我遇到的問題是如何按字母順序排序。 如果有人能指出我正確的方向,這將是驚人的。 我們的想法是獲取每個名稱中前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;
    }
}

一些偽代碼給你:

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方法將非常有用

要進行簡單的比較,您的節點應該實現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();

        }

    }
}

嘗試使用Collections.sort(列表)

此外,為了進行比較,您可以在Comparable Interface下使用compareTo函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM