数据结构C语言版 索引表.docx
- 文档编号:29541356
- 上传时间:2023-07-24
- 格式:DOCX
- 页数:21
- 大小:20.27KB
数据结构C语言版 索引表.docx
《数据结构C语言版 索引表.docx》由会员分享,可在线阅读,更多相关《数据结构C语言版 索引表.docx(21页珍藏版)》请在冰豆网上搜索。
数据结构C语言版索引表
数据结构C语言版索引表
P86-P87
编译环境:
Dev-C++4.9.9.2
日期:
2011年2月8日
说明一下,常用词表文件的数据,第一行是词表含有的常用词个数,然
后每一行一个词,都是以回车符结束的。
书目文件,每一行前面三个字符是书号,后面紧接着是书名,而没有空
格,每一行一本书,以回车符结束。
文件以一个空行结束。
注意是回车符结束,这个要用编辑器显示了回车符的才能够看到,我就
是不小心删了,然后一直出错的。
*/
#include
#include
#include
#include
typedefintElemType;
//串的堆分配存储
typedefstruct
{
char*ch;//若是非空串,则按串长分配存储区,否则ch为NULL
intlength;//串长度
}HString;
//带头结点的线性链表类型
typedefstructLNode//结点类型
{
ElemTypedata;
structLNode*next;
}LNode,*Link,*Position;
typedefstructLinkList//链表类型
{
Linkhead,tail;//分别指向线性链表中的头结点和最后一个结点
intlen;//指示线性链表中数据元素的个数
}LinkList;
//这里将有三个表,词表(一本书中的关键词),索引表,常用词表
#defineMaxKeyNum25//索引表的最大容量(关键词的最大数)
#defineMaxLineLen100//书目串(书名+书号)buf的最大长度
#defineMaxWordNum10//词表(一本书的关键词)的最大容量
#defineMaxNoIdx10//常用词(仅指大写)的最大数
typedefstruct_WordListType
{
char*item[MaxWordNum];//词表(字符串)指针数组
intlast;//词的数量
}WordListType;//词表类型(顺序表)
typedefstruct
{
HStringkey;//关键词(堆分配类型,HString.h)
LinkListbnolist;//存放书号索引的链表(LinkList.h)
}IdxTermType;//索引项类型
typedefstruct
{
IdxTermTypeitem[MaxKeyNum+1];
intlast;//关键词的个数
}IdxListType;//索引表类型(有序表)
typedefstruct
{
char*item[MaxNoIdx];//常用词表指针数组
intlast;//常用词的数量
}NoIdxType;//常用词表类型(有序表)
//全局变量
charbuf[MaxLineLen+1]={'\0'};//当前书目串(包括'\0')
WordListTypewdlist;//暂存一本书的词表
NoIdxTypenoidx;//常用词表
#defineMaxBookNum10//假设只对10本书建索引表
#defineMaxKeyNum25//索引表的最大容量(关键词的最大数)
#defineMaxLineLen100//书目串(书名+书号)buf的最大长度
typedefstruct
{
charbookname[MaxLineLen];//书目串
intbookno;//书号
}BookTermType;//书目项类型
typedefstructBookListType//书目表类型(有序表)
{
BookTermTypeitem[MaxBookNum];
intlast;//书目的数量
}BookListType;//书目表类型(有序表)
//生成一个其值等于串常量chars的串T
intStrAssign(HString*T,char*chars)
{
inti,j;
if((*T).ch)
free((*T).ch);//释放T原有空间
i=strlen(chars);//求chars的长度i
if(!
i)
{
//chars的长度为0
(*T).ch=NULL;
(*T).length=0;
}
else
{
//chars的长度不为0
(*T).ch=(char*)malloc(i*sizeof(char));//分配串空间
if(!
(*T).ch)//分配串空间失败
exit(0);
for(j=0;j
(*T).ch[j]=chars[j];
(*T).length=i;
}
return1;
}
//初始化(产生空串)字符串T
voidInitString(HString*T)
{
(*T).length=0;
(*T).ch=NULL;
}
//若S>T,则返回值>0;若S=T,则返回值=0;若S intStrCompare(HStringS,HStringT) { inti; for(i=0;i if(S.ch[i]! =T.ch[i]) returnS.ch[i]-T.ch[i]; returnS.length-T.length; } //由串S复制得串T intStrCopy(HString*T,HStringS) { inti; if((*T).ch) free((*T).ch);//释放T原有空间 (*T).ch=(char*)malloc(S.length*sizeof(char));//分配串空间 if(! (*T).ch)//分配串空间失败 exit(0); for(i=0;i (*T).ch[i]=S.ch[i]; (*T).length=S.length; return1; } //构造一个空的线性链表 intInitList(LinkList*L) { Linkp; p=(Link)malloc(sizeof(LNode));//生成头结点 if(p) { p->next=NULL; //将头尾结点都分配好,并将其下一结点置空 (*L).head=(*L).tail=p; (*L).len=0;//初始为0 return1; } else//分配失败返回 return0; } //分配由p指向的值为e的结点,并返回1;若分配失败。 则返回0 intMakeNode(Link*p,ElemTypee) { *p=(Link)malloc(sizeof(LNode));//动态分配一个Link空间 if(! *p) return0; (*p)->data=e;//赋值 return1; } //将指针s(s->data为第一个数据元素)所指(彼此以指针相链,以NULL结尾)的 //一串结点链接在线性链表L的最后一个结点之后,并改变链表L的尾指针指向新 //的尾结点 intAppend(LinkList*L,Links) { inti=1;//记录s为头的串结点个数 (*L).tail->next=s;//尾结点指向s while(s->next) { s=s->next; i++; } (*L).tail=s; (*L).len+=i; return1; } //置索引表idxlist为空表,且在idxliat.item[0]设一空串 voidInitIdxList(IdxListType*idxlist) { (*idxlist).last=0;//将索引表的关键词个数置为0 //将索引表的关键词块存储初始化为0 InitString(&(*idxlist).item[0].key); //将索引表的索引项的书号索引初始化为0 InitList(&(*idxlist).item[0].bnolist); } //从buf中提取书名关键词到词表wdlist,书号存入BookNo voidExtractKeyWord(int*BookNo) { inti,l, f=1;//f是字符串结束标志0: 结束1: 未结束 char*s1,*s2; if(buf[0]<'0'||buf[0]>'9')//buf的首字母不是数字 exit(0); //释放上一个书目在词表wdlist的存储空间,相当于初始化词表 for(i=1;i<=wdlist.last;i++) { free(wdlist.item[i]); wdlist.item[i]=NULL; } wdlist.last=0; //前三位为书号 *BookNo=(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0'); s2=&buf[2];//s2指向书号的尾字符 do { //提取书名关键词到词表wdlist s1=s2+1;//s1向后移动一个单词 //char*strchr(char*s,charc) //查找字符串s中首次出现字符c的位置 //说明: 返回首次出现c的位置的指针,如果s中不存在c则返回NULL。 //s2指向s1的第一个空格,如没有,返回NULL s2=strchr(s1,''); if(! s2)//到串尾 { s2=strchr(s1,'\n');//s2指向buf的最后一个字符(回车符) f=0; } l=s2-s1;//单词长度 if(s1[0]>='A'&&s1[0]<='Z')//单词首字母为大写 { //写入词表 //生成串空间(包括'\0') wdlist.item[wdlist.last]=(char*)malloc((l+1)*sizeof(char)); for(i=0;i wdlist.item[wdlist.last][i]=s1[i];//写入词表 wdlist.item[wdlist.last][l]='\0';//记得加上字符串结束符 for(i=0;i if(! strcmp(wdlist.item[wdlist.last],noidx.item[i])) break; if(i! =noidx.last)//是常用词 { free(wdlist.item[wdlist.last]);//从词表中删除该词 wdlist.item[wdlist.last]=NULL; } else wdlist.last++;//词表长度+1 } }while(f);//未到字符串结尾,继续循环 } //用wd返回词表wdlist中第i个关键词 voidGetWord(inti,HString*wd) { StrAssign(wd,wdlist.item[i]);//生成关键字字符串 } //在索引表idxlist中查询是否存在与wd相等的关键词。 若存在,则返回其 //在索引表中的位置,且b取值1;否则返回插入位置,且b取值0 intLocate(IdxListType*idxlist,HStringwd,int*b) { inti,m; for(i=(*idxlist).last;(m=StrCompare((*idxlist).item[i].key,wd))>0; --i) ; if(m==0)//找到 { *b=1; returni; } else { *b=0; returni+1; } } //在索引表idxlist的第i项上插入新关键词wd,并初始化书号索引的链表为空表 voidInsertNewKey(IdxListType*idxlist,inti,HStringwd) { intj; InitList(&(*idxlist).item[(*idxlist).last+1].bnolist); for(j=(*idxlist).last;j>=i;--j)//后移索引项 (*idxlist).item[j+1]=(*idxlist).item[j]; InitString(&(*idxlist).item[i].key); StrCopy(&(*idxlist).item[i].key,wd);//串拷贝插入新的索引项 InitList(&(*idxlist).item[i].bnolist);//初始化书号索引表为空表 (*idxlist).last++; } //在索引表idxlist的第i项中插入书号为bno的索引 voidInsertBook(IdxListType*idxlist,inti,intbno) { Linkp; if(! MakeNode(&p,bno))//分配失败LinkList.c exit(0); p->next=NULL; Append(&(*idxlist).item[i].bnolist,p);//插入新的书号索引 } //将书号为bno的关键词插入索引表 voidInsIdxList(IdxListType*idxlist,intbno) { inti,j; intb; HStringwd; InitString(&wd);//bo4-2.c for(i=0;i { GetWord(i,&wd); j=Locate(idxlist,wd,&b); if(! b) InsertNewKey(idxlist,j,wd);//插入新的索引项 InsertBook(idxlist,j,bno);//插入书号索引 } } //将生成的索引表idxlist输出到文件f voidPutText(FILE*f,IdxListTypeidxlist) { inti,j; Linkp; fprintf(f,"%d\n",idxlist.last); for(i=1;i<=idxlist.last;i++) { for(j=0;j fprintf(f,"%c",idxlist.item[i].key.ch[j]); fprintf(f,"\n%d\n",idxlist.item[i].bnolist.len); p=idxlist.item[i].bnolist.head; for(j=1;j<=idxlist.item[i].bnolist.len;j++) { p=p->next; fprintf(f,"%d",p->data); } fprintf(f,"\n"); } } intmain() { FILE*f; IdxListTypeidxlist;//索引表 intBookNo;//书号 inti,k,l; BookListTypebooklist;//书目表 HStringch;//索引字符串 Linkp; f=fopen("常用词表.txt","r");//打开常用词文件 if(! f) exit(0); printf("常用词表中的内容: \n"); fscanf(f,"%d",&noidx.last);//常用词个数 for(k=0;k { fscanf(f,"%s",buf); printf("%s\n",buf); l=strlen(buf); noidx.item[k]=(char*)malloc(l*sizeof(char)); strcpy(noidx.item[k],buf); } fclose(f); f=fopen("书目文件.txt","r");//打开书目文件 if(! f) exit(0); InitIdxList(&idxlist);//初始化索引表idxlist为空 wdlist.last=0;//词表长度初值为0 printf("书目文件中的内容: \n"); while(! feof(f)) { fgets(buf,MaxLineLen,f); l=strlen(buf); printf("%s\n",buf); if(l<=1) break; //从buf中提取关键词到词表,书号存入BookNo ExtractKeyWord(&BookNo); InsIdxList(&idxlist,BookNo); } fclose(f); //将生成的索引表idxlist输出到文件f f=fopen("关键词索引.txt","w"); if(! f) exit(0); PutText(f,idxlist);//将生成的索引表idxlist输出到文件f fclose(f); //根据索引查询图书 InitString(&ch);//初始化HString类型的变量 f=fopen("关键词索引.txt","r");//打开书名关键词索引表文件 if(! f) exit(0); fscanf(f,"%d",&idxlist.last);//书名关键词个数 for(k=0;k { fscanf(f,"%s",buf); i=0; while(buf[i]) buf[i++]=tolower(buf[i]);//字母转为小写 InitString(&idxlist.item[k].key); StrAssign(&idxlist.item[k].key,buf); InitList(&idxlist.item[k].bnolist);//初始化书号链表 fscanf(f,"%d",&i); for(l=0;l { fscanf(f,"%d",&BookNo); if(! MakeNode(&p,BookNo))//分配失败 exit(0); p->next=NULL; Append(&idxlist.item[k].bnolist,p);//插入新的书号索引 } } fclose(f); f=fopen("书目文件.txt","r");//打开书目文件 if(! f) exit(0); i=0; while(! feof(f))//把书目文件的内容拷到booklist中 { fgets(buf,MaxLineLen,f); //前三位为书号 booklist.item[i].bookno= (buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0'); strcpy(booklist.item[i].bookname,buf); i++; } booklist.last=i; printf("请输入书目的关键词(一个)"); scanf("%s",buf); i=0; while(buf[i]) buf[i++]=tolower(buf[i]);//字母转为小写 StrAssign(&ch,buf); i=0; do { k=StrCompare(ch,idxlist.item[i].key);//HString.c i++; }while(k&&i<=idxlist.last); if(! k)//索引表中有此关键词 { p=idxlist.item[i-1].bnolist.head->next; while(p) { l=0; while(l =booklist.item[l].bookno) l++; if(l printf("%s",booklist.item[l].bookname); p=p->next; } } else printf("没找到\n"); system("pause"); return0; } /* 常用词表.txt的内容: 5 A An In Of The 书目文件.txt的内容: 005ComputerDataStructures 010IntroductiontoDataStructures 023FundamentalsofDataStructures 034TheDesignand
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构C语言版 索引表 数据结构 语言版 索引
