LinkedList

Hi folks!

I'm in trouble with a LinkedList of my own...
This list is ended with a NULL Cell.

typedef struct _cell
{
int value;
struct _cell *next;
} Cell;

typedef Cell* LinkedList;

LinkedList init_LC()
{
return NULL;
}

int add_LC(LinkedList *l, int e)
{
Cellu *tmp;

if((tmp=(Celle*)malloc(sizeof(Cell))) == NULL)
return 0;

tmp->value=e;
tmp->next=*l;
*l=tmp;
return 1;
}

The problem is in the following function.
A segmentation fault happens when the last value of the list is removed...

void delete_LC(LinkedList *l, int e)
{
LinkedList p = *l, previous = NULL;

while(p!=NULL)
{
if(p->value == e)
{
previous->next = p->next;
free(p);
return;
}
previous=p;
p=p->next;
}
}

Do you have an idea?

Comments

  • : Hi folks!
    :
    : I'm in trouble with a LinkedList of my own...
    : This list is ended with a NULL Cell.
    :
    : typedef struct _cell
    : {
    : int value;
    : struct _cell *next;
    : } Cell;
    :
    : typedef Cell* LinkedList;
    :
    : LinkedList init_LC()
    : {
    : return NULL;
    : }
    :
    : int add_LC(LinkedList *l, int e)
    : {
    : Cellu *tmp;
    :
    : if((tmp=(Celle*)malloc(sizeof(Cell))) == NULL)
    : return 0;
    :
    : tmp->value=e;
    : tmp->next=*l;
    : *l=tmp;
    : return 1;
    : }
    :
    : The problem is in the following function.
    : A segmentation fault happens when the last value of the list is removed...
    :
    : void delete_LC(LinkedList *l, int e)
    : {
    : LinkedList p = *l, previous = NULL;
    :
    : while(p!=NULL)
    : {
    : if(p->value == e)
    : {
    : previous->next = p->next;
    : free(p);
    : return;
    : }
    : previous=p;
    : p=p->next;
    : }
    : }
    :
    : Do you have an idea?
    :

    hi there,
    I have made some corrections in your program. Pls try to execute this.
    Best of luck for your sucess.

    typedef struct _cell
    {
    int value;
    struct _cell *next;
    }CELL;

    CELL *LinkedList;

    CELL init_LC()
    {
    return NULL;
    }

    int add_LC(CELL *l, int e)
    {
    CELL *tmp;
    if((tmp=(CELL*)malloc(sizeof(CELL))) == NULL)
    return 0;

    tmp->value=e;
    tmp->next=*l;
    *l=tmp;

    return 1;
    }


    void delete_LC(CELL *l, int e)
    {
    CELL *p = l;
    CELL previous = NULL;

    while(p!=NULL)
    {
    if(p->value == e)
    {
    previous->next = p->next;
    free(p);
    return;
    }
    previous=p;
    p=p->next;
    }
    }




    A winner is not the one who never fails , but the one who never quits.
    Sumit Shah.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion