C linked list segmentation fault (core dumped) error : <p>I am new to data structure and having some trouble implementing double linked list. After taking all the input compiler showing segmentation fault (core dumped). Cannot find the mistake. 
I searched for core dump errors as well as segmentation fault but it didnt help. 
Here is my code:</p>

<pre><code>struct node
{
    int data;
    struct node *lptr,*rptr;
};

struct node *head;

void create(struct node *head)
{
    struct node *newp,*ptr;
    int num,n;

    printf("Enter the no. of elements you want to enter :\n");
    scanf("%d",&amp;n);

    printf("Enter the elements:\n");

    while(n)
    {
        scanf("%d",&amp;num);
        n--;

        if(head==NULL)
        {
            newp=(struct node *)malloc(sizeof(struct node));
            newp-&gt;lptr=NULL;
            newp-&gt;data=num;
            newp-&gt;rptr=NULL;
            head=newp;
            printf("Head created\n");
            ptr=head;

        }

        else
        {
            while(ptr-&gt;rptr)
            {
                ptr=ptr-&gt;rptr;
            }

            ptr-&gt;rptr=(struct node *)malloc(sizeof(struct node));
            ptr-&gt;rptr-&gt;lptr=ptr;
            ptr-&gt;data=num;
            ptr-&gt;rptr-&gt;rptr=NULL;
            ptr=ptr-&gt;rptr;
            printf("node inserted\n");
        }
    }
}   

void display(struct node *head)
{
    struct node *ptr;
    ptr=head;
    while(ptr-&gt;rptr)
    {
        printf("%d\t",ptr-&gt;data );
        ptr=ptr-&gt;rptr;
    }
}

void main()
{   
    head=NULL;

    create(head);
    display(head);
}   
</code></pre>
