Exemple #1
0
/**
* \brief Function that merges two sublists together.
* \param a Pointer to the first sublist.
* \param b Pointer to the second sublist.
* \return Returns a pointer to the now merged list.
*/
NODE* sorted_merge(NODE* a, NODE* b)
{
	NODE* result = NULL;
	
	/**
	* If one of them is empty, the other is returned.
	*/
  	if (a == NULL){

    	return(b);

  	} else if (b==NULL){
    	
    	return(a);
  	
  	}
	
	/**
	* Based on priority, it picks a or b and recurs.
	*/
  	if (a->priority <= b->priority){
    	
    	result = a;
     	result->next = sorted_merge(a->next, b);

  	} else {
     	
     	result = b;
     	result->next = sorted_merge(a, b->next);

  	}
	
  	return(result);
}
Exemple #2
0
/**
* \brief Merge sort function.
* \param headRef Takes as a parameter a pointer to the pointer of the first element.
* \return Doesn't return anything.
*/
void merge_sort(NODE** headRef)
{
  	NODE* head = *headRef;
	NODE* a;
	NODE* b;
	
	/** 
	* If list is empty or has 1 element, it quits.
	*/
  	if ((head == NULL) || (head->next == NULL)){

    	return;
  	
  	}
 
	/**
	* It else splits it into 2 sublists.
	*/
 	front_back_split(head, &a, &b); 
 
	/** 
	* Recursively sorts the sublists.
	*/
  	merge_sort(&a);
  	merge_sort(&b);
 
	/**
	* Merges them together.
	*/
  	*headRef = sorted_merge(a, b);

}
Exemple #3
0
int
main(int argc , char **argv)
{
	show(l1);
	show(l2);
	sorted_merge(&l1,&l2,&l3);
	show(l3);
	return 0;
}
Exemple #4
0
/* cmp can compare with no, name, score, etc. */
list *sorted_merge(list *a, list *b, float (*cmp)(list *a, list *b))
{
    list *result = NULL;
    if (!a) return b;
    if (!b) return a;

    /* Pick either a or b, and recur */
    /* for int, <= 0 is OK, but for float needs checking range*/
    if ((cmp(a, b) <= 0.000000001))
    {
        result = a;
        result->next = sorted_merge(a->next, b, cmp);
    }
    else
    {
        result = b;
        result->next = sorted_merge(a, b->next, cmp);
    }
    return(result);
}
Exemple #5
0
static struct filelist *
merge_sort(struct filelist *head, int (*cmp)(const struct filelist *, const struct filelist *))
{
        struct filelist *left, *right;
        if (head == NULL || head->fl_next == NULL)
                return head;
        list_split(head, &left, &right);
        left = merge_sort(left, cmp);
        right = merge_sort(right, cmp);

        return sorted_merge(left, right, cmp);
}
Exemple #6
0
static t_file	*sorted_merge(t_file *a, t_file *b, int (*f)(t_file*, t_file*))
{
	t_file	*result;

	result = NULL;
	if (a == NULL)
		return (b);
	else if (b == NULL)
		return (a);
	if (f(a, b))
	{
		result = a;
		result->next = sorted_merge(a->next, b, f);
	}
	else
	{
		result = b;
		result->next = sorted_merge(a, b->next, f);
	}
	return (result);
}
Exemple #7
0
t_list	*sorted_merge(t_list *a, t_list *b,
	int (*cmp)(void *a_, void *b_), void *(*get_data)(t_list *e))
{
	t_list	*result;

	result = NULL;
	if (a == NULL)
		return (b);
	else if (b == NULL)
		return (a);
	if (cmp(get_data(a), get_data(b)))
	{
		result = a;
		result->next = sorted_merge(a->next, b, cmp, get_data);
	}
	else
	{
		result = b;
		result->next = sorted_merge(a, b->next, cmp, get_data);
	}
	return (result);
}
Exemple #8
0
void merge_sort(list **head, int (*comparator)(const void *, const void *))
{
    if(head)
    {
        list *mid = NULL;
        front_back_split(*head, &mid);
        if(!mid)
            return;
        merge_sort(head, comparator);
        merge_sort(&mid, comparator);
        *head = sorted_merge(*head, mid, comparator);
    }
}
Exemple #9
0
static void merge_sort(struct ListNode **headptr)
{
	struct ListNode *head;
	struct ListNode *a, *b;

	if (!(head = *headptr) || !head->next)
		return;

	front_back_split(head, &a, &b);
	merge_sort(&a);
	merge_sort(&b);
	*headptr = sorted_merge(a, b);
}
void linkedList<T>::merge_sort(linkedListNode<T>* &a)
{
   if (a==NULL || a->next==NULL)
      return;

   linkedListNode<T> *b;
   split(a, b);

   merge_sort(a);
   merge_sort(b);

   a = sorted_merge(a,b);
}
Exemple #11
0
void merge_sort(list **headRef, float (*cmp)(list *a, list *b))
{
    list *head = *headRef;
    list *a = NULL;
    list *b = NULL;

    if ((head == NULL) || (head->next == NULL)) return;

    /* Split head into 'a' and 'b' sublists, recursively sort the sublists */
    front_back_split(head, &a, &b);
    merge_sort(&a, cmp);
    merge_sort(&b, cmp);
    *headRef = sorted_merge(a, b, cmp);
}
Exemple #12
0
void sort_merge_(int n, int* a, int* buffer)
{
    if (n <= 1)
        return;

    // [0, n) --> [0, middle) + [middle, n)
    int middle = n/2;
    sort_merge_(middle, a, buffer);
    sort_merge_(n-middle, a+middle, buffer);

    sorted_merge(middle, a, n-middle, a+middle, buffer);
    for (int i = 0; i < n; ++i)
        a[i] = buffer[i];
}
Exemple #13
0
void			merge_sort(t_file **head_ref, int (*f)(t_file*, t_file*))
{
	t_file	*head;
	t_file	*a;
	t_file	*b;

	head = *head_ref;
	if ((head == NULL) || (head->next == NULL))
		return ;
	front_back_split(head, &a, &b);
	merge_sort(&a, f);
	merge_sort(&b, f);
	*head_ref = sorted_merge(a, b, f);
}
Exemple #14
0
void	ft_lstsort(t_list **list,
	int (*cmp)(void *a_, void *b_), void *(*get_data)(t_list *e))
{
	t_list	*head;
	t_list	*a;
	t_list	*b;

	head = *list;
	if ((head == NULL) || (head->next == NULL))
		return ;
	back_split(head, &a, &b);
	ft_lstsort(&a, cmp, get_data);
	ft_lstsort(&b, cmp, get_data);
	*list = sorted_merge(a, b, cmp, get_data);
}
void linkedList<T>::merge_with(linkedList<T> &b)
{
   first = sorted_merge(first, b.first);
   length += b.length;
   b.first = NULL;
   b.last = NULL;
   b.length = 0;

   last = first;
   if (last!=NULL)
   {
      while (last->next != NULL)
         last = last->next;
   }
}
Exemple #16
0
struct node* sorted_merge(struct node* a, struct node* b)
{
	struct node* returned = NULL;
	if(a==NULL && b==NULL)
		return NULL;
	else if(a!=NULL && b==NULL)
	{
		returned = sorted_merge(a->next, NULL);
		move_node(&returned, &a);
	}
	else if(a==NULL && b!=NULL)
	{
		returned = sorted_merge(NULL, b->next);
		move_node(&returned, &b);
	}
	else if(a!=NULL && b!=NULL)
	{
		if(b->data <= a->data)
		{
			returned = sorted_merge(a, b->next);
			b->next == NULL;
			move_node(&returned,&b);
			//a->next == NULL;
			//move_node(&returned,&a);
		}
		else
		{
			returned = sorted_merge(a->next, b);
			a->next == NULL;
			move_node(&returned,&a);
			//b->next == NULL;
			//move_node(&returned,&b);
		}
	}
	return returned;
}
Exemple #17
0
void merge_sort(struct node** headRef)
{
	struct node* part1 = NULL;
	struct node* part2 = NULL;

	print_list(*headRef);
	if((*headRef)->next == NULL)
		return;
	front_back_split(*headRef,&part1,&part2);
	if(part1!=NULL)
		merge_sort(&part1);
	print_list(part1);
	if(part2!=NULL)
		merge_sort(&part2);
	print_list(part2);
	*headRef = sorted_merge(part1,part2);
	print_list(*headRef);
	return;
}
Exemple #18
0
int main(void)
{
    FILE* f;
    char str[MS];
    int k;
    struct mylist* head=NULL, *head1 = NULL, *headr=NULL, *headr1=NULL, *kwn, *cir, *cir1;
    struct anime* mas=NULL, *mas1=NULL, *masr = NULL, *masr1 = NULL;
    setlocale(0, "russian");

    printf("Тестирование функции readline из mystruct.c\n");
    f = fopen("testread/test1.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "bbbb") == 0 && k == 4)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    fclose(f);

    f = fopen("testread/test2.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "bbbbb") == 0 && k == 5)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    fclose(f);

    f = fopen("testread/test3.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "aba aba b") == 0 && k == 9)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    fclose(f);

    f = fopen("testread/test4.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "trata trata") == 0 && k == 11)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    fclose(f);

    f = fopen("testread/test5.txt","r");
    k = freadline(f, str, MS);
    if (strcmp(str, "") == 0 && k == 0)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    fclose(f);

    f = fopen("testread/test6.txt","r");
    k = freadline(f, str, 3);
    if (strcmp(str, "AA") == 0 && k == 2)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED!");
    fclose(f);



    printf("\n\nТестирование функции readdate из mystruct.c\n");
    mas = malloc(sizeof(struct anime));
    f = fopen("testread/test_d.txt","r");
    readdate(f, mas);
    if (strcmp(mas->name, "One Punch Man") == 0 && strcmp(mas->stud, "Madhouse") == 0 && mas->year==2015)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    readdate(f, mas);
    if (strcmp(mas->name, "Overlord") == 0 && strcmp(mas->stud, "Mad House") == 0 && mas->year==2014)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    fclose(f);
    free(mas);  mas = NULL;


    printf("\n\nТестирование функции readfile из mystruct.c\n");
    mas = NULL;
    f = fopen("testread/test_d.txt","r");
    mas = readfile(f, &k);
    if (strcmp(mas->name, "One Punch Man") == 0 && strcmp(mas->stud, "Madhouse") == 0 && mas->year==2015 && k == 2 &&
        strcmp((mas+1)->name, "Overlord") == 0 && strcmp((mas+1)->stud, "Mad House") == 0 && (mas+1)->year==2014)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    fclose(f);
    mas = NULL;
    f = fopen("testread/test_de.txt","r");
    mas = readfile(f, &k);
    if (mas == NULL && k == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    fclose(f);


    printf("\n\nТестирование функции formlist из mystruct.c\n");
    mas = NULL;
    f = fopen("testread/test_d.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (strcmp(((struct anime*) head->date)->name, "One Punch Man") == 0 && strcmp(((struct anime*) head->date)->stud, "Madhouse") == 0 && ((struct anime*) head->date)->year==2015 && k == 2 &&
        strcmp(((struct anime*) head->next->date)->name, "Overlord") == 0 && strcmp(((struct anime*) head->next->date)->stud, "Mad House") == 0 && ((struct anime*) head->next->date)->year==2014)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    fclose(f);

    mas = NULL;
    head = NULL;
    f = fopen("testread/test_de.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (head == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    fclose(f);

    printf("\n\nТестирование функции namecmp из mystruct.c\n");
    mas = malloc(2*sizeof(struct anime));
    snprintf(mas->name, MS, "AAA");
    snprintf(mas->stud, MS, "A");
    mas->year = 0;
    snprintf((mas+1)->name, MS, "BBB");
    snprintf((mas+1)->stud, MS, "A");
    mas->year = 0;
    head = create_mylist(mas);
    head1 = create_mylist(mas+1);
    if (namecmp(head, head1) < 0)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    snprintf(mas->name, MS, "CCC");
    if (namecmp(head, head1) > 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    snprintf((mas+1)->name, MS, "CCC");
    if (namecmp(head, head1) == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    mas = NULL;
    free(head);
    head = NULL;
    free(head1);
    head1 = NULL;

    printf("\n\nТестирование функции lookup из exfunclo.c\n");
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;

    head1 = create_mylist(mas1);
    f = fopen("testlook/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    free(head1);
    head1 = create_mylist(mas1);
    f = fopen("testlook/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) != NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);
    f = fopen("testlook/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    if (lookup(head, head1, namecmp) == NULL)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    free_list(head); head = NULL;
    head = NULL;
    free(mas);  mas = NULL;
    mas = NULL;
    fclose(f);

    printf("\n\nТестирование функции insert из exfunclo.c\n");
    f = fopen("testins/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testins/test11.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    insert(&head, head1);
    int s = 0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    free_list(headr); headr = NULL;
    free(masr);  masr = NULL;
    free(mas1);  mas1 = NULL;
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;
    head1 = create_mylist(mas1);
    f = fopen("testins/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testins/test12.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    kwn = head->next->next->next;
    insert(&kwn, head1);
    s = 0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free_list(head); head = NULL;
    free(mas);  mas = NULL;
    free_list(headr); headr = NULL;
    free(masr);  masr = NULL;
    free(mas1);  mas1 = NULL;

    f = fopen("testins/test2.txt","r");
    mas1 = malloc(sizeof(struct anime));
    snprintf(mas1->name, MS, "AAABBB");
    snprintf(mas1->stud, MS, "AAA");
    mas1->year = 24;
    head1 = create_mylist(mas1);
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    insert(&head, head1);
    if (allcmp(head, head1) == 0 && head->next == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas1);  mas1 = NULL;
    free(mas);  mas = NULL;
    free(head); head = NULL;

    printf("\n\nТестирование функции remove_duplicates из exfunclo.c\n");
    f = fopen("testrmd/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test1r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    if (head == headr && head == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test2r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test3r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testrmd/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testrmd/test4r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    remove_duplicates(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    printf("\n\nТестирование функции front_back_split из exfunclo.c\n");
    headr = front_back_split(head);
    if (head == NULL && headr == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");

    f = fopen("testfbs/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test1r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test1r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testfbs/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test2r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test2r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testfbs/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testfbs/test3r1.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    f = fopen("testfbs/test3r2.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    head1 = front_back_split(head);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    for (cir = head1, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;


    printf("\n\nТестирование функции sorted_merge из exfunclo.c");
    f = fopen("testmerge/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test1a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test1r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 1 - OK!");
    else
        printf("\nTest 1 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test2a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test2r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    free_list(head1); head1 = NULL;
    free(masr);  masr = NULL;
    //free_list(headr);
    headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test3a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test3r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test4a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test4r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test5a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test5r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test6.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test6a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test6r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 6 - OK!");
    else
        printf("\nTest 6 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test7.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test7a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test7r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 7 - OK!");
    else
        printf("\nTest 7 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    f = fopen("testmerge/test8.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testmerge/test8a.txt", "r");
    mas1 = readfile(f, &k);
    head1 = formlist(head1, mas1, k);
    fclose(f);
    f = fopen("testmerge/test8r.txt", "r");
    masr1 = readfile(f, &k);
    headr1 = formlist(headr1, masr1, k);
    fclose(f);
    headr = sorted_merge(head, head1, namecmp);
    s=0;
    for (cir = headr, cir1 = headr1; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    s += cir || cir1;
    if (s == 0)
        printf("\nTest 8 - OK!");
    else
        printf("\nTest 8 - FAILED");
    free(mas);  mas = NULL;
    //free_list(head);
    head = NULL;
    free(mas1); mas1 = NULL;
    //free_list(head1);
    head1 = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    free(masr1);  masr1 = NULL;
    free_list(headr1); headr1 = NULL;

    printf("\n\nТестирование функции merge_sort из exfunclo.c\n");
    f = fopen("testbacisort/test1.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test1r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("Test 1 - OK!");
    else
        printf("Test 1 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test2.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test2r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 2 - OK!");
    else
        printf("\nTest 2 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test3.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test3r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 3 - OK!");
    else
        printf("\nTest 3 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test4.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test4r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head = merge_sort(head, namecmp);
    s=0;
    if (head == headr && head == NULL)
        printf("\nTest 4 - OK!");
    else
        printf("\nTest 4 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;

    f = fopen("testbacisort/test5.txt","r");
    mas = readfile(f, &k);
    head = formlist(head, mas, k);
    fclose(f);
    f = fopen("testbacisort/test5r.txt", "r");
    masr = readfile(f, &k);
    headr = formlist(headr, masr, k);
    fclose(f);
    head =  merge_sort(head, namecmp);
    s=0;
    for (cir = head, cir1 = headr; cir && cir1; cir = cir->next, cir1 = cir1->next)
        s += allcmp(cir, cir1);
    if (s == 0 && cir == NULL && cir1 == NULL)
        printf("\nTest 5 - OK!");
    else
        printf("\nTest 5 - FAILED!");
    free(mas);  mas = NULL;
    free_list(head); head = NULL;
    free(masr);  masr = NULL;
    free_list(headr); headr = NULL;
    return 0;
}
Exemple #19
0
void main()
{
	struct node* head = BuildOneTwoThree();
	print_list(head);
//	int count = Count(head, 2);
//	int count = get_nth(head, 0);
//	printf("Count is: %d\n", count);
//	delete_list(&head);
//	print_list(head);
//	int i = 0;
//	int length = list_length(head);
/*	for(i = 0; i<length; i++)
	{
		int c = pop(&head);
		printf("%d\t", c);
	}
	printf("\n");
*/
//	insert_nth(&head,0,10);
//	print_list(head);
#ifdef SORTED_INSERT
	struct node* new_node1 = (struct node*) malloc(sizeof(struct node));
	new_node1->data = 13;
	sorted_insert(&head, new_node1);
	print_list(head);
	struct node* new_node2 = (struct node*) malloc(sizeof(struct node));
	new_node2->data = 0;
	sorted_insert(&head, new_node2);
	print_list(head);
	struct node* new_node3 = (struct node*) malloc(sizeof(struct node));
	new_node3->data = 8;
	sorted_insert(&head, new_node3);
	print_list(head);
	struct node* new_node4 = (struct node*) malloc(sizeof(struct node));
	new_node4->data = 4;
	sorted_insert(&head, new_node4);
	print_list(head);
	struct node* new_node5 = (struct node*) malloc(sizeof(struct node));
	new_node5->data = 30;
	sorted_insert(&head, new_node5);
	print_list(head);
#endif

#ifdef INSERT_SORT
	insert_nth(&head,0,10);
	insert_nth(&head,1,20);
	insert_nth(&head,3,14);
	insert_nth(&head,4,78);
	insert_nth(&head,1,32);
	insert_nth(&head,3,42);
	print_list(head);
	insert_sort(&head);
	print_list(head);
#endif

#ifdef APPEND	
	struct node* head1 = BuildOneTwoThree();
	insert_nth(&head1,0,10);
	print_list(head1);
	append(&head,&head1);
	print_list(head);
	print_list(head1);
#endif

#ifdef SPLIT_FRONT_BACK
	struct node* new_node5 = (struct node*) malloc(sizeof(struct node));
	new_node5->data = 30;
	sorted_insert(&head, new_node5);
	struct node* new_node6 = (struct node*) malloc(sizeof(struct node));
	new_node6->data = 10;
	sorted_insert(&head, new_node6);

	struct node* head1 = NULL;
	struct node* head2=NULL;
	front_back_split(head, &head1, &head2);
	print_list(head1);
	print_list(head2);
#endif
#ifdef REMOVE_DUPLICATES
	struct node* new_node5 = (struct node*) malloc(sizeof(struct node));
	new_node5->data = 2;
	sorted_insert(&head, new_node5);
	struct node* new_node6 = (struct node*) malloc(sizeof(struct node));
	new_node6->data = 1;
	sorted_insert(&head, new_node6);
	print_list(head1);
	remove_duplicates(head1);
	print_list(head1);
#endif

#ifdef MOV_NODE
	struct node* a = BuildOneTwoThree();
	struct node* b = BuildOneTwoThree();
	print_list(a);
	print_list(b);

	move_node(&a,&b);
	print_list(a);
	print_list(b);
#endif

#ifdef ALTERNATING_SPLIT
	struct node* a = NULL;
	struct node* b = NULL;
	alternating_split(head, &a, &b);
	print_list(a);
	print_list(b);
#endif

#ifdef SHUFFLE_MERGE
	struct node* a = BuildOneTwoThree();
	struct node* b = BuildOneTwoThree();
	print_list(a);
	print_list(b);
	struct node* c = shuffle_merge(a,b);
	print_list(c);
#endif

#ifdef SORTED_MERGE
	struct node* a = BuildOneTwoThree();
	struct node* b = BuildOneTwoThree();
	struct node* new_node1 = (struct node*) malloc(sizeof(struct node));
	new_node1->data = 13;
	sorted_insert(&a, new_node1);
	struct node* new_node2 = (struct node*) malloc(sizeof(struct node));
	new_node2->data = 0;
	sorted_insert(&b, new_node2);
	struct node* new_node3 = (struct node*) malloc(sizeof(struct node));
	new_node3->data = 8;
	sorted_insert(&a, new_node3);
	struct node* new_node4 = (struct node*) malloc(sizeof(struct node));
	new_node4->data = 4;
	sorted_insert(&a, new_node4);
	struct node* new_node5 = (struct node*) malloc(sizeof(struct node));
	new_node5->data = 30;
	sorted_insert(&b, new_node5);
	print_list(a);
	print_list(b);
	struct node* c = sorted_merge(a,b);
	print_list(c);
#endif

#ifdef MERGE_SORT
	insert_nth(&head,0,10);
	insert_nth(&head,1,20);
	insert_nth(&head,3,14);
	insert_nth(&head,4,78);
	insert_nth(&head,1,32);
	insert_nth(&head,3,42);
	print_list(head);
	merge_sort(&head);
//	print_list(head);
#endif
	insert_nth(&head,0,10);
	insert_nth(&head,1,20);
	insert_nth(&head,3,14);
	insert_nth(&head,4,78);
	insert_nth(&head,1,32);
	insert_nth(&head,3,42);
	print_list(head);
	reverse(&head);
	print_list(head);

}