Getting a runtime error (while using del function)

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

struct node
{
int data;
struct node *link;
}*first;

void create(int A[],int n)
{
struct node *head,*t;
int i;

  first=(struct node *)malloc(sizeof(struct node ));
  first->data=A[0];
  first->link=first;
  head=first;

for(i=1;i<n;i++)
{
t=(struct node*)malloc(sizeof(struct node));
t->data=A[i];
t->link=head->link;
head->link=t;
head=t;

}

}
void display(struct node *p)
{
do
{
printf("%d\t\n",p->data);
p=p->link;

}while(p!=first);

}
void insert(struct node *h,int index,int x,int length)
{
int i;struct node *t;
if(index<0||index>length)
{
return ;

}
if (index==0)
{
    t=(struct node *)malloc(sizeof(struct node));
    t->data=x;
    if(first==NULL)
    {
        first=t;
        first->link=first;
    }
    else
    {
        while(h->link!=first)
        {
            h=h->link;
        }
        h->link=t;
        t->link=first;
        first=t;

    }

}
else
{
    for(i=0;i<index-2;i++)
    {
        h=h->link;

    }
    t=(struct node *)malloc(sizeof(struct node ));
    t->data=x;
    t->link=h->link;
    h->link=t;

}

}

int del(struct node *p,int index)
{
struct node *q;
int i,x;
if(index==1)
{

while(p->link!=first)
      {


    p=p->link;
      }
x=first->data;
if(first==p)
{
    free(first);
    first==NULL;

}
else
{
p->link=first->link;
free(first);
first=first->link;

}

}
else
{
for(i=0;i<index-2;i++)
{
p=p->link;
}
q=p->link;
x=q->data;
free(q);
q=NULL;

}
return x;
}

int main()
{
int A[]={2,3,4,5,6};
create(A,5);
// display(first);
// del(first,3);
// insert(first,3,56,5);

// display(first);
del(first,4);
display(first);
return 0;
}

1 Like

Please send the link to your code. These posting of codes usually make threads very lengthy. Copy paste the URL of your code in the forum, that would be better.

1 Like

https://ideone.com/5rGrTC
getting an error of type SIGSEGV

1 Like