Do you receive the Programmer's Heaven newsletter? If not, why not subscribe?

Beginner C/C++

Moderators: Lundin
Number of threads: 4916
Number of posts: 15971

This Forum Only
Post New Thread

Report
problem in while loop. Posted by cmsc on 28 Nov 2009 at 9:01 PM
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.

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;
		}
		
	}




 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.