我有课:
public class WordNode {
private String _word;
private WordNode _next;
....
}
以及以下列表:
public class TextList {
private WordNode _head;
public char mostFrequentStartingLetter(....){}
}
在TextList类中,我应该使用递归方法(mostFrequentStartingLetter),该方法返回列表中单词开头的最常用字母... 我不知道从哪里开始.....
请帮忙......
谢谢, Alona
只是你知道我不是在作弊:
public class TextList {
private WordNode _head;
public TextList(String text) {
String word = "";
WordNode tmp;
// After the split that in the array we are going over all the array
for (int i = 0; i < text.length(); i++) {
for (int j = 0; j < text.length(); j++) {
if (text.charAt(j) == ' ') {
word = text.substring(0, j);
text = text.substring(j + 1);
i = 0;
break;
} else if (j == text.length() - 1) {
word = text.substring(0, j + 1);
text = text.substring(j + 1);
break;
}
}
if (_head == null) {
tmp = new WordNode(word, null);
_head = tmp;
}
// if the word starts with a smalles letter then the head, make it
// the head
else if (_head.getWord().compareTo(word) > 0) {
tmp = new WordNode(word, _head);
_head = tmp;
} else {
WordNode current;
current = _head;
// go over all the nodes in the list and push the current word
// to the list in the right order
while (current.getNext() != null) {
if (current.getWord().compareTo(word) < 1
&& current.getNext().getWord().compareTo(word) > 0) {
tmp = new WordNode(word, current.getNext());
current.setNext(tmp);
break;
}
current = current.getNext();
}
// If the current was the tail, check that the word is bigger
// and then make it the tail.
if (current.getNext() == null
&& current.getWord().compareTo(word) < 1) {
tmp = new WordNode(word, null);
current.setNext(tmp);
}
}
}
}
public String mostFrequentWord() {
String frequentWord = _head.getWord();
WordNode current = _head;
int count = 0;
int max = 0;
while (current.getNext() != null) {
if (current.getWord().compareTo(current.getNext().getWord()) == 0) {
count++;
}
if (count > max) {
max = count; frequentWord = current.getWord();
}
current = current.getNext();
}
return frequentWord;
}
public String toString() {
String s = "";
WordNode current = _head;
int count = 1;
while (current != null) {
while (current.getNext() != null && current.getWord().equals(current.getNext().getWord())) {
count++;
current = current.getNext();
}
s += current.getWord() + "\t" + count + "\n";
count = 1;
current = current.getNext();
}
return s;
}
public char mostFrequentStartingLetter(....){}
}
答案 0 :(得分:2)
由于这是作业,我只会给你一些提示。
你需要做两件事。
当你有递归函数时,你需要一个停止条件。考虑链表的停止条件。你怎么知道什么时候到达链表的末尾? _next
的价值是什么?
你的方法最终会看起来像这样:
///The pieces in the angle brackets are for you to figure out.
public void determineStartingLetter(WordNode currentNode) {
if(!<stopping condition>) {
determineStartingLetter(<next node after currentNode>);
}
}
现在这只会遍历链表。您还需要跟踪到目前为止您看到的起始字符。想想你可以用来做那个的结构。您希望将字符映射到您看过它的次数。什么数据结构可以帮到你?
现在你在哪里可以维持这样的结构?最简单的解决方案(但不是最易维护或最优雅的)将是TextList
类的私有成员。但还有更好的方法。如果您可以简单地将此数据结构传递到递归方法,然后传递到每个递归调用,该怎么办?
那么你的方法看起来像这样:
//As before, the things in angle brackets are for you to figure out.
public <data structure> determineStartingLetter(WordNode currentNode, <data structure>) {
if(!stopping condition>) {
<look at starting letter for currentNode>
<increment the count for this letter in the data structure>
return determineStartingLetter(<next node after currentNode>, <data structure>);
}
return <data structure>
}
这应该给你足够的暗示来弄清楚如何去做。在第二部分中,我实际上给了你一些比我应该提供的更多的提示:)。