日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

C语言字符串倒排,C语言兑现简单的倒排文件索引

發(fā)布時(shí)間:2025/3/12 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言字符串倒排,C语言兑现简单的倒排文件索引 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C語言實(shí)現(xiàn)簡單的倒排文件索引

inver.h文件

#ifndef INVERT_FILE_H

#define INVERT_FILE_H

#include

#include

typedef struct _invertfile_ {

unsigned int tablelen;

void **table;

//unsigned int offset;

unsigned int nodecount;

}if_t;

typedef struct _word_{

unsigned int id;

unsigned int refered;//

void *link;

}word_t;

typedef struct _word_frequency_{

unsigned int d_id;

unsigned int refered;//the num of referenced in the document

void *next;

}wf_t;

if_t* invertfile_create(int length);

void invertfile_insert(if_t *h,int w_id,int d_id);

wf_t* invertfile_search(if_t *h,int w_id,int d_id);

void invertfile_traverse(if_t *h);

void invertfile_free(if_t *h);

#endif

invert.cpp

#include"invert.h"

if_t* invertfile_create(int length){

if_t *h;

h = (if_t *)calloc(1,sizeof(if_t));

if (NULL == h) return NULL;

h->table =(void **)calloc(length,sizeof(void *));

h->tablelen=length;

h->nodecount=0;

word_t *w;

for(int i=0;i

h->table[i]=malloc(sizeof(word_t));

w=(word_t*)h->table[i];

w->id=i;

w->refered=0;

w->link=NULL;

}

return h;

}

//check if document d_id have word w_id

wf_t* invertfile_search(if_t *h,int w_id,int d_id){

word_t *w;

wf_t*wf;

w=(word_t*)h->table[w_id];

if(w->refered>0){

wf=(wf_t*)w->link;

while(wf){

if(wf->d_id==d_id)return wf;

wf=(wf_t*)wf->next;

}

}

return NULL;

}

void invertfile_insert(if_t *h,int w_id,int d_id){

word_t * w=(word_t*)h->table[w_id];

wf_t * wf;

if((wf=invertfile_search(h,w_id,d_id))!=NULL){

wf->refered++;

}

else{

wf=(wf_t *)malloc(sizeof(wf_t));

wf->next=w->link;

w->link=wf;

w->refered++;

wf->refered++;

wf->d_id=d_id;

h->nodecount++;

}

}

void invertfile_free(if_t *h){

word_t *w;

wf_t* wf,*cur;

for(int i=0;itablelen;i++){

w=(word_t*)h->table[i];

wf=(wf_t*)w->link;

while(wf!=NULL){

cur=wf;

wf=(wf_t*)wf->next;

free(cur);

}

free(w);

}

free(h->table);

}

void invertfile_traverse(if_t *h){

word_t *w;

wf_t* wf,*cur;

for(int i=0;itablelen;i++){

w=(word_t*)h->table[i];

wf=(wf_t*)w->link;

printf("word_id:%d;",w->id);

while(wf!=NULL){

cur=wf;

wf=(wf_t*)wf->next;

printf("d_id:%d,freq:%d;",cur->d_id,cur->refered);

}

printf("\n");

}

}測試文件main.cpp

#include"invert.h"

int main(){

if_t *f=invertfile_create(10);

invertfile_insert(f,1,1);

invertfile_insert(f,1,1);

invertfile_insert(f,1,3);

invertfile_insert(f,2,5);

invertfile_traverse(f);

invertfile_free(f);

}實(shí)驗(yàn)結(jié)果:

word_id:0;

word_id:1;d_id:3,freq:1;d_id:1,freq:2;

word_id:2;d_id:5,freq:1;

word_id:3;

word_id:4;

word_id:5;

word_id:6;

word_id:7;

word_id:8;

word_id:9;

總結(jié)

以上是生活随笔為你收集整理的C语言字符串倒排,C语言兑现简单的倒排文件索引的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。