📄 bb.c
字号:
/* file_name: bb.c
* author: wangtiezhen miuliang @ Xidian University
* description:
*/
/* Begin:
* read bb data from file;
*/
extern int readbb(void);
extern struct bb *inputbb(int, int);
int readbb(void) {
FILE *fp;
int i = 0;
if( (fp = fopen("bb", "r") ) == NULL) {
printf("It cann't opean file\n"); /* Cannot open the file */
return 0;
}
while( !feof(fp) ) {
int snum,bnum;
if(fscanf(fp, "%d%d", &bnum,&snum) == 2 ){
inputbb(bnum,snum);
}else{
break;
}
if(!feof(fp)){
i++;
}
}
fclose(fp);
return i;
}
struct bb *inputbb(int bnum, int snum) {
struct bb *p,*q;
p=(struct bb *)malloc(sizeof(struct bb));
p->snum = snum;
p->bnum=bnum;
p->lchild=NULL;
p->rchild=NULL;
if((p->bnum)!=0) {
q=bbbt;
if(q==NULL)
bbbt=p;
else {
while((q->lchild!=p)&&(q->rchild)!=p) {
if((p->bnum)<(q->bnum)) {
if(q->lchild!=NULL)
q=q->lchild;
else
q->lchild=p;
} else {
if(q->rchild!=NULL) {
q=q->rchild;
} else
q->rchild=p;
}
}
}
}
return bbbt;
}
/* End:
* read bb data from file;
*/
/* Begin:
* bb travel;
*/
int print_bb(struct bb* root)
{
if(root == NULL){
printf("no find.\n");
return -1;
}
// putchar('p');
printf("!!!booknum:\t%d\tstunum:\t%d\t\n", root->bnum, root->snum);
return 1;
}
int tree_visit_bb(struct bb* root, int f(), int style)
{
// putchar('j');
if(root == NULL)
{
return 0;
}
if(style == 0)
(*f)(root);
tree_visit_bb (root->lchild, f, style );
if(style == 1)
(*f)(root);
tree_visit_bb (root->rchild, f, style );
if(style == 2)
(*f)(root);
}
/* End:
* bb travel;
*/
struct bb *find_bb_bnum(struct bb *bbbt, int bnum)
{
struct bb *p;
p=bbbt;
if(p==NULL)
return (p);
if(bnum==p->bnum)
return(p);
else if(bnum > p ->bnum)
{
return find_bb_bnum(p->rchild, bnum );
}
else if(bnum < p ->bnum)
{
return find_bb_bnum(p->lchild, bnum );
}
}
/* Begin:
* del bb;
*/
extern int bb_del(bbpointer *);
extern int bb_del_value(bbpointer *, int );
int bb_del_value(bbpointer *proot, int num){
bbpointer root = *proot;
if(root == NULL){
return 0;
}
if(root ->bnum == num){
bb_del(proot);
}else if(num > root ->bnum){
if(root -> rchild == NULL){
return 0;
}
if(root ->rchild -> bnum == num){
return bb_del( &(root->rchild));
}else{
return bb_del_value(&(root->rchild), num );
}
}else if(num < root ->bnum){
if(root -> lchild == NULL){
return 0;
}
if(root ->lchild -> bnum == num){
return bb_del( &(root->lchild));
}else{
return bb_del_value(&(root->lchild), num );
}
}
}
int bb_del(bbpointer *proot){
bbpointer root = *proot;
if(root == NULL)
return 0;
if(root -> lchild == NULL){
*proot = root -> rchild;
free(root);
}else if(root -> rchild == NULL){
*proot = root -> lchild;
free(root);
}else{
bbpointer temp = root -> lchild;
while (temp->rchild != NULL){
temp = temp -> rchild;
}
temp -> rchild = root -> rchild;
free(root);
}
}
int bb_destory(bbpointer root){
if(root == NULL){
return 0;
}
bb_destory (root->lchild);
bb_destory (root->rchild);
// printf("Killing num:\t%ld\t\n", root->bnum);
free(root);
}
/* End:
* del bb;
*/
/* Begin:
* cal the num of books a man borrowed.
*/
int bb_stu_borrow_num(bbpointer root, int snum)
{
// struct bb* t = NULL;
// putchar('j');
int count = 0;
if(root == NULL){
return 0;
}
if(root -> snum == snum){
// printf("find it!");
count++;
}
// putchar('J');
count+= bb_stu_borrow_num (root->lchild, snum );
count+= bb_stu_borrow_num (root->rchild, snum );
// printf("\t");
return count;
}
/* End:
* cal the num of books a man borrowed.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -