Search code examples
csortingstructsingly-linked-listfunction-definition

Delete and insert a node linked list C


I'm trying to write a program, in which i have to delete and insert elements in a linked list. I have problems when I delete and after that insert. In particular if i print the list after delete-insert, i print a loop. And also, if i delete the first element i think i delete the entire list.

These are the two function, but i don't know where it's the problem (sorry for the italian comments)

void delete_peer(struct node_peer** head, int value){
    struct node_peer* temp;
    struct node_peer* prev;
    temp = head;
    
    if(temp != NULL && temp->port == value){
        head = temp->next;
        free(temp);
        temp = NULL;
        return;
    }
    
    while(temp != NULL && temp->port != value){
        prev = temp;
        temp = temp->next;
    }

    //peer non presente
    if(temp == NULL)
        return;

    prev->next = temp->next;
    free(temp);
    temp = NULL;
};

void insert_sort(struct node_peer* head, struct node_peer* new_peer){
    
    //se la lista è vuota, o il peer ha il n° di porta piu piccolo tra quelli presenti
    //allora inseriamo il nodo in testa
    if(*head == NULL || (*head)->port >= new_peer->port) {
        new_peer->next = *head;
        *head = new_peer;
        printf("Inserimento in testa\n");
        return;
    }
    
    //altrimenti, inserisco all'interno della lista, oridnata per n° di porta dei peers
    struct node_peer* current = *head;
    while(current->next != NULL && current->next->port < new_peer->port)
        current = current->next;

    new_peer->next = current->next;
    current->next = new_peer;   
    printf("Inserimento in mezzo\n");
    return;
};


EDIT: I pass the *head instead of **head

Solution

  • Your code shall not compile because at least in the function delete_peer

    void delete_peer(struct node_peer** head, int value){
    

    in this statement

    temp = head;
    

    there are used operands of different types (struct node_peer* and struct node_peer**) and there is no implicit conversion from one type to another.

    Or within the function declared like

    void insert_sort(struct node_peer* head, struct node_peer* new_peer){
    

    again for example the expression in the if statement

    if(*head == NULL || (*head)->port >= new_peer->port) {
    

    is incorrect because at least the operand *head (according to the declaration of the parameter head) does not have a pointer type.

    The function delete_peer can be defined the following way

    int delete_peer( struct node_peer **head, int value )
    {
        while ( *head && ( *head )->port != value )
        {
            head = &( *head )->next;
        }
    
        int success = *head != NULL;
    
        if ( success )
        {
            struct node_peer *tmp = *head;
            *head = ( *head )->next;
            free( tmp );
        }
    
        return success;
    }
    

    and the function should be called at least like

    delete_peer( &head, value );
    

    The function insert_sort can be defined like

    void insert_sort( struct node_peer **head, struct node_peer *new_peer )
    {
        while ( *head && !( new_peer->port < ( *head )->port ) )
        {
            head = &( *head )->next;
        }
    
        new_peer->next = *head;
        *head = new_peer;
    }
    

    The function insert_sort should be called like

    insert_sort( &head, new_peer );