List *merge(List *first, List *second, List *result) { if (list_is_empty(first) && list_is_empty(second)) return result; if (list_is_empty(first)) { if (list_is_empty(result)) return second; result->tail->next = second->head; second->head->prev = result->tail; list_delete(first); list_delete(second); return result; } if (list_is_empty(second)) { if (list_is_empty(result)) return first; result->tail->next = first->head; first->head->prev = result->tail; list_delete(first); list_delete(second); return result; } if (first->head->data->key <= second->head->data->key) { list_insert_last(result, first->head->data); list_delete_elem(first, first->head); } else { list_insert_last(result, second->head->data); list_delete_elem(second, second->head); } // list_print(result); return merge(first, second, result); }
ActionInformation *actinfo_add(List *list, STRING *name) { ActionInformation *info = NULL; for(info = list_first(list); info != NULL; info = list_next(list, info)) { if(strcmp(info->actionname, _chr(name)) == 0) return NULL; } info = sys_malloc(sizeof(ActionInformation)); strcpy(info->actionname, _chr(name)); list_insert_last(list, info); return info; }
List *merge_sort(List *list) { if (list->head == NULL) return NULL; if (list_lenght(list) < 2) return list; if (list_lenght(list) == 2) { if (list->head->data->key > list->tail->data->key) two_elem_list_reverse(list); return list; } if (list_lenght(list) > 2) { List *list_1 = list_create(); List *list_2 = list_create(); int lenght = list_lenght(list); for (int i = 0; i < lenght; ++i) { ListNode *tmp = list->head; if (i + 1 > lenght/ 2) list_insert_last(list_2, tmp->data); else list_insert_last(list_1, tmp->data); list_delete_elem(list, tmp); } list_1 = merge_sort(list_1); list_2 = merge_sort(list_2); list = merge(list_1, list_2, list); } return list; }
RegAction *action_register(STRING *name, void *ptr) { if(registeredActions == NULL) registeredActions = list_create(); RegAction *info = NULL; for(info = list_first(registeredActions); info != NULL; info = list_next(registeredActions, info)) { if(strcmp(info->actionname, _chr(name)) == 0) return NULL; } info = sys_malloc(sizeof(RegAction)); strcpy(info->actionname, _chr(name)); info->ptr = ptr; list_insert_last(registeredActions, info); return info; }
int main(void) { int i; int ret; t_list * list = list_init(); if (list == NULL) return 1; for (i = 0; i < NUM; i++) { tab_item_ptr[i] = &tab_item[i]; } for (i = 0; i < NUM; i++) { ret = list_insert_last(list, tab_item_ptr[i]); if (ret != LIST_OP_OK) fprintf(stderr, "CHYBA\n"); } printf("LIST\n"); for (i = 0; i < NUM; i++) { printf("%d: %x\n", i, list_get_first(list)); list_free_first(list); } for (i = 0; i < NUM; i++) { printf("%d: %x\n", i, list_get_first(list)); list_free_first(list); } list_free(list); printf("%x\n", list); return 0; }
void interface_2(List *list) { annotation_2(); while (1) { int key_1 = 0; int value_1 = 0; int n = 0; printf("Num of actinon(2):\n"); scanf("%d", &n); if (n == 0) break; if (n == 1) { printf("Key after each add\n"); int find_key = 0; scanf("%d", &find_key); if (!find_elem(list, find_key)) { printf("Try again, press 1\n"); continue; } printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_after_elem(find_elem(list, find_key), ins_data); continue; } if (n == 2) { printf("Key before each add\n"); int find_key = 0; scanf("%d", &find_key); if (!find_elem(list, find_key)) { printf("Try again, press 1\n"); continue; } printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_before_elem(find_elem(list, find_key), ins_data); continue; } if (n == 3) { printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_front(list, ins_data); continue; } if (n == 4) { printf("Key - Value\n"); scanf("%d%d", &key_1, &value_1); sData *ins_data = list_node_data_create(key_1, value_1); if (ins_data == NULL) { printf("ERRROR\n"); continue; } list_insert_last(list, ins_data); continue; } if (n == 5) { list_print(list); continue; } if (n == 9) { annotation_2(); continue; } printf("Wrong input try again\n"); } }