Understanding the underlying code given causing STACKDUMP!
Posts 1–4 of 4 · Page 1 of 1
Understanding the underlying code given causing STACKDUMP!
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;
}
}
@faid,
I understand everything until the part where have a while loop ( /* Do a loop to increment the node */).
What's the purpose of this? What are you trying to do with this while loop?
If I understand correctly, your Node structure has an index associated with it? so something like:
Code:
struct Node {
int value;
int index;
struct Node *next;
}
Am I correct? Why are you doing it this way? A linked list is not an array. If you want to insert a new node before a particular node, you can iterate through the list and find the node before the target node, and then insert the new node after the current node.
Example:
You want to insert new node with vlaue =7, before the index=12:
@faid,
I understand everything until the part where have a while loop ( /* Do a loop to increment the node */).
What's the purpose of this? What are you trying to do with this while loop?
If I understand correctly, your Node structure has an index associated with it? so something like:
Code:
struct Node {
int value;
int index;
struct Node *next;
}
Am I correct? Why are you doing it this way? A linked list is not an array. If you want to insert a new node before a particular node, you can iterate through the list and find the node before the target node, and then insert the new node after the current node.
Example:
You want to insert new node with vlaue =7, before the index=12:
Hey man just read your example and explanation, i seem to understand it more now.
Although my node only consist of this;
Code:
struct Node {
int value;
struct Node *next;
}
but it should be a similar application.
Thank you.
Originally Posted by faid
Hey man just read your example and explanation, i seem to understand it more now.
Although my node only consist of this;
Code:
struct Node {
int value;
struct Node *next;
}
but it should be a similar application.
Thank you.
Glad I could help. So you basically have just a "regular" basic singly linked list. When you try to insert a node before a particular node, just iterate through the list for that node and then insert the newNode when you find the old node, just like the example above.
Don't hesitate to DM if you have any more questions.