?? 5-4-1.c
字號:
/*中國系統(tǒng)分析員顧問團,http://www.csai.cn*/
/*程序員下午考試指南書籍源碼*/
#include <stdio.h>
#include <malloc.h>
#define INF "t2.in"
#define OUTF "t2.out"
typedef struct treenode{
int val;
int count;
struct treenode *left, *right;
} Binary;
void binary_tree ( Binary **t,int data ){
Binary *ptr,*p;
int d;
p = NULL;ptr = *t;
while(ptr)
if (data == ptr->val) {
ptr->count++;
return;
}
else {
p=ptr;
ptr =ptr->val<data ? ptr->right:ptr->left;
}
ptr =(Binary * )malloc(sizeof(Binary));
ptr->left = ptr->right = NULL;
ptr->val = data; ptr->count=1;
if ( p==NULL ) *t = ptr;
else if (data > p->val ) p->right = ptr;
else p->left = ptr;
}
void travel_tree( FILE *fpt,Binary *t) {
if (t == NULL) return;
travel_tree ( fpt, t->left);
fprintf(fpt,"%d %d\n",t->val,t->count );
travel_tree (fpt, t->right);
}
void main () {
FILE *fpt;
Binary *root = NULL;
int d;
if (( fpt = fopen (INF, "r")) == NULL ) {
printf( "Can't open file !\n") ;
exit(1);
}
while ( fscanf ( fpt,"%d" ,&d) == 1)
binary_tree( &root,d);
fclose (fpt);
fpt = fopen(OUTF,"wt" ) ;
travel_tree(fpt,root);
fclose(fpt) ;
travel_tree( stdout,root);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -