I have created a function which will return the base address of the linked list. It is always returning the last node address(null) instead of the first node.
#include<stdio.h>
#include<stdlib.h>
typedef struct _LinkedList
{
int data;
struct LinkedList *nextVal;
}LinkedList;
//function to create a linked list
LinkedList* createLL()
{
LinkedList *ll;
ll=malloc(sizeof(LinkedList));
ll->nextVal =malloc(sizeof(LinkedList));
int i;
int count = 5;
for(i=0;i<5;i++)
{
ll->nextVal = malloc(sizeof(LinkedList));
ll->data = i;
printf("LL data value %d address val %p\n ",ll->data,ll->nextVal);
}
//last node should point to null
ll->nextVal=NULL;
printf("======================\n");
return ll;
}
int main()
{
LinkedList *basePtr;
int i;
basePtr=createLL();
for(i=0;i<5;i++)
{
printf("LL data value %d address val %p\n ",basePtr->data,basePtr->nextVal);
}
return 0;
}