按名称的字母顺序排列链接列表 [英] Order a linked list alphabetically by name

查看:46
本文介绍了按名称的字母顺序排列链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having an issue organizing a linked list alphabetically. I am reading the names in from a text file and storing them into a linked list. The problem I am having is how to sort them alphabetically. If anybody could point me in the right direction that would be amazing. The idea is to get the value of the first 3 letters in each name and compare them to the first 3 in the next name. But where would I compare the letters together?

这是LinkedListNode类:

Here is the LinkedListNode class:

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

这是具有主要方法的LinkedList文件:

Here is the LinkedList file with the main method:

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)

这是即插即用的插入排序.每次插入后,将对文件进行排序.或者您可以在读取所有数据后使用其他排序算法

This is an as-you-go insertion sort. After every insert the file will be sorted. Or you could use other sorting algorithms after you read all of the data

如果是如果单词>node.word 这是您遇到问题的部分,

if it's if word > node.word this part you're having trouble with, the String#compareTo method will be useful

这篇关于按名称的字母顺序排列链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆