/** * find_spec: Parse CCL find specification * cclp: CCL Parser * qa: Qualifier attributes already applied. * return: pointer to node(s); NULL on error. */ static struct ccl_rpn_node *find_spec(CCL_parser cclp, ccl_qualifier_t *qa) { struct ccl_rpn_node *p1, *p2, *pn; if (!(p1 = search_elements(cclp, qa))) return NULL; while (1) { switch (KIND) { case CCL_TOK_AND: ADVANCE; p2 = search_elements(cclp, qa); if (!p2) { ccl_rpn_delete(p1); return NULL; } pn = ccl_rpn_node_create(CCL_RPN_AND); pn->u.p[0] = p1; pn->u.p[1] = p2; pn->u.p[2] = 0; p1 = pn; continue; case CCL_TOK_OR: ADVANCE; p2 = search_elements(cclp, qa); if (!p2) { ccl_rpn_delete(p1); return NULL; } pn = ccl_rpn_node_create(CCL_RPN_OR); pn->u.p[0] = p1; pn->u.p[1] = p2; pn->u.p[2] = 0; p1 = pn; continue; case CCL_TOK_NOT: ADVANCE; p2 = search_elements(cclp, qa); if (!p2) { ccl_rpn_delete(p1); return NULL; } pn = ccl_rpn_node_create(CCL_RPN_NOT); pn->u.p[0] = p1; pn->u.p[1] = p2; pn->u.p[2] = 0; p1 = pn; continue; } break; } return p1; }
/* Search a value in the BST and returns that value */ int search_elements(struct node **address_root, int data) { struct node *current = *address_root; if(current == NULL) return; if(current->data < data) search_elements(&(current->right_child), data); else if(current->data > data) search_elements(&(current->left_child), data); else return current->data; }
int main() { /* 1. Inserting the elements. 2. Searching the elements. 3. Inroder, postorder and preorder. 4. Height of the tree. */ struct node *root = NULL; insert_elements(&root, 5); insert_elements(&root, 5); insert_elements(&root, 3); insert_elements(&root, 7); printf("\nHeight of the BST: %d\n", height_BST(root)); insert_elements(&root, 1); insert_elements(&root, 2); insert_elements(&root, 6); insert_elements(&root, 8); print_tree_inorder(root); printf("\n"); //print_tree_postorder(root); insert_elements(&root, 22); print_tree_postorder(root); printf("\n"); print_tree_preorder(root); printf("\n"); // A tree with only the root is of height 0 printf("\nHeight of the BST: %d\n", height_BST(root)); printf("\n%d\n", search_elements(&root, 4)); }
void *access_element(hash *array, char *key) { int index = search_elements(array, key); if (index == -1) return NULL; else return array->hashes[index]->value; }
void delete_element(hash *array, char *key) { int index = search_elements(array, key); if (index == -1) { return; } free(array->hashes[index]->value); strcpy(array->hashes[index]->key, "\0"); array->hashes[index]->value = NULL; }
void create_element(hash *array) { int index = search_elements(array, "\0"); if (index >= 0) { array->num++; array->hashes = (hash_element**)realloc(array->hashes, sizeof(hash_element*) * array->num + 1); index = array->num; } else { free(array->hashes[index]->key); } array->hashes[index] = (hash_element*)malloc(sizeof(hash_element)); array->hashes[index]->key = (char*)malloc(1); array->hashes[index]->value = malloc(1); }
int assign_element(hash *array, char *key, void *value, int size) { int i, index; index = search_elements(array, key); if (index == -1) { create_element(array); index = array->num; } array->hashes[index]->key = (char*)realloc(array->hashes[index]->key, strlen(key)); strcpy(array->hashes[index]->key, key); array->hashes[index]->value = realloc(array->hashes[index]->value, size); memcpy(array->hashes[index]->value, value, size); return index; }