易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
C语言用二叉树统计一个源文件中每个单词的次数
由于出现的单词不确定,所以用二叉树实现:
//TreeNode.h
typedef
struct
_TreeNode
{
int
count; //出现的次数
char
* word;//单词本身
struct
_TreeNode* left;
struct
_TreeNode* right;
}TreeNode;
//给TreeNode分配内存
TreeNode* talloc(
void
)
{
return
(TreeNode*)malloc(
sizeof
(TreeNode));
}
//打印tree
void
tprint(TreeNode* root)
{
//打印left->self->right
if
(root!=NULL)
{
tprint(root->left);
printf("%4d %s ",root->count,root->word);
tprint(root->right);
}
}
//把单词添加节点的合适位置
TreeNode* addNode(TreeNode* node,
const
char
* word)
{
int
con;
TreeNode* tmp;
if
(node==NULL)
{
node = talloc();
node->count=1;
node->word=strdup(word);
node->left=node->right=NULL;
}
else
if
((con=strcmp(word,node->word))<0)
{
tmp = addNode(node->left,word);
node->left=tmp;
}
else
if
(con>0)
{
tmp = addNode(node->right,word);
node->right=tmp;
}
else
{
node->count++;
}
return
node;
}
/**
从指定的流中读取单词
*/
int
getWord(
char
* ch,
size_t
n,
FILE
* f)
{
int
c;
char
* p = ch;
while
(isspace(c=fgetc(f)))
;
if
(c!=EOF)
*p++=c;
if
(!isalpha(c))
{
*p=" ";
return
c;
}
for
(;--n>0;p++)
{
if
(!isalnum(*p=fgetc(f)))
{
ungetc(*p,f);
break
;
}
}
*p=" ";
return
c;
}
//是否tree占用的内存
void
treeFree(TreeNode* root)
{
if
(root!=NULL)
{
treeFree(root->left);
free(root->word); //释放节点的word占用的内存
free(root); //是否节点占用的内存
treeFree(root->right);
}
}
//Test.c
#include <stdio.h>
#include <stdlib.h>
#include "TreeNode.h"
#define MAX 100
int
main(
int
argc,
char
*argv[])
{
FILE
* f;
char
w[MAX]={0};
char
* fname="TreeNode.h";
if
((f=fopen(fname,"r"))!=NULL)
{
TreeNode* root = NULL;
while
((getWord(w,MAX,f)!=EOF))
{
if
(isalpha(w[0]))
root = addNode(root,w);
}
tprint(root);
treeFree(root);
fclose(f);
}
else
{
printf("open %s error ",fname);
}
getchar();
return
0;
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图