Okay this is my code for deleting a node from a linked list.
vec_store
holds seq
and size
. Variable seq
holds the vectors and a pointer.
For some reason, the else if(i<s->size-1)
doesn't work which is the last condition.
Any1 can solve the problem? By the way this is C code.
void delete_vec(vec_store s, int i)
{
if (i<0 || s->size-1<i)
{
printf("Cannot delete vector because index %d is out of bounds\n",i);
}
else if (i==0)
{
node temp;
temp = s->seq;
s->seq = s->seq->next;
s->size--;
free(temp);
}
else if(i==s->size-1)
{
node temp1, temp2;
//temp1 = malloc(sizeof (node));
temp2 = malloc(sizeof (node));
temp1=s->seq;
if(temp1->next==NULL)
{
free(temp1);
s->seq=NULL;
s->size--;
printf("s->size-1\n");
}
else
{
while (temp1->next!=NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
free(temp1);
temp2->next=NULL;
s->size--;
printf("s->size-1 2\n");
}
}
else if(i<s->size-1)
{
node temp1,temp2;
int j;
temp1=s->seq;
temp2 = malloc(sizeof (struct node_record));
for(j=0;j<=i-1;j++)
{
temp2=temp1;
temp1 = temp1->next;
}
free(temp1);
temp2->next=NULL;
s->size--;
}
}