gur111 icon

merge employee lists - Itay PVT

gur111 | PRO | 07/02/19 06:29:55 PM UTC | 0 ⭐ | 554 👁️ | Never ⏰ | []
C |

1.18 KB

|

None

|

0 👍

/

0 👎

#include <stdlib.h>
#include <stdio.h>
 
typedef struct {
    char * name;
    char * admin;
    char * phone;
 
}* Class;
 
typedef struct emp {
    int id;
    int xp;
    Class class;
    struct emp next;
}* Emp;
 
Emp merge_lists(Emp h1, Emp h2) {
    Emp h, c1, c2, prv;
    int is_first_run = 1;
 
    if (h1 == NULL)
        return h2;
    else if (h2 == NULL)
        return h1;
 
    h = h1->id < h2->id ? h1 : h2;
    c1 = h1;
    while (c1->next != h1)
        ;
    prv = c1;
    c1 = h1;
 
    do {
        if (!is_first_run && c2 == h2)
            break;
        while (c2->id < c1->id) {
            if (!is_first_run && c2 == h2)
                break;
            is_first_run = 0;
            prv->next = c2;
            prv = c2;
            c2 = c2->next;
            prv->next = c1;
        }
        prv = c1;
        c1 = c1->next;
    } while (c1 != h1);
 
    if (c2 != h2) {
        prv->next = c2;
        while (c2->next != h2)
            ;
        c2->next = h;
    } else {
        while (c1->next != h1)
            ;
        c1->next = h;
    }
 
    return h;
}
 
 
 
Emp add(Emp list, Emp to_add){
    Emp c = list, tmp;
    while(c->next != list){
        if(c->next->id < to_add->id){
            tmp = c->next;
            c->next = to_add;
            to_add->next = tmp;
        }
        c = c->next;
    }
    c->next = to_add;
    to_add->next = c;
    return to_add->id < c->id ? to_add : c;
}

Comments