数据结构(严蔚敏)第四章 串 实例——索引表
来源:互联网 发布:云计算与hadoop 编辑:程序博客网 时间:2024/04/29 12:08
package index;import java.io.*;import java.util.*;class IdxTermType{ // 索引项类型 StringBuffer key = new StringBuffer(); // 关键词 LinkedList<StringBuffer> bnolist = new LinkedList<StringBuffer>(); // 存放书号索引的链表}class Ret{ boolean b = false; int lct = 0;}//class IdxListType extends define{ // 索引表类型(有序表)��// IdxTermType item[] = new IdxTermType[9]; // 包含索引项�// int last; // 索引表长度//}//class WordListType extends LinkedList{ // 词表类型(顺序表),存放一本书的书名中若干关键词// String item; //}public class index extends define{ // static IdxListType idxlist = new IdxListType(); // 定义索引表 public static LinkedList<IdxTermType> idxlist = new LinkedList<IdxTermType>(); public static void ExtractKeyWord( LinkedList<String> wdlist,String line,StringBuffer BookNo){ // 从 line 中提取关键词到词表,书号存入 BookNo String arrs[] = line.split(" "); // 将 line 以空格为断点分为数个子字符串(将句子转化为多个单词) for(int i=0; i<arrs.length; i++){ int ucount = 0; for(int j=0; j<usual.length; j++) if((arrs[i].equalsIgnoreCase(usual[j]))) // 如果 arrs[i] 不属于常用词汇 ucount ++; if(ucount == 0){ for(int k=0; k<arrs[i].length(); k++){ char c = arrs[i].charAt(k); if(c<'0' || c>'9'){ // 如果 arrs[i] 不是书号,则为关键词,插入词表wdlist,书号存入 BookNo if(!CheckRpt(wdlist,arrs[i])) wdlist.add(arrs[i]); // 插入词表 if(BookNo.length() == 0) // 如果书号为空,则存入 BookNo.append(arrs[0]); break; } } } } } public static boolean CheckRpt(LinkedList<String> wdlist,String wd){ //判断 词表 wdlist 中是否已有 wd,无返回为 false ,有返回为 true boolean jd = false; for(int i=0; i<wdlist.size();i++){ if(wd.equalsIgnoreCase(wdlist.get(i))) // 判断是否相等,不区分大小写 jd = true; else jd = false; } return jd; } public static void InsIdxList( LinkedList<String> wdlist,String line,StringBuffer BookNo){ String wd; Ret j = new Ret(); for(int i=0; i<wdlist.size(); ++i){ wd = wdlist.get(i); Locate(j,wd); if(!j.b) InsertNewKey(j.lct,wd); InsertBook(j.lct,BookNo); } } public static void InsertBook(int i,StringBuffer BookNo){ // 在索引表的第 i 项中插入书号为 BookNo 的索引 // 定义 j 为即将插入书号索引链表的位置 if(idxlist.get(i).bnolist.get(0).toString().equals(" ")) idxlist.get(i).bnolist.set(0, BookNo); else idxlist.get(i).bnolist.add(BookNo); } public static void InsertNewKey(int i,String wd){ // 在索引表 idxlist 的第 i 项上插入新关键词 wd,并初始化书号索引的链表为空 // if(idxlist.last == 0){ // 如果 idxlist 为空,则将 wd 直接插入// idxlist.item[0].key = wd;// idxlist.item[0].bnolist = null;// ++idxlist.last;// } StringBuffer bno = new StringBuffer(); bno.append(" "); IdxTermType elem = new IdxTermType(); // 定义即将插入的索引元素 elem.key.append(wd); elem.bnolist.add(bno); if(i == 0){ //idxlist.get(0).key = wd; //idxlist.get(i).key = wd; idxlist.add(elem); } else{// for(int j=idxlist.size()-1; j>=i; --j) { // 后移索引项 ?为什么后移索引项?// idxlist.get(j+1).key = idxlist.get(j).key;// //插入新的索引项// //idxlist.get(i).key = wd; // 将 wd 插入 idxlist 的第 i 项// //idxlist.get(i).bnolist = null; // 初始化书号索引表为空// idxlist.add(elem);// } idxlist.add(i, elem); } } // ??? public static void Locate(Ret j,String wd){ // 在索引表 idxlist 中查询是否存在与 wd 相等的关键词。若存在, // 则返回其在索引表中的位置,且 b 取值true,否则返回插入位置, // 且 b 取值false // 定义函数的返回值// for(int i=idxlist.size()-1;i>=0;--i){ // 将索引表中的关键词与 wd 按字典顺序比较// int m = idxlist.get(i).key.toString().compareTo(wd); // if(m == 0){ // 如果索引表中已存在 wd// b = true;// lct = i;// }// else{ // 否则// b = false;// lct = i+1;// }// } for(int i=0; i<idxlist.size(); i++){ int m = idxlist.get(i).key.toString().compareTo(wd); if(m == 0){ j.b = true; j.lct = i; break; } else{ j.b = false; j.lct = i+1; } } } public static void main(String[] args) throws IOException{ int c1;int c2; try{ FileInputStream fin1 = new FileInputStream("Book/Bookinfo.txt"); // 定义 Bookinfo 输入流对象 FileInputStream fin2 = new FileInputStream("Book/BookIdx.txt"); // 定义 BookIdx 输入流对象 // FileOutputStream fout = new FileOutputStream("Book/BookIdx.txt"); //定义 BookIdx 输出流对象 FileReader fr = new FileReader("Book/Bookinfo.txt"); // 定义 Bookinfo 读取对象 FileWriter fw = new FileWriter("Book/BookIdx.txt"); // 定义 BookIdx 写入对象 BufferedReader br = new BufferedReader(fr); // 读取 Bookinfo 的缓冲区 BufferedWriter bw = new BufferedWriter(fw); // 写入 BookIdx 的缓冲区 PrintWriter out = new PrintWriter(bw); // 格式化写入过程 String line = ""; // 从 Bookinfo 中读取的一行书目 while((line=br.readLine())!=null){ LinkedList<String> wdlist = new LinkedList<String>(); // 词表 StringBuffer BookNo = new StringBuffer(); // 书号 ExtractKeyWord(wdlist,line,BookNo); // 从 line 中提取关键词到词表,书号存入 BookNo InsIdxList(wdlist,line,BookNo); // 将书号为 BookNo 的关键词插入索引表 //out.println(idxlist); // 将生成的索引表 idxlist 输出到文件 BookIdx } while((c1=fin1.read())!=-1){ // 测试 Bookinfo 内容 System.out.print((char)c1); } fin1.close(); // 关闭输入流 String s1[] = new String[10]; String s2[][] = new String[10][10]; for(int i=0; i<idxlist.size(); i++){ s1[i] = idxlist.get(i).key.toString(); for(int j=0; j<idxlist.get(i).bnolist.size(); j++) s2[i][j] = idxlist.get(i).bnolist.get(j).toString(); } for(int i=0; i<10; i++){ if(s1[i] != null) out.print(s1[i]+" "); for(int j=0; j<10; j++) if(s2[i][j] != null) out.print(s2[i][j]+" "); out.println(); }// for(ListIterator it1 = idxlist.listIterator();it1.hasNext();){ // for(int i=)// s = (String) it1.next(); // out.println(s); // } out.close(); while((c2=fin2.read())!=-1){ //测试 BookIdx 内容 System.out.print((char)c2); } fin2.close(); }catch (FileNotFoundException e){System.err.println(e);} catch (IOException e){System.err.println(e);} }}
0 0
- 数据结构(严蔚敏)第四章 串 实例——索引表
- 数据结构之串—关键词索引表
- 数据结构第四章--串
- 数据结构 第四章 串
- 数据结构(C++版)笔记整理——第四章
- 数据结构复习——第四章:串、多维数组和广义表
- 第四章(5).建立词索引表
- 第四章-字典当索引不好用时:——python基础教程(第二版)笔记
- MongoDB——第四天 索引操作
- MongoDB——第四天 索引操作
- 第四章 索引构建
- 数据结构 第四章 串、数组和广义表
- 数据结构笔记——第四章字符串和多维数组
- 大话数据结构第四章——栈与队列
- 数据结构——c语言描述 第四章 字符串
- 《数据结构教程(第四版)》读书笔记-第二章 线性表
- 数据结构——第四讲、树(中)(1)
- 数据结构——第四讲、树(中)(2)
- Java之------socket系列(一)
- Java中静态变量与静态方法的继承
- CPU的前世今生
- android讲义 2016/5/8
- php常用函数-数学
- 数据结构(严蔚敏)第四章 串 实例——索引表
- java中sleep和wait的区别
- 提高篇项目1(1)——函数版星号图
- jQuery-DOM节点的删除
- 在使用xutils时post请求传递中文到服务端Mysql数据库出现中文乱码。
- 语法篇2之运算符
- Android SlidingMenu 环境配置
- c++第5次作业
- 在wamp环境中进入phpMyAdmin的报错处理方法