Results 1 to 4 of 4
  1. #1
    faid's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Location
    Somewhere over the rainbow
    Posts
    547
    Reputation
    40
    Thanks
    63
    My Mood
    In Love

    Question 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;
    	}
    
    
    }

  2. #2
    ElonTusk's Avatar
    Join Date
    Feb 2020
    Gender
    female
    Posts
    33
    Reputation
    10
    Thanks
    6
    @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:
    Code:
    struct Node *curr = *pplist;
    while (curr != NULL && (curr->next->index != index)) {
        curr = curr->next;
    }
    if (curr) {
        struct Node *nxt = curr->next;
        curr->next = newNode;
        newNode->next = nxt;
    }
    Let me know if this help.
    Last edited by ElonTusk; 02-16-2020 at 07:31 PM.

  3. The Following User Says Thank You to ElonTusk For This Useful Post:

    faid (02-18-2020)

  4. #3
    faid's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Location
    Somewhere over the rainbow
    Posts
    547
    Reputation
    40
    Thanks
    63
    My Mood
    In Love
    Quote Originally Posted by ElonTusk View Post
    @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:
    Code:
    struct Node *curr = *pplist;
    while (curr != NULL && (curr->next->index != index)) {
        curr = curr->next;
    }
    if (curr) {
        struct Node *nxt = curr->next;
        curr->next = newNode;
        newNode->next = nxt;
    }
    Let me know if this help.
    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.

  5. #4
    ElonTusk's Avatar
    Join Date
    Feb 2020
    Gender
    female
    Posts
    33
    Reputation
    10
    Thanks
    6
    Quote Originally Posted by faid View Post
    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.

Similar Threads

  1. Replies: 0
    Last Post: 06-12-2010, 04:03 PM
  2. is this the right code for (Anti-Kick) New Addresses
    By floris12345! in forum Visual Basic Programming
    Replies: 6
    Last Post: 01-28-2008, 06:33 PM
  3. is this the right code?
    By ownedplox in forum WarRock - International Hacks
    Replies: 5
    Last Post: 01-21-2008, 01:35 PM
  4. Hello... does someone give me a code of the promo code?
    By mackus in forum WarRock - International Hacks
    Replies: 13
    Last Post: 05-06-2007, 08:31 AM
  5. I dont understand the tutorial
    By ValconGSX in forum WarRock - International Hacks
    Replies: 1
    Last Post: 01-16-2006, 04:11 PM

Tags for this Thread