void proc_data(int line_no, char *vtype, int ptr_to_type, char *vname, char *ftype, char *fname) { Loc *tmp_loc; TNode *tmp_tnode; List *tmp_list; char *buffer; int i = 0; if((tmp_loc = (Loc *)malloc((unsigned)sizeof(Loc))) == NULL) { fprintf(stderr, "save_data/malloc(): tmp_loc"); exit(-1); } if((tmp_tnode = (TNode *)malloc((unsigned)sizeof(TNode))) == NULL) { fprintf(stderr, "save_data/malloc(): tmp_tnode"); exit(-1); } if((tmp_list = (List *)malloc((unsigned)sizeof(List))) == NULL) { fprintf(stderr, "save_data/malloc(): tmp_list"); exit(-1); } memset(tmp_loc, 0, sizeof(Loc)); memset(tmp_tnode, 0, sizeof(TNode)); memset(tmp_list, 0, sizeof(List)); list_init(tmp_list, clobber_loc); tmp_loc->fname = fname; tmp_loc->ftype = ftype; if(ptr_to_type > 0) { if( (buffer = (char *)malloc((unsigned)strlen(vtype) + 1 + ptr_to_type + 1 + 1)) == NULL) { fprintf(stderr, "save_data/malloc()"); exit(-1); } strcat(buffer, vtype); for(i = 0; i < ptr_to_type; i++) { strcat(buffer, asterisk); } vtype = buffer; } tmp_loc->type = vtype; tmp_loc->line_no = line_no; list_ins_next(tmp_list, NULL, tmp_loc); tmp_tnode->vname = vname; tmp_tnode->appear = tmp_list; bistree_insert(&tree, tmp_tnode); return; }
int main(int argc, char **argv) { BiTree tree; char *target; char sarray[12][STRSIZ], tarray[12][STRSIZ]; /***************************************************************************** * * * Load an array with the data to search. * * * *****************************************************************************/ strcpy(sarray[hop], "hop"); strcpy(sarray[hat], "hat"); strcpy(sarray[tap], "tap"); strcpy(sarray[bat], "bat"); strcpy(sarray[tip], "tip"); strcpy(sarray[mop], "mop"); strcpy(sarray[mom], "mom"); strcpy(sarray[cat], "cat"); strcpy(sarray[zoo], "zoo"); strcpy(sarray[wax], "wax"); strcpy(sarray[top], "top"); strcpy(sarray[dip], "dip"); /***************************************************************************** * * * Initialize the binary search tree. * * * *****************************************************************************/ bistree_init(&tree, compare_str, NULL); /***************************************************************************** * * * Perform some binary search tree operations. * * * *****************************************************************************/ fprintf(stdout, "Inserting some nodes\n"); if (bistree_insert(&tree, sarray[tap]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[tip]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[top]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[cat]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[bat]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Removing %s\n", sarray[tap]); if (bistree_remove(&tree, &sarray[tap]) != 0) { fprintf(stdout, "Could not find %s\n", sarray[tap]); } else { fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); } fprintf(stdout, "Removing %s\n", sarray[top]); if (bistree_remove(&tree, &sarray[top]) != 0) { fprintf(stdout, "Could not find %s\n", sarray[top]); } else { fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); } fprintf(stdout, "Removing %s\n", sarray[tip]); if (bistree_remove(&tree, &sarray[tip]) != 0) { fprintf(stdout, "Could not find %s\n", sarray[tip]); } else { fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); } fprintf(stdout, "Removing %s\n", sarray[hop]); if (bistree_remove(&tree, &sarray[hop]) != 0) { fprintf(stdout, "Could not find %s\n", sarray[hop]); } else { fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); } fprintf(stdout, "Inserting %s\n", sarray[hop]); if (bistree_insert(&tree, sarray[hop]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Inserting %s\n", sarray[dip]); if (bistree_insert(&tree, sarray[dip]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Inserting %s\n", sarray[tap]); if (bistree_insert(&tree, sarray[tap]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Inserting %s\n", sarray[top]); if (bistree_insert(&tree, sarray[top]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Inserting %s\n", sarray[tip]); if (bistree_insert(&tree, sarray[tip]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Inserting more nodes\n"); if (bistree_insert(&tree, sarray[mom]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[hat]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[mop]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[wax]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); if (bistree_insert(&tree, sarray[zoo]) != 0) return 1; fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); fprintf(stdout, "Removing %s\n", sarray[wax]); if (bistree_remove(&tree, &sarray[wax]) != 0) { fprintf(stdout, "Could not find %s\n", sarray[wax]); } else { fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); } fprintf(stdout, "Removing %s\n", sarray[hop]); if (bistree_remove(&tree, &sarray[hop]) != 0) { fprintf(stdout, "Could not find %s\n", sarray[hop]); } else { fprintf(stdout, "Tree size is %d\n", bistree_size(&tree)); fprintf(stdout, "(Preorder traversal)\n"); preorder_tree(bitree_root(&tree)); } fprintf(stdout, "Looking up some nodes\n"); strcpy(tarray[0], "top"); strcpy(tarray[1], "hop"); strcpy(tarray[2], "wax"); strcpy(tarray[3], "hat"); strcpy(tarray[4], "xxx"); target = tarray[0]; if (bistree_lookup(&tree, (void **)&target) == -1) fprintf(stdout, "Could not find %s\n", target); else fprintf(stdout, "Found %s\n", target); target = tarray[1]; if (bistree_lookup(&tree, (void **)&target) == -1) fprintf(stdout, "Could not find %s\n", target); else fprintf(stdout, "Found %s\n", target); target = tarray[2]; if (bistree_lookup(&tree, (void **)&target) == -1) fprintf(stdout, "Could not find %s\n", target); else fprintf(stdout, "Found %s\n", target); target = tarray[3]; if (bistree_lookup(&tree, (void **)&target) == -1) fprintf(stdout, "Could not find %s\n", target); else fprintf(stdout, "Found %s\n", target); target = tarray[4]; if (bistree_lookup(&tree, (void **)&target) == -1) fprintf(stdout, "Could not find %s\n", target); else fprintf(stdout, "Found %s\n", target); /***************************************************************************** * * * Destroy the binary search tree. * * * *****************************************************************************/ fprintf(stdout, "Destroying the search tree\n"); bistree_destroy(&tree); return 0; }