/*======================================================================\ * Author (作者): i.sshe * Date (日期): 2016/03/26 * Others (其他): \*=======================================================================*/ int insert_element(HASH_TABLE_S *hash_table, int hash_len, int value) { int position_num = value % hash_len; HASH_NODE_S *node ; if (search_element(hash_table, hash_len, value)) { return 0; } node = (HASH_NODE_S *)malloc(sizeof(HASH_NODE_S)); if (node == NULL) { printf("insert element malloc error!\n"); exit(1); } node->data = value; if (hash_table[position_num].hash_node != NULL) //冲突了 { node->next = hash_table[position_num].hash_node; hash_table[position_num].hash_node->prev = node; hash_table[position_num].hash_node = node; node->prev = NULL; } else { hash_table[position_num].hash_node = node; } return 1; }
int countGreaterNumbers(struct transaction *Arr, int len, char *date) { int temp = search_element(Arr, date, len, 0, len-1); if (temp == -1) return 0; return (len - temp - 1); }
int search_element(struct transaction *arr, char *key, int len, int low, int high) { //if(key<arr[low] || key>arr[high]) int temp1, temp2; temp1 = isGreater(key, arr[low].date); temp2 = isGreater(key, arr[high-1].date); //printf("%d,%d",temp1,temp2); if (temp1 == -1 || temp2 == 1) return -1; if (high >= low) { //printf("high=%d\nlow=%d",high,low); int mid = (low + high) / 2; temp1 = isGreater(arr[mid + 1].date, key); temp2 = isGreater(arr[mid].date, key); if ((mid == 0 || temp1 == 1) && temp2 == 0) return mid; else if (temp2 == 1) search_element(arr, key, len, low, mid-1); else search_element(arr, key, len, mid+1, high); } }
void add_element_in_order(list_t *list, content_t element) { node_t *new_element = NULL; node_t *cur = NULL; node_t *next = NULL; if (list == NULL) { printf("Initialize your list."); return; } if (search_element(list, element) != NULL) { printf("Element exists!"); return; } new_element = create_node(element); if (list->size == 0) { list->head = new_element; list->size++; return; } if (COMP(new_element, list->head) < 0) { new_element->next = list->head; list->head = new_element; list->size++; return; } next = list->head->next; cur = list->head; while (next != NULL && (COMP(next, new_element) < 0)) { cur = next; next = next->next; } new_element->next = next; cur->next = new_element; list->size++; return; }
int main() { struct node *start; start = NULL; int ch, x; printf("1.Insertion in end\n2.Insertion in beginning\n5.Deletion in end\n6.Deletion in beginning\n7.Size of link list\n8.Printing\n9.Search for an element\n>9.Exit\n"); while (1) { printf("Enter your choice ----> "); scanf("%d", &ch); if (ch == 1) { printf("Number to be inserted in end ----> "); scanf("%d", &x); start = insert_in_end(start, x); } else if (ch == 2) { printf("Number to be inserted in beginning ----> "); scanf("%d", &x); start = insert_in_beginning(start, x); } else if (ch == 5) { start = delete_in_end(start); } else if (ch == 6) { start = delete_in_beginning(start); } else if (ch == 7) { printf("Size of link list ----> %d\n", size_of_link_list(start)); } else if (ch == 8) { output(start); } else if (ch == 9) { printf("Enter the number to be searched ----> "); scanf("%d", &x); search_element(x); } else { break; } } return 0; }
int main(int argc, char *argv[]) { if (initialize_context(&my_context, argc, argv) != 0) { usage(argv[0]); return 1; } if (art_tree_map_init(&myds, &my_context) != 0) { fprintf(stderr, "failed to initialize memory pool file\n"); return 1; } if (my_context.pop == NULL) { perror("pool initialization"); return 1; } if (art_tree_init(my_context.pop, &my_context.newpool)) { perror("pool setup"); return 1; } if ((my_context.mode & FILL)) { if (add_elements(&my_context)) { perror("add elements"); return 1; } } if ((my_context.mode & INSERT)) { if (insert_element(&my_context)) { perror("insert elements"); return 1; } } if ((my_context.mode & SEARCH)) { if (search_element(&my_context)) { perror("search elements"); return 1; } } if ((my_context.mode & REMOVE)) { if (delete_element(&my_context)) { perror("delete elements"); return 1; } } if (my_context.mode & DUMP) { art_iter(my_context.pop, dump_art_leaf_callback, NULL); } if (my_context.mode & GRAPH) { printf("digraph g {\nrankdir=LR;\n"); art_iter(my_context.pop, dump_art_node_callback, NULL); printf("}"); } exit_handler(&my_context); return 0; }