user968000
user968000

Reputation: 1843

Segmentation fault while getting the size of Linked list

I am getting segmenation fault when calling the function getLength. I edited the code, now I am getting length as 0 instead of 5.

#include <stdio.h>
#include <stdlib.h>

   node *headptr;
   node *topptr;

typedef struct node
{
    int value;
    struct node *nextPtr; 

}node;

void initializeLinkedList(node *headptr, node *topptr)
{
    int i=0;
    headptr = (node*)malloc(sizeof(node));
    topptr = (node*)malloc(sizeof(node));
    topptr = headptr;


    headptr->value=i;
    headptr->nextPtr = (node*)malloc(sizeof(node));
    for(i=1;i<5;i++)
   {

       headptr = headptr->nextPtr ;
       headptr->value=i;
       headptr->nextPtr=(node*)malloc(sizeof(node));
       printf("val is %p \n ",  *headptr);
   }

 headptr->nextPtr = NULL;


}

int getLength(node *topptr)
{
    int i=0;
    node* local;
    local = topptr;
    while(local!=NULL)
    {

     local=local->nextPtr;
     i++;
    }
    return i;

}


int main()
{

initializeLinkedList(headptr,topptr);
printf("val is %d \n",   getLength(topptr));
return 0;

}

Upvotes: 0

Views: 166

Answers (3)

BLUEPIXY
BLUEPIXY

Reputation: 40145

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int value;
    struct node *nextPtr; 
} node;

void initializeLinkedList(node **top, node **rear){
    int i=0;
    node *local;

    *top = (node*)malloc(sizeof(node));
    local = *top;
    local->value=i;
    local->nextPtr = NULL;
    for(i=1;i<5;++i){
        local->nextPtr = (node*)malloc(sizeof(node));
        local = local->nextPtr;
        local->value = i;
        local->nextPtr = NULL;
    }
    *rear = local;
}

int getLength(node *np){
    int i;
    for(i=0;np!=NULL;++i, np = np->nextPtr)
        ;//printf("debug:%d\n", np->value);
    return i;
}

int main(void){
    node *top, *rear;
    initializeLinkedList(&top, &rear);
    printf("length is %d \n", getLength(top));
    return 0;
}

Upvotes: 0

sethi
sethi

Reputation: 1889

void initializeLinkedList(node *headptr, node *topptr)

change it to

void initializeLinkedList(node *headptr, node** topptr)

and change your code accordingly...

There are lot of other issues too...

When you need a pointer just define the pointer don't allocate memory and overwrite the poiter..

If i have to code it

void initializeLinkedList( node **topptr)
    {
         int i=0;
         node* headptr = (node*)malloc(sizeof(node));
         headptr->value=i;

        *topptr = headptr;


        for(i=1;i<5;i++)
       {

           headptr->nextPtr = (node*)malloc(sizeof(node)); 
           headptr->nextPtr->value=i;
           headptr->nextPtr->nextPtr=NULL;
           headptr=headptr->nextPtr;

       }

    }



    int main()
    {
    node* topptr;
    initializeLinkedList(&topptr);
    printf("val is %d \n",   getLength(topptr));
    return 0;
    }

Upvotes: 1

Emanuele Paolini
Emanuele Paolini

Reputation: 10172

initializeLinkedList does not modify the variables headptr and topptr defined in main (pass by value). Hence the variable passed to getLength contains junk.

Upvotes: 1

Related Questions