在链表上实现单词统计
2016-08-21 14:37
309 查看
结点为WordNode,有两个域,分别是结点上存储的单词,结点出现的频度以及这个单词下一个出现的单词
文件内容为:word count another link count sun yellow another link word
输出:
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
相关文章推荐
- 数据库操作
- 算法导论第二章笔记
- cocos2d-x-3.0学习笔记之如何实现地图的卷动
- POJ 3311 Hie with the Pie
- c++安全的释放指针
- SLF4J: Class path contains multiple SLF4J bindings.问题解决方案
- 作用域
- Python R 线性回归 高斯回归 比较
- mvc
- Javascript发送AJAX请求实例代码
- spring框架
- Codeforces Round #367 (Div. 2) A. Beru-taxi
- IOS应用开发入门-(IOS项目包含的文件)
- nginx tcp代理模块安装
- Qt面试以及常用类继承关系图
- 《那些年 在美国》读书笔记-1
- mybatis 更新数据表、自动增长序列、
- h5学习笔记: 选项卡
- linux screen 命令详解
- Tmux