数据结构(严蔚敏)第四章 串 实例——索引表
2016-05-08 15:55
489 查看
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(i 4000 nt 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);} } }
相关文章推荐
- 交换机升级排障实例
- PostgreSQL教程(八):索引详解
- C#数据结构之顺序表(SeqList)实例详解
- Oracle外键不加索引引起死锁示例
- oracle 索引的相关介绍(创建、简介、技巧、怎样查看) .
- Lua教程(七):数据结构详解
- 用SQL建立索引的方法步骤
- SQL2005重新生成索引的的存储过程 sp_rebuild
- SQL效率提升之一些SQL编写建议并有效利用索引
- SQLSERVER的非聚集索引结构深度理解
- SQL Server误区30日谈 第8天 有关对索引进行在线操作的误区
- SQL Server 索引介绍
- SqlServer 索引自动优化工具
- mysql 中存在null和空时创建唯一索引的方法
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- 详解sqlserver查询表索引
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 优化 SQL Server 索引的小技巧