是的,这是一个家庭作业项目。 也就是说,我希望从中吸取教训,而不仅仅是让别人替我完成。
我的项目是一个词频列表 - 我接受一个文本文件(或网站URL)并计算:
-唯一单词的数量, 以及
-它们出现的次数。
所有方法都已经提供给我了,除了一个:insert(E word)
方法,其中参数是泛型类型的单词。
该单词存储在一个节点中 (链表项目),该节点还有一个'count'值,该值表示该单词在所读取的文本中出现的次数。
这个方法需要做的事情如下:
- 如果参数已经在列表中,则增加该元素的计数。 我已经完成了这部分
- 如果在列表中找不到该参数,则将其附加到列表中。 我也已经完成了这部分。
- 对列表按递减计数值排序。即:最高->最低计数 3.5 如果两个元素具有相同的计数值,则按其单词的字典顺序进行排序
我对链表非常不熟悉,因此遇到了很多NullPointerExceptions
。
public void insert(E word){
if(word.equals("")){
return;
}
if(first == null){//if list is null (no elements)
/*Node item = new Node(word);
first = item;*/
first = new Node(word);
}
else{//first != null
Node itemToAdd = new Node(word);
boolean inList = false;
for(Node x = first; x != null; x=x.next){
if (x.key.equals(word)){// if word is found in list
x.count++;//incr
inList = true;//found in list
break;//get out of for
}//end IF
if(x.next == null && inList == false){//if end of list && not found
x.next = itemToAdd;//add to end of list
break;
}//end IF
}//end FOR
//EVERYTHING ABOVE THIS LINE WORKS.
if (!isSorted()){
countSort();
}
}//end ELSE
}//end method
我的isSorted()
方法:
public boolean isSorted(){
for(Node copy = first; copy.next != null; copy = copy.next){
if (copy.count < copy.next.count){
return false;
}
}
return true;
}
最后但并非不重要的部分,我正在苦苦挣扎的是排序方法:
public void countSort(){
for (Node x = first, p = x.next; p != null; x=x.next, p=p.next){
// x will start at the first Node, P will always be 1 node ahead of X.
if(x == first && (x.count < p.count)){
Node oldfirst = first;
x.next = p.next;
first = p;
first.next = oldfirst;
break;
}
if (x.count < p.count){
//copy.next == x.
Node oldfirst = first;
oldfirst.next = first.next;
x.next = p.next;
first = p;
first.next = oldfirst;
break;
}
if (x.count == p.count){
if(x.toString().charAt(0) < p.toString().charAt(0)){
//[x]->[p]->[q]
Node oldfirst = first;
x.next = p.next;
first = p;
first.next = oldfirst;
break;
}
}
}
}
当被赋予给我的类/方法调用时,以下是我的插入方法的输出:
Elapsed time:0.084
(the,60)
(of,49)
(a,39)
(is,46)
(to,36)
(and,31)
(can,9)
(in,19)
(more,7)
(thing,7)
(violent,3)
(things,3)
(from,9)
(collected,1)
(quotes,1)
(albert,1)
(einstein,2)
(any,2)
(intelligent,1)
(fool,1)
(make,1)
(bigger,1)
(complex,1)
(it,11)
(takes,1)
(touch,1)
(genius,1)
(lot,1)
(courage,1)
(move,1)
(opposite,1)
(direction,1)
(imagination,1)
(important,5)
(than,3)
(knowledge,3)
(gravitation,1)
(not,17)
(responsible,1)
(for,14)
(people,2)
(falling,1)
(love,2)
(i,13)
(want,1)
(know,3)
(god,4)
(s,8)
(thoughts,2)
(rest,2)
(are,11)
(details,2)
(hardest,1)
(world,7)
(understand,3)
(income,1)
(tax,1)
(reality,3)
(merely,1)
(an,7)
(illusion,2)
(albeit,1)
(very,3)
(persistent,2)
(one,12)
(only,7)
(real,1)
(valuable,1)
(intuition,1)
(person,1)
(starts,1)
(live,2)
(when,3)
(he,11)
(outside,1)
(himself,4)
(am,1)
(convinced,1)
(that,14)
(does,5)
(play,2)
(dice,1)
(subtle,1)
(but,8)
(malicious,1)
(weakness,2)
(attitude,1)
(becomes,1)
(character,1)
(never,3)
(think,1)
(future,2)
(comes,1)
(soon,1)
(enough,1)
(eternal,1)
(mystery,1)
(its,4)
(comprehensibility,1)
(sometimes,1)
我的初始想法是尝试循环运行
if(!isSorted()){ countSort();}
部分,直到排序完成,但是当我这样做时,似乎会陷入无限循环。我尝试按照我的教授的讲座笔记操作,但不幸的是,他两次发布了上一次讲座的笔记,所以我感到困惑。我不确定是否值得一提,但他们为我提供了一个带有
hasNext()
和next()
方法的迭代器 - 我该如何使用它?我无法想象如果它没有用处,他们会提供它。我哪里做错了?