Search code examples
cdata-structuresstaticnodessingly-linked-list

I want to delete a node using the deletea() function in my code but after that i enter an infinite loop in display() function?


i have tried to correct the code several times but in vain

void deletea(int x)
{
int q=0,r;
while(list[q].next!=-1 && list[q].info != x)
    q = list[q].next ;
    r = list[q].next ;
    list[q].next = list[r].next ;
    list[r].next = avail ;
    avail = r;
}
void display()
{
    int p = 0;
    while(list[p].next != -1)
    {
        printf("\n%d\t%d\t%d",p,list[p].info,list[p].next) ;
        p = list[p].next ;
    }
    printf("\n%d\t%d\t%d",p,list[p].info,list[p].next) ;
}

expect to display the remaining nodes with display() but i enter into a infinite loop --this is a static version of linked list taught to us at school which does not involve pointers


Solution

  • Adding this after the while loop might help

    if(list[q].next == -1)
    {
    printf("Deletion not possible") ;
    return ;
    }