/*

============================================================================

Name        : btree.c

Author      : SeYUN

Version     :

Copyright   : Your copyright notice

Description : Hello World in C, Ansi-style

============================================================================

*/


#include <stdio.h>

#include <stdlib.h>

#include <search.h>


typedef struct _Customer {

char cust_no[10+1];

char name[50+1];

int age;

char tel[25+1];

}Customer;


int cust_compare(const Customer *one, const Customer *other) {

return strncmp(one->cust_no, other->cust_no, sizeof(one->cust_no) - 1);

}


void cust_free(void *p_cust) {

return;

}


void print_tree( const void *node, VISIT order, int level)

{

  Customer *p_cust;


  p_cust   = *(Customer **)node;


  if ( ( postorder == order)  ||

       ( leaf      == order) )

     printf( "%d cust: [%s],[%s],[%d],[%s]\n", level, p_cust->cust_no, p_cust->name, p_cust->age, p_cust->tel);

}


int main(void) {

FILE *fp = NULL;


void  *btree = NULL;

Customer temp = {0,};

Customer temp2 = {0,};

Customer key_value = {0,};


void **pp_cust = NULL; /* tfind, tsearch에서 리턴되는 포인터는 double pointer로 받아야 한다. */

Customer  *p_cust = NULL;


if ( fp = fopen("./cust.dat", "r")) {

if(fp == NULL) {

printf("파일 open fail\n");

exit(1);

}

while( 0 < fscanf(fp, "%s %s %d %s", temp.cust_no, temp.name, &temp.age, temp.tel)) {

Customer *p_temp = (Customer *)malloc(sizeof(Customer));

memcpy(p_temp, &temp, sizeof(Customer));

tsearch((void*)p_temp, &btree, cust_compare);

}

}


printf( "-------------------------------\n");

strcpy(key_value.cust_no, "A000000003");


printf("btree : %p\n", btree);


pp_cust = NULL;


pp_cust   = tfind( &key_value  , &btree, cust_compare);

if ( pp_cust)

{

p_cust = *(Customer **)pp_cust;

printf("찾은 값 : cust: [%s],[%s],[%d],[%s]\n", p_cust->cust_no, p_cust->name, p_cust->age, p_cust->tel);

}


twalk( btree, print_tree);


tdestroy(btree, free);


return 0;

}




+ Recent posts