前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在链表上实现单词统计

在链表上实现单词统计

作者头像
张凝可
发布2019-08-22 11:02:53
5640
发布2019-08-22 11:02:53
举报
文章被收录于专栏:技术圈

结点为WordNode,有两个域,分别是结点上存储的单词,结点出现的频度以及这个单词下一个出现的单词

代码语言:javascript
复制
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());
	}
	

}

统计

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
结点单词为:word,单词频度为:2
结点单词为:count,单词频度为:2
结点单词为:another,单词频度为:2
结点单词为:link,单词频度为:2
结点单词为:sun,单词频度为:1
结点单词为:yellow,单词频度为:1
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年08月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档