📄 link_func.cpp
字号:
#include"link_func.h"
WordNode *init(const wchar_t *w,unsigned f)
{//建立一个结点
WordNode *p=(WordNode*)malloc(sizeof(WordNode));
if(p==NULL) return NULL;
p->word=wcsdup(w);
if(p->word==NULL) return NULL;
p->freq=f;
p->next=NULL;
return p;
}
WordNode *search(WordNode *hptr,const wchar_t *w)
{//搜索一个链表
WordNode *p=hptr;
while(p!=NULL){
if(wcscmp(p->word,w)==0) return p;//找到了则返回
p=p->next;//搜索下一个结点
}
return NULL;
}
WordNode *search_w_order(WordNode *hptr,const wchar_t *w)
{//搜索一个排序的链表
WordNode *p=hptr;
while(p!=NULL){
int cmp=wcscmp(p->word,w);
if(cmp==0) return p;
if(cmp>0) return NULL;
p=p->next;
}
return NULL;
}
void show_nodes(WordNode *hptr)
{//打印一个链表
WordNode *p=hptr;
int i=0;
if(p==NULL)
cout<<"链表为空!"<<endl;
while(p!=NULL){
printf("[%d]%ls\t%d\n",i++,p->word,p->freq);
p=p->next;
}
return;
}
void show_node(WordNode *hptr)
{//打印一个结点
WordNode *p=hptr;
if(p==NULL)
printf("结点为空!\n");
else printf("%ls\t%d\n",p->word,p->freq);
return;
}
WordNode *insert_head(WordNode *hptr,const wchar_t *w,unsigned f)
{//插入头结点
WordNode *q=init(w,f);
q->next=hptr;
hptr=q;
return hptr;
}
WordNode *insert_rear(WordNode*hptr,const wchar_t *w,unsigned f)
{//插入尾结点
WordNode *p=hptr,*q=init(w,f);
if(hptr==NULL){//如果链表为空,则返回该结点
hptr=q;
return hptr;
}
while(p->next!=NULL) p=p->next;
p->next=q;
return hptr;
}
int add_Titems(const wchar_t *filename,WordNode *hptr)
{//从unicode文本词表中加载
int n_item=0;
FILE *in=_wfopen(filename,L"rb");
if(in==NULL) {
cout<<"can't open file!\n";
return 0;
}
wchar_t line[100],word[30];
unsigned freq=0;
if(fgetwc(in)!=65279) rewind(in);//unic标记处理
while(fgetws(line,40,in)){
swscanf(line,L"%s%d",word,&freq);
insert_rear(hptr,word,freq);
n_item++;
}
fclose(in);
return n_item;
}
WordNode *add_Titems_order(const wchar_t *filename)
{//从unicode文本词表中加载,并排序
int n_item=0;
FILE *in=_wfopen(filename,L"rb");
if(in==NULL) {
cout<<"can't open file!\n";
return 0;
}
WordNode *hptr=NULL;
wchar_t line[100],word[30];
unsigned freq=0;
if(fgetwc(in)!=65279) rewind(in);//unic标记处理
while(fgetws(line,40,in)){
swscanf(line,L"%s%d",word,&freq);
hptr=insert_w_order(hptr,word,freq);
n_item++;
}
cout<<n_item<<endl;
fclose(in);
return hptr;
}
WordNode *insert_w_order(WordNode *hptr,const wchar_t *w,unsigned f)
{//排序插入结点
int cmp=0;
WordNode *p=hptr,*q;
if(hptr==NULL){//如果是一个空链表
q=init(w,f);
hptr=q;
return hptr;
}
cmp=wcscmp(hptr->word,w);
if(cmp==0) return hptr;
if(cmp>0) return insert_head(hptr,w,f);
while(p->next!=NULL){
int cmp=wcscmp(p->next->word,w);
if(cmp==0){
p->next->freq+=f;
return hptr;
}
if(cmp>0) break;
p=p->next;
}
q=init(w,f);
q->next=p->next;
p->next=q;
return hptr;
}
WordNode *delete_head(WordNode *&hptr)
{//删除头结点
WordNode *p=hptr;
if(hptr==NULL) {
cout<<"没有结点可以删除!"<<endl;
return hptr;
}
hptr=hptr->next;//如果存在,则指向下一个结点
free(p->word);//释放词条的内存
free(p);//释放结点内存
return hptr;
}
WordNode *delete_rear(WordNode *&hptr)
{//删除尾结点
WordNode *p=hptr,*q=p;
if(hptr==NULL){
cout<<"没有结点可以删除!"<<endl;
return hptr;
}
while(p->next!=NULL){
q=p;
p=p->next;
}
q->next=NULL;
free(p->word);
free(p);
return hptr;
}
WordNode *delete_w_order(WordNode *hptr,const wchar_t *w)
{//顺序查找并删除结点
int cmp=0;
WordNode *p=hptr,*q;
if(hptr==NULL){
cout<<"没有结点可以删除!"<<endl;
return hptr;
}
cmp=wcscmp(hptr->word,w);
if(cmp>0) return hptr;
if(cmp==0) return delete_head(hptr);
while(p->next!=NULL){
cmp=wcscmp(p->next->word,w);
if(cmp==0) break;
if(cmp>0) return hptr;
p=p->next;
}
q=p->next;
p->next=q->next;
free(q->word);
free(q);
return hptr;
}
void delete_list(WordNode *hptr)
{//删除整个链表
if(hptr==NULL) return;
delete_list(hptr->next);
free(hptr->word);
free(hptr);
}
void delete_list2(WordNode *hptr)
{//删除整个链表,不用递归
WordNode *p=hptr,*q=p;
if(hptr==NULL){
cout<<"没有链表可以删除!"<<endl;
return;
}
while(p->next!=NULL){
q=p;
p=p->next;
free(q->word);
free(q);
}
free(p->word);
free(p);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -