problem in while loop.

I think there's a problem in my condition inside the while loop. my linked list is supposed to traverse until the two pointers to two different linkedlists become null. but after one of the pointers becomes null, the program exits the loop. why is that? please help. thanks!

here's my code for the while loop.

[code]
while( ptr!=NULL && ptr2!= NULL){

if(ptr3 == NULL){

if( ptr -> exponent != ptr2 -> exponent ){
if (ptr -> exponent > ptr2 -> exponent){
temp -> exponent = ptr -> exponent;
temp -> coeff = ptr -> coeff;
ptr = ptr -> next;
}else if( ptr2 -> exponent > ptr -> exponent){
temp -> exponent = ptr2 -> exponent;
temp -> coeff = ptr2 -> coeff;
ptr2 = ptr2 -> next;

}
}else{
temp -> exponent = ptr -> exponent;
temp -> coeff = (ptr -> coeff) + (ptr2 -> coeff);


ptr = ptr -> next;
ptr2 = ptr2 -> next;
}
temp -> next = head3;
head3 = temp;

}else if(ptr == NULL){

temp -> next = NULL;
temp -> exponent = ptr2 -> exponent;
temp -> coeff = ptr2 -> coeff;
ptr3 -> next = temp;
ptr2 = ptr2 -> next;
}else if(ptr2 == NULL){

temp -> next = NULL;
temp -> exponent = ptr -> exponent;
temp -> coeff = ptr -> coeff;
ptr3 -> next = temp;
ptr = ptr -> next;
}else{
if( ptr -> exponent != ptr2 -> exponent){
if (ptr -> exponent > ptr2 -> exponent ){
temp -> exponent = ptr -> exponent;
temp -> coeff = ptr -> coeff;
ptr = ptr -> next;

}else if( ptr2 -> exponent > ptr -> exponent ){
temp -> exponent = ptr2 -> exponent;
temp -> coeff = ptr2 -> coeff;
ptr2 = ptr2 -> next;
}
}else{
temp -> exponent = ptr -> exponent;
temp -> coeff = (ptr -> coeff) + (ptr2 -> coeff);

ptr = ptr -> next;
ptr2 = ptr2 -> next;
}

temp -> next = NULL;
ptr3 -> next = temp;
}

}[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories