Okay This is the code for insering a node into a linked list.
vec_store
holds seq and size. Variable seq holds the vectors and a pointer. and vec_mag
takes magnitude of vectors.
For some reason, the (vec_mag(v)<=vec_mag(temp2->next->data))
doesn't work which is the last condition.
Any1 can solve the problem? By the way this is C code.
vector last_vec(vec_store s){
node temp3;
temp3=s->seq;
while (temp3->next!=NULL)
{temp3 = temp3->next;
}
return temp3->data;
}
void insert_vec(vec_store s, vector v){
node temp1,temp2,temp4;
int i;
temp1 = malloc(sizeof (struct node_record));
if(s->seq==NULL){
s->seq=temp1;
temp1->next=NULL;
temp1->data=v;
s->size++;
printf("1\n");
}
else if(vec_mag(v)<=vec_mag(s->seq->data)){
s->size++;
temp2=s->seq;
temp1->data=v;
temp1->next=temp2;
s->seq=temp1;
printf("2\n");
}
else if(vec_mag(v)>=vec_mag(last_vec(s)))
{ s->size=s->size+1;
temp4=s->seq;
while (temp4->next!=NULL)
{temp4 = temp4->next;
}
temp1->next=NULL;
temp1->data=v;
temp4->next=temp1;
printf("3\n");
}
else{
temp2 = s->seq;
temp4 = s->seq;
for(i=0;i<s->size-1;i++){
if(vec_mag(v)<=vec_mag(temp2->next->data)){
temp1->data = v;
temp1->next = temp2->next;
temp2->next=temp1;
printf("4\n");
s->size++;
break;
}
}
}
}