Frage

I created a linked list with ten nodes. I want to get the address of the first node I created in the linked list. I am getting incorrect address value for the first node. This is the code which I wrote :

#include "singlyLinkedList.h"
#include <stddef.h> //for NULL
#include <stdlib.h> //for malloc
#include <stdio.h> //for printf

node *pNode;

void CreateLinkedList()
{
    int i;
    pNode = (node*)malloc(sizeof(node)); //create space for first node []
    for(i=0;i<10;i++)
    {
        pNode->element = i; //enter value [0]
        printf("Value is %d addr is %p\n",pNode->element,pNode);
        pNode->nextPtr = (node*)malloc(sizeof(node)); //[0]->[]->NULL
        pNode = pNode->nextPtr;
    }
    pNode->nextPtr=NULL;
}

//Function to get first node address
void GetFirstNodeAddress()
{
    pNode = pNode-10*(sizeof(node));
    printf("\n *** Value is %p",pNode);
}


int main()
{
    CreateLinkedList();
    GetFirstNodeAddress();
}
War es hilfreich?

Lösung

You assume that the 10 malloc's you do will result in 10 consecutive addresses. You handle it as if it is an array, yet the elemenents of the linked list are independent.

The way these linked lists usually work is that you initialise them and keep the first pointer to be returned later. Then grow more elements on the tail. You can not walk backwards in a single linked list.

I gave a code example in an earlier post today

Andere Tipps

You don't use pointer arithmetic in linked list implementations, at least not trivial ones. And you're populating your list incorrectly. You need to retain the head pointer. This is one such way to do it:

// Note: do NOT invoke on a non-empty pNode list
void CreateLinkedList()
{
    node **pp = &pNode; 
    int i;

    for (i=0;i<10;++i)
    {
        *pp = malloc(sizeof(**pp));
        (*pp)->element = i;
        pp = &(*pp)->nextPtr;
    }
    *pp = NULL;
}

How It Works

Just like pNode is a pointer to a node, pp is a pointer to a node *. During the lifetime of the loop pp always holds the address of the next pointer about to be filled with a new node allocation. After the allocation and node setup, pp is filled with the address of the nextPtr pointer of the node just-created. This continues until the end of the fill. At that point the pointer addressed by pp is the tail nextPtr and should be null-terminated (which is what *pp = NULL does). Step through this code in a debugger to see better how it works.

Personally I would pass the address of the head pointer as a parameter, but this is one immediate way to get your code running.

Try printf("\n *** Value is %p",(void *) &pNode);. & is typically the easiest way to get pointer addresses

also from what I know: pNode = pNode-10*(sizeof(node)); is incorrect.

when you are doing pointers, they point to areas in memory that are not necessarily contiguous (following in order in memory). Meaning you could have one pointer pointing to memory blocks 5-10, which also has a pointer to memory blocks 83-88. If you want to get the first node, create a pNode rootNode and assign your first created pNode to that. Then you can traverse it in a while loop as well as printing out the address as listed above or whatever else you please.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top