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); }
static int list_pop_nth (SLang_List_Type *list, SLindex_Type indx) { SLang_Object_Type *obj; if (NULL == (obj = find_nth_element (list, indx, NULL))) return -1; if (-1 == _pSLpush_slang_obj (obj)) return -1; list_delete_elem (list, &indx); return 0; }
void interface_1(List *list) { annotation(); while (1) { int interact = 0; int key = 0; printf("Num of actinon:\n"); scanf("%d", &interact); if (interact == 0) break; if (interact == 1) { interface_2(list); continue; } if (interact == 2) { if (list_is_empty(list)) { printf("Nothing to delete, enter element, just press 1\n"); continue; } printf("Key of delete element?\n"); scanf("%d", &key); list_delete_elem(list, find_elem(list, key)); continue; } if (interact == 3) { list = merge_sort(list); continue; } if (interact == 4) { list_print(list); continue; } if (interact == 9) { annotation(); continue; } printf("Wrong input try again\n"); } }
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; }