Transfer linked list values from one linked list to another in C

60 views Asked by At

I am doing the next program, there is a system of ships (naves), that travel to planets (planetas) to give deliveries (paquetes). This are the structs I have created for that.

Naves* generando_naves(int ID){
    Naves* nave = calloc(1, sizeof(Naves));

    *nave = (Naves){
        .ID = ID, 
/*Linked list of deliveries*/
        .paquetes = NULL,
/*counter of deliveries to do*/
        .pedidos = 0
    };
    return nave;

}
/* Deliveries struct*/
Paquetes* generando_paquetes(int ID,int IDplanet, int IDnaves){
    Paquetes* paquete = calloc(1, sizeof(Paquetes));
    *paquete = (Paquetes){
        .ID = ID,
/*Chosen planet*/
        .Planet = IDplanet,
/*Chosen ship*/
        .Ship = IDnaves,
        .next = NULL,
        .before = NULL
    };
    return paquete;
}

I have to do this next:

You will have to transfer the deliveries of the indicated planet between the indicated ships, so that the ship with the least amount of pending deliveries from that planet is transferred to the ship with the least amount of pending deliveries from that planet. of that planet will be transferred to the ship with more deliveries. If the two ships have the same amount of packets, ship 2 must pass the deliveries, ship 2 must pass the orders to ship 1. Finally you must return the total number of pending orders

So I have to traspass from one linked list (from ship1) to another (ship2)

This is what I have so far:

int moveFromFirstToSecond(Naves* firstNave, Naves* secondNave, bool (*match)(const Paquetes*)) {
    Paquetes **current = &(firstNave->paquetes), *next = NULL;
    int counter = 0;

    while (*current != NULL) {
        if (match(*current)) {
          counter++;
            // Match found, move package
            next = (*current)->next; // Store next package
            (*current)->next = secondNave->paquetes; // Link package to the beginning of second nave's list
            secondNave->paquetes = *current; // Update second nave's head
            *current = next; // Move current to next in first nave's list
        } else {
            current = &((*current)->next); // Move to the next package in the list
        }
    }
  return counter;
}

int nave1ID, nave2ID, planetaID;
  fscanf(input_file, "%d %d %d", &nave1ID, &nave2ID, &planetaID);

  Naves* nave1 = naves[nave1ID];
  Naves* nave2 = naves[nave2ID];

  bool matchCondition(const Paquetes* paquete) {
    return paquete->Planet == planetaID;
  }
  if (nave1 >= nave2){
    int counter = moveFromFirstToSecond(nave1, nave2, matchCondition);
    fprintf(output_file, "PAQUETES TRANSFERIDOS: %d\n", counter);
    nave1->pedidos = nave1->pedidos - counter;
    nave2->pedidos = nave2->pedidos + counter;
    fprintf(output_file, "    NAVE %d: %d PEDIDOS\n    NAVE %d: %d PEDIDOS\n", nave1ID, nave1->pedidos, nave2ID, nave2->pedidos);
  } else {
    
    int counter = moveFromFirstToSecond(nave2, nave1, matchCondition);
    fprintf(output_file, "PAQUETES TRANSFERIDOS: %d\n", counter);
    nave1->pedidos = nave1->pedidos + counter;
    nave2->pedidos = nave2->pedidos - counter;
    fprintf(output_file, "    NAVE %d: %d PEDIDOS\n    NAVE %d: %d PEDIDOS\n", nave1ID, nave1->pedidos, nave2ID, nave2->pedidos);
  }
  

But the transfering is wrong, when checking the output I have the wrong counters. TO make it clear:

Paquetes transferidos == transfered deliveries Nave == ship

My output:

PAQUETES TRANSFERIDOS: 4
    NAVE 1: 7 PEDIDOS
    NAVE 3: 7 PEDIDOS

REPORTE-PEDIDOS
    NAVE 0
        PEDIDO 9642 CON PLANETA 0
        PEDIDO 8611 CON PLANETA 4
        PEDIDO 1544 CON PLANETA 5
    NAVE 1
        PEDIDO 2940 CON PLANETA 1
        PEDIDO 11712 CON PLANETA 1
        PEDIDO 15790 CON PLANETA 1
        PEDIDO 6325 CON PLANETA 1
        PEDIDO 2369 CON PLANETA 1
        PEDIDO 189 CON PLANETA 5
        PEDIDO 12240 CON PLANETA 2
    NAVE 2
        PEDIDO 13626 CON PLANETA 4
        PEDIDO 5945 CON PLANETA 3
        PEDIDO 14475 CON PLANETA 4
        PEDIDO 515 CON PLANETA 0
        PEDIDO 8441 CON PLANETA 6
        PEDIDO 780 CON PLANETA 5
        PEDIDO 7699 CON PLANETA 5
    NAVE 3
        PEDIDO 5160 CON PLANETA 3
        PEDIDO 14820 CON PLANETA 0
        PEDIDO 6752 CON PLANETA 2
        PEDIDO 5179 CON PLANETA 5
        PEDIDO 9643 CON PLANETA 3
        PEDIDO 10328 CON PLANETA 6
        PEDIDO 3457 CON PLANETA 0
    NAVE 4
        PEDIDO 6032 CON PLANETA 4
        PEDIDO 8459 CON PLANETA 4
        PEDIDO 1339 CON PLANETA 4
TOTAL DE PEDIDOS: 27

When it should be:

PAQUETES TRANSFERIDOS: 1
    NAVE 1: 2 PEDIDOS
    NAVE 3: 12 PEDIDOS
REPORTE-PEDIDOS
    NAVE 0
        PEDIDO 9642 CON PLANETA 0
        PEDIDO 8611 CON PLANETA 4
        PEDIDO 1544 CON PLANETA 5
    NAVE 1
        PEDIDO 189 CON PLANETA 5
        PEDIDO 12240 CON PLANETA 2
    NAVE 2
        PEDIDO 5945 CON PLANETA 3
        PEDIDO 14475 CON PLANETA 4
        PEDIDO 515 CON PLANETA 0
        PEDIDO 8441 CON PLANETA 6
        PEDIDO 780 CON PLANETA 5
        PEDIDO 7699 CON PLANETA 5
        PEDIDO 13626 CON PLANETA 4
    NAVE 3
        PEDIDO 6325 CON PLANETA 1
        PEDIDO 5160 CON PLANETA 3
        PEDIDO 14820 CON PLANETA 0
        PEDIDO 6752 CON PLANETA 2
        PEDIDO 15790 CON PLANETA 1
        PEDIDO 11712 CON PLANETA 1
        PEDIDO 5179 CON PLANETA 5
        PEDIDO 9643 CON PLANETA 3
        PEDIDO 10328 CON PLANETA 6
        PEDIDO 3457 CON PLANETA 0
        PEDIDO 2940 CON PLANETA 1
        PEDIDO 2369 CON PLANETA 1
    NAVE 4
        PEDIDO 6032 CON PLANETA 4
        PEDIDO 8459 CON PLANETA 4
        PEDIDO 1339 CON PLANETA 4
TOTAL DE PEDIDOS: 27

The list should be actualized, deleted from the original ship and moved to the new list.

0

There are 0 answers