首页 技术 正文
技术 2022年11月10日
0 收藏 338 点赞 3,406 浏览 2125 个字

建立一棵二叉树,每个接单存放单词以及指向一个链表的指针,以及指向左右节点的指针。链表内存放行号以及指向下一个链表节点的指针。

每录入一个单词,先寻找二叉树,再寻找它的链表,分别将单词和行号插入二叉树和链表,这样,每一个单词自然就有一个属于它的行号链表。

最后打印。

代码如下:

 #include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h> #define MAXWORD 100
#define MAXHANG 10000 extern int getword(char *word, int lim);
struct hang *hes(struct hes *, int); /* 链表hang,每个节点存放行数及一个指向下一个行的指针 */
struct ci *cc(struct ci *, char *, int);/* 二叉树ci,每个节点存放一个单词以及一个指向链表hang的指针 */
void hesprint(struct hes *); /* 打印链表 */
void ccprint(struct cc *); /* 打印二叉树 */
struct hang *talloc(void); /* 为树cc申请储存空间 */
struct ci *atalloc(void); /* 为树cc申请储存空间 */
char *strduper(char *s); /* word存放在一个安全的地方 */
int exclude(char *); struct hang {
int x;
struct hang *next;
}; struct ci {
char *word;
struct hang *hangg;
struct ci *left;
struct ci *right;
}; /* 编写程序,打印文本中的所有单词,并且打印每个单词出现的行号,is,to等非实意单词不考虑 */
main() {
struct ci *root;
char word[MAXWORD];
int hangs; root=NULL; while((hangs=getword(word,MAXWORD))!=EOF) /* getword返回行号 */
if((isalpha(word[])||word[]=='_')&&!exclude(word)) /* 函数exclude确认单词是否可以被忽略 */
root=cc(root,word,hangs);
ccprint(root);
return ;
}
struct ci *cc(struct ci *p, char *w, int x) {
int cond; if(p==NULL) {
p=atalloc();
p->word=strduper(w);
p->hangg=NULL;
p->hangg=hes(p->hangg,x);
p->left=p->right=NULL; } else if ((cond= strcmp(w,p->word))==)
p->hangg=hes(p->hangg,x);
else if (cond <)
p->left=cc(p->left,w,x);
else
p->right=cc(p->right,w,x);
return p;
} struct hang *hes(struct hang *p, int x) { if(p==NULL) {
p=talloc();
p->x=x;
p->next=NULL;
} else if (p->x==x)
;
else if (p->x!=x)
p->next=hes(p->next,x);
return p; } struct hang *talloc(void) {
return (struct hang *) malloc(sizeof(struct hang)); }
struct ci *atalloc(void) {
return (struct ci *) malloc(sizeof(struct ci));
}
char *strduper(char *s) {
char *p; p=(char *)malloc(strlen(s)+);
if(p!=NULL)
strcpy(p,s);
return p;
} void hesprint(struct hang *p) {
if(p!=NULL) {
printf("%4d ",p->x);
hesprint(p->next);
}
}
void ccprint(struct ci *p) {
if(p!=NULL)
{
ccprint(p->left);
printf("%s ",p->word);
hesprint(p->hangg);
printf("\n");
ccprint(p->right);
} } int exclude(char *s) {
static char *ex[]={
"a",
"an",
"and",
"are",
"in",
"is",
"of",
"or",
"that",
"the",
"this",
"to"
};
int cond,mid;
int low=;
int high=sizeof(ex)/sizeof(char *)-; while(low<=high) {
mid=(low+high)/;
if((cond=strcmp(s,ex[mid]))==)
return ;
else if(cond<)
high=mid-; else if(cond>)
low=mid+; }
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,301