0

I want to add a Newnode to the end of a list but it crashes and goes into a endless loop. I am attaching the function: hope to your help!!

    void AddProduct(products **head,products *newProduct)
{
    products* current=*head;
    if(current == NULL)
     {
        (*head) =(products *)malloc(1*sizeof(products));
        (*head) = newProduct;
         current=*head;
        return;
     }
    while(current->nextProduct!=NULL)
    {
        current=current->nextProduct;
    }
    //Attaching the new product to the list
    current->nextProduct=newProduct;
    newProduct->prevous=current;
    //SortList(head);
    }

1 Answers1

0

When creating a new element, you're both accepting it as an argument, and allocating it yourself inside the function. You're overwriting the newly allocated pointer with the one passed in, it doesn't make any sense.

Also, you're not setting the nextProduct field of the new element.

Finally, you're using too many casts and parentheses.

unwind
  • 391,730
  • 64
  • 469
  • 606