您的位置:首页 > 其它

在链表上实现单词统计

2016-08-21 14:37 309 查看
结点为WordNode,有两个域,分别是结点上存储的单词,结点出现的频度以及这个单词下一个出现的单词

public class WordNode{
private int freq;
private String word;
private WordNode next;
public WordNode(String word){
this.word = word;
this.freq = 0;

}
public int getFreq() {
return freq;
}
public void setFreq(int freq) {
this.freq = freq;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
public WordNode getNext() {
return next;
}
public void setNext(WordNode next) {
this.next = next;
}

public  boolean equals(WordNode o) {

return this.getWord().equals(o.getWord());
}

}
统计

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;

public class WordLinkList {
String filePath;
private WordNode startNode;
ArrayList<String[]> wordList;
public WordLinkList(String filePath){
this.filePath = filePath;
readDataFile();

}
private void readDataFile() {
File file = new File(filePath);
wordList = new ArrayList<String[]>();
try{
BufferedReader in = new BufferedReader(new FileReader(file));
String str;
String temp[];
while((str=in.readLine())!=null){
temp = str.split(" ");
wordList.add(temp);

}
in.close();
}catch(IOException e){
e.printStackTrace();

}

}
//我要建立一个链表是不是需要,第一判断当前建的结点是否已经存在了,另外我还要判断要建的这个结点要健在什么地方

public void buildLinkList(){
//初始化一个链表,循环wordList,
this.startNode = new WordNode(wordList.get(0)[0]);
WordNode node = startNode;
WordNode tempNode;
for(String[] temp:wordList){
for(String str:temp){
//注意还要判断当前链表中是否已经存在
tempNode = new WordNode(str);
//生成节点之后在判断是有点不太好哈,直接判断单词是否存在

if(!isExit(tempNode)){
tempNode.setFreq(1);
node.setNext(tempNode);
node = tempNode;
}else{//查找该节点,并且域上+1;
tempNode =searchWordNode(str);
tempNode.setFreq(tempNode.getFreq()+1);

}

}

}
printLinkList();
}
//判断单词相同的结点是否存在
public boolean isExit(WordNode node){
boolean isExit = false;//默认是不存在的
//遍历当前序列
WordNode start = this.startNode;
WordNode tempNode =start;
while(tempNode!=null){
if(tempNode.equals(node)){
//找到
isExit = true;
break;

}
tempNode = tempNode.getNext();

}
return isExit;

}
public WordNode searchWordNode(String str){
//在链表中查找具有相同单词的结点,并且返回
WordNode start = this.startNode;
WordNode temp = start;
while(temp!=null){
if(temp.getWord().equals(str)){
return temp;

}
temp = temp.getNext();

}

return null;

}
//把这个链表打印出来
public void printLinkList(){
WordNode start = this.startNode;
WordNode temp = start;
while(temp!= null){
System.out.print(MessageFormat.format("结点单词为:{0},单词频度为:{1}",temp.getWord(),temp.getFreq()));
System.out.println();
temp = temp.getNext();
}

}

}


文件内容为:word count another link count sun yellow another link word

输出:

结点单词为:word,单词频度为:2
结点单词为:count,单词频度为:2
结点单词为:another,单词频度为:2
结点单词为:link,单词频度为:2
结点单词为:sun,单词频度为:1
结点单词为:yellow,单词频度为:1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: