我为字符串类型编写了一个单独排序的链表。 让我告诉你我该做什么。
要在已排序的链接列表中存储单词。 每个单词都有一些变化(或含义FYI),也需要存储在单独排序的链表中。
所以基本上链表中的每个单词都有链接的变体列表。
问题是如何连接它们。 我的意思是,删除这个词也会删除他们的变化。
任何帮助将不胜感激。 提前谢谢。
下面是LinkedList:
public class LinkedList {
private Node start = null;
private class Node{
private String value = null;
private Node next = null;
}
public void insert(){
// This method will loop from head and inserts in ascending order
}
}
// And other methods like delete etc...
以下是我要做的事情:
public class Demo {
public class Word {
private String stringWord;
private LinkedList variations;
}
private LinkedList Words;
注意:不允许使用任何API或集合
答案 0 :(得分:0)
您可以对LinkedList使用java泛型,如下所示
public class LinkedList<T> {
private Node start = null;
private class Node {
private T value = null;
private Node next = null;
}
}
// for string type
LinkedList<String> variations = new LinkedList<String>();
// for word type
LinkedList<Word> words = new LinkedList<Word>();
答案 1 :(得分:0)
因为你想要一个&#34;变种&#34;与主列表中每个单词相关联的列表,您需要将主列表作为Word
个对象的列表,而不仅仅是普通String
对象的列表。
假设您已经了解了泛型,这意味着您需要更改LinkedList
类以使用&#34;类型参数&#34;而不是硬编码String
,以便value
可以是Word
(对于主列表)或String
(对于变体列表)。
由于您的列表需要排序,因此您需要将type参数设置为Comparable
,因此您的代码将变为:
public class LinkedList<E extends Comparable<E>> {
private Node start = null;
private class Node{
private E value = null;
private Node next = null;
}
public void insert(E newValue){
// code here, e.g.
// node.getValue().compareTo(newValue)
}
}
public class Word implements Comparable<Word>{
private String stringWord;
private LinkedList<String> variations;
@Override
public int compareTo(Word that){
return this.stringWord.compareTo(that.stringWord);
}
}
// main list
LinkedList<Word> words;