Frage

ma fellow geek, i've created single linked list algorithm, but i didnt see it work properly :

#include <stdio.h>
#include <stdlib.h>

/*
 * 
 */
  typedef struct _data_konfirm
    {
        char *id;
        char *buff;
        int v;

        struct _data_konfirm *next;
    }data_konfirm;

 void data_add(data_konfirm **data,char *id,char *buff,int v)
{
    data_konfirm *tmp = (data_konfirm*)malloc(sizeof(data_konfirm));
    tmp->id             = strdup(id);
    tmp->buff           = strdup(buff);
    tmp->v = v;

    tmp->next = *data;
    *data = tmp;    
}

void data_destroy(data_konfirm **data)
{
        data_konfirm *b = *data;
        free(b->id);
        free(b->buff);
        free(b);
}

void data_del(data_konfirm **data,char *id)
{


    data_konfirm *tmp = *data, *b = NULL;

    if(!strcmp(tmp->id,id))
    {
        b = tmp;
        tmp = tmp->next;
        data_destroy(&b);              
        return;
    }


    while(tmp->next->next != NULL)
    {
        if(!strcmp(tmp->next->id,id))
        {
            b = tmp->next;
            tmp->next = b->next;
            data_destroy(&b);

            return;
        }

        tmp = tmp->next;
    }

    if(!strcmp(tmp->next->id,id))
    {
        b = tmp->next;
        data_destroy(&b);
        tmp->next = NULL;
        return;        
    }


}

void inform_show_all(data_konfirm **data)
{        
    data_konfirm *tmp = *data;
    int i = 0;
    while(tmp)
    {
        printf("id              -> %s\n",tmp->id);
        printf("buff            -> %s\n",tmp->buff);        
        i++;
        tmp = tmp->next;
    }

    printf("jumlah data : %d\n",i);
}


int main(int argc, char** argv) {
    data_konfirm *a = NULL;

    data_add(&a,"1","silit",2);
    data_del(&a,"1");
    data_add(&a,"2","wwww",1);
    data_add(&a,"3","zzz",2);
    data_add(&a,"4","huaaa",2);

    data_del(&a,"3");

    inform_show_all(&a);
    return (EXIT_SUCCESS);
}

if i run these code, the loop will never end, whats wrong then ?

thank in advance...

War es hilfreich?

Lösung

In function data_del substitute

b = tmp;
tmp = tmp->next;

with

b = *data;
*data = (*data)->next;

Here is a picture of the situation:

*data -> +---+           +---+
  tmp -> | | | -> ... -> | | |
    b -> +---+           +---+

The statement tmp = tmp->next will not update the pointer *data.

Andere Tipps

I don't like just providing code, but that data_del function is a mess. Try this:

void data_del(data_konfirm **data,char *id)
{
    while( *data != NULL )
    {
        data_konfirm* tmp = *data;
        if( strcmp( tmp->id, id ) == 0 )
        {
            *data = tmp->next;  /* remove tmp from list */
            data_destroy( &tmp );
            break;
        }

        data = &tmp->next;
    }
}

Fair warning, I haven't actually tested this.

If your data_del function needs to delete the list's head node, it won't set the head to the next element, so you will be left with a pointer to a freed node. What you should do is set *data to the new head in that case.

You also have a problem with data_del not working with empty lists: it will try accessing a null pointer in that case.

This bit at the start of data_del can't be right:

if(!strcmp(tmp->id,id))
{
    b = tmp;
    tmp = tmp->next;
    data_destroy(&b);              
    return;
}

When you enter this block you delete the first item in the list but don't update the head pointer.

I would not be surprised for there to be other problems in the code.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top