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

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

最后打印。

代码如下:

 #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 ;
}

最新文章

  1. 【Java EE 学习 69 上】【struts2】【paramsPrepareParamsStack拦截器栈解决model对象和属性赋值冲突问题】
  2. 配置Visual Studio Code在Mac上作为.NET Core的IDE
  3. 掌握Thinkphp3.2.0----标签库
  4. 资金归集率比率sql
  5. Java反编译插件JODE介绍
  6. .net弹出框
  7. 如何用pdb进行python调试
  8. VMware网络配置 - 三种网络模式简介
  9. ExtJs之字段集FieldSet
  10. PHP中最容易忘记的一些知识点总结
  11. c#自动更新+安装程序的制作 (转)
  12. (转)iOS如何取得APP的版本信息跟服务器对比进行升级提示?
  13. CSS3知识点整理(二)----CSS3选择器
  14. vue-resource的使用,前后端数据交互
  15. arduino 引脚作为输入时的不稳定 解决方案
  16. Tomcat 的 server.xml 文件详解
  17. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7
  18. leecode第五十三题(最大子序和)
  19. 非常可乐---hdu 1495(BFS)
  20. 解析C#彩色图像灰度化算法的实现代码详解

热门文章

  1. POJ3061 尺取法
  2. js操作document文档元素 节点交换交换
  3. 1.jenkins持续集成-jenkins安装
  4. 一个简单的makefile
  5. asp.net应用程序生命周期和asp.net网页的生命周期
  6. dedecms内容管理系统学习
  7. c# json转换实例
  8. 读书笔记——body and html
  9. mysql 乱码问题(程序界面显示正常,mysql command line显示乱码)
  10. 对button或radiobutton制作样式