简体   繁体   English

按字母顺序将新对象按顺序插入到(字符串的)Linkedlist中

[英]Inserting new object onto Linkedlist(of Strings) in alphabetical order without sorting

I'm wondering if this is possible in Java. 我想知道在Java中是否可行。 I want to insert it into the correct spot alphabetically. 我想按字母顺序将其插入正确的位置。 For example is the the LinkedList's (let's say it's called coollist) elements were : [Dusty, Gordon, Mayer, Popovic, Zechariah] and I try to insert another String by doing: 例如,LinkedList的元素(被称为coollist)是:[Dusty,Gordon,Mayer,Popovic,Zechariah],我尝试通过执行以下操作插入另一个String:

    coollist.add(d,Nyugen); //d is a a variable representing ant int which is the index

What can I do to make d the value that will insert it in alphabetical order, regardless of what's in the LinkedList? 无论LinkedList中包含什么,我如何使d值按字母顺序插入? Can you guys help me out? 你们能帮我吗? I hope this makes sense. 我希望这是有道理的。

You can iterate though the list, searching for when the index produces a string that is greater than the argument. 您可以遍历列表,搜索索引何时生成大于参数的字符串。 Then just insert behind that index. 然后只需在该索引后面插入即可。 If this is a one-way linked list, you'll have to keep track of the previous node so you can update its fields. 如果这是一个单向链接列表,则必须跟踪上一个节点,以便可以更新其字段。

    Node newNode = new Node( stringToBeAdded ); //Create new node

    if ( this.head == null ){ //list is empty, just insert
      this.head = newNode; //Initialize head
    }

    else{

      Node cur = this.head; //Start at the beginning of the list
      Node prev = this.head; //just initialize the previous node to something

      //keep going until found or at end of list
      while( (stringToBeAdded < cur.data) && (cur != null) ){ 
        prev = cur;
        cur = cur.next;
      }

      prev.next = newNode;

      if ( cur != null ){ //if we did not reach the end
        newNode.next = cur; //current Node is alphabetically greater
      }
    }

Following is one way to find the sorted index in LinkedList. 以下是在LinkedList中找到排序索引的一种方法。

import java.util.*;

public class SortedLinkedListDemo {

public static void main (String [] args) {
    List<String> list = new LinkedList<String> ();
    list.add ("Dusty");
    list.add ("Gordon");
    list.add ("Mayer");
    list.add ("Popovic");
    list.add ("Zechariah");

    list.add (getSortedIndex ("Nyugen", list), "Nyugen");

    System.out.println ("List: "+list);
}

private static int getSortedIndex (String name, List<String> list) {
    for (int i=0; i < list.size(); i++) {
        if (name.compareTo(list.get(i)) < 0) {
            return i;
        }
    }       
    // name should be inserted at end.
    return list.size();
}

} }

This will give the following output: 这将给出以下输出:

List: [Dusty, Gordon, Mayer, Nyugen, Popovic, Zechariah] 名单:[达西,哥顿,梅耶,纽根,波波维奇,撒迦利亚]

Searching a linked list takes O(n). 搜索链接列表需要O(n)。 But since your data is sorted, putting the next string in place is a matter of finding the right location. 但是,由于对数据进行了排序,因此将下一个字符串放在适当位置只是找到正确位置的问题。 In another data structure backed by an array, this is done by binary search and takes O(log n). 在由数组支持的另一个数据结构中,这是通过二进制搜索完成的,并采用O(log n)。 See lreeder's link in the comments. 请参阅注释中的lreeder链接。 Of course you can always look through the list yourself and insert the string, but that's not what a linked list is best at. 当然,您总是可以自己浏览列表并插入字符串,但这并不是链接列表最擅长的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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