Hello can anybody please enlighten me why is it that this is causing stackdump? I can't seem to note that the point/position of the pointer is off.
Any help would be appreciated thank you!
Code:
/* Inserts a node in front of given index node */
void InsertAt(struct Node** ppList, int value, int index)
{
/* Temp pointer to store new node */
struct Node* newNode = NULL;
/* Serves as our runner to find the last node */
struct Node* curr = NULL;
/* Check ppList is valid */
if (ppList == NULL)
{
/* Invalid, return don't do anything */
return;
}
/* Allocate the Node memory via malloc */
newNode = (struct Node*)malloc(sizeof(struct Node));
/* Failed allocation, can't do anything else */
if (newNode == NULL)
return;
if (!index)
{
/* Assign the values to the new Node */
newNode->value = value;
/* New node should point to whatever the head had */
newNode->next = *ppList;
/* Replace the head with the new node */
*ppList = newNode;
return;
}
/* Allocate the Node memory via malloc */
curr = (struct Node*)malloc(sizeof(struct Node));
/* Failed allocation, can't do anything else */
if (curr == NULL)
return;
/* Do a loop to increment the node */
while (index--)
{
/* Check condition to determine the curr node points to */
if (!index)
{
/* Assign the values to the new Node */
curr->value = value;
/* New node should point to whatever the head had */
curr->next = *ppList;
/* Replace the head with the new node */
*ppList = curr;
}
else
ppList = &(*ppList)->next;
}
}