I have to add 2 polynomial equations. Each are stored in linked lists. eq_1 and eq_2 linklists are added and the total is stored in eq_1 linklist.

But the problem is when im trying to add both equations, the 2nd for loop(eq_2) is looping once only. I mean when eq_1 loops for the 2nd time eq_2 doesn't loop anymore. I used 2 integer counters to check the loop running times. Now I want to know how I can run the second loop for every time when loop 1 is looped.
Thanks :)

for( ; eq_1 != nil; eq_1 = eq_1->tail ) { //follow up each nodes of eq1
  for( ; eq_2 != nil; eq_2 = eq_2->tail ) {
    if(eq_1->power==eq_2->power){ //if both have equal power then add up both coefficients
      newnode(eq_1,(eq_1->coefficient+eq_2->coefficient),eq_1->power);//add to eq1
    }
    else { //When both aren't same add up both to sum link list
	 eq1(eq_2->power,eq_2->coefficient,nil); //add it to eq1
     }
   }	  
}

Change the inner loop to for(eq_2 = head; eq_2!=nil; eq_2 = eq_2->tail) (substitute whatever your head pointer actually is), that way the second loop gets reset to the beginning of the second list. I think that's what you're trying to do...please clarify if it's not.

ya this is what I'm exactly looking for. Headpointer is the way to solve this, But I dont know how to implement head pointer.
so if i want to create head pointer for the eq1 how should i declare it?

Here is my eq1 code

Cons* eq1( num pow, num no, Cons* tail ) { 
   Cons* cell = (Cons*)malloc( sizeof(Cons) ); 
   assert( cell != 0 ); 
   cell->power= pow; 
   cell->coefficient=no;
   cell->tail = tail; 
   return cell; 
}

What are eq1 and eq2 equal to before your loops start?

A head pointer should have been a fundamental part of your design, what pointing to the first node of your list now? Set eq2 to whatever the beginning of your list is.

Well the first post was the sum function which is called in main to add both equations(eq1 and eq2). I'm using eq1 itself to save the total of 2 equations as I find it easier than creating a new linklist for total.

This is the sum function with its parameters:

void sum( Cons* eq_1,Cons* eq_2 )

when i call it in main:

sum(eq1(2,2,eq1(1,3,(eq1(3,2,nil)))),eq2(1,4,eq2(2,3,(3,4,nil))));

sum function is expected to add up both and print the result to screen.
Inputs are printed in the form of power;coeff,power;coeff...

I'm sending the whole c code to you as PM. :)

If you don't have a limitation, you should post the code out in the forum so that everyone can learn from it. It's against the site's policies to help through PM, anyway. :)

If you don't have a limitation, you should post the code out in the forum so that everyone can learn from it. It's against the site's policies to help through PM, anyway. :)

ok so this is the whole code of my program-

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

typedef int num; 

typedef struct Cons { 
   num power;	
   num coefficient;
   struct Cons* tail; 
} Cons;

static Cons NIL = { 0, 0,&NIL };
Cons* nil = &NIL; 

Cons* eq1( num pow, num no, Cons* tail ) { 
   Cons* cell = (Cons*)malloc( sizeof(Cons) ); 
   assert( cell != 0 ); 
   cell->power= pow; 
	cell->coefficient=no;
   cell->tail = tail; 
   return cell; 
}

Cons* eq2( num pow, num no, Cons* tail ) { 
   Cons* cell = (Cons*)malloc( sizeof(Cons) ); 
   assert( cell != 0 );
   cell->power= pow; 
	cell->coefficient=no;
   cell->tail = tail; 
   return cell; 
}

void printlist(Cons* sum){ //NOT USED
	char* sep = ""; 
	for( ; sum != nil; sum = sum->tail ) {
		printf( "%s%d;%d", sep, sum->power,sum->coefficient ); 
		sep = ","; 
	}
}
void newnode(Cons* node,num number,num exp){
	if(node->tail!=nil){
		node->coefficient=number;
		node->power=exp;
	}
	else if(node->tail==nil){
		node->coefficient=number;
		node->power=exp;
		node->tail=nil;//nil is moved  1 more node
	}
}

void sum( Cons* eq_1,Cons* eq_2 ) { 
	int count1=1,count2=1;
	char* sep = ""; 
   for( ; eq_1 != nil; eq_1 = eq_1->tail ) { //follow up each nodes of eq1
	printf("loop1 RUNS: %d times\n",count1);//TMP
	for( ; eq_2 != nil; eq_2 = eq_2->tail ) {
            printf("loop2 RUNS: %d times\n",count2);//TMP
	    if(eq_1->power==eq_2->power){ //if both have equal power
	       newnode(eq_1,(eq_1->coefficient+eq_2->coefficient),eq_1->power); 
	 }
	 else { //When both aren't same add up both to sum link list
	     eq1(eq_2->power,eq_2->coefficient,nil); //add it to eq1
	}
	 count2++;
      }
	  count1++;
	  printf( "%s%d;%d", sep, eq_1->power,eq_1->coefficient );
	  sep = ","; 
  }
}


int main( ) {
   sum(eq1(2,2,eq1(1,3,(eq1(3,2,nil)))),eq2(1,4,eq2(2,3,(3,4,nil))));
   return 0;
}

Yes, it looks good, but as I suspected you're missing a head pointer. It would be just like NIL except the "tail" of it would point to the first Cons. You can't start at NIL because NIL doesn't have a pointer to the prior node.

|h |->|  |->|  |->|N |
->can only proceed ->

(this is why doubly linked lists were invented, so you could traverse from either end).

That's probably more abstract that you bargained for, but give it a try and see how far you get.

BTW, you're missing an eq2 on line 78.

For others benefit I'm leaving the working head pointer code-

void sum( Cons* eq_1,Cons* eq_2 ) { 
	Cons* eqa1=eq_1; //Headpointer
	Cons* eqa2=eq_2; //Headpointer
	char* sep = ""; 
   for( ; eq_1 != nil; eq_1 = eq_1->tail ) { //follow up each nodes of eq1
	   for( ; eq_2 != nil; eq_2 = eq_2->tail ) {
		   if(eq_1->power==eq_2->power){
			   eq_1->coefficient=(eq_1->coefficient+eq_2->coefficient);
				eq_2->coefficient=0;
		   }
		   else { 
				newnode(eq_1,eq_2->coefficient,eq_2->power);
		   }
	   }
	   eq_2=eqa2; //Resets - Pointing to headpointer
		printf( "%s%d;%d", sep, eq_1->power,eq_1->coefficient );
		sep = ","; 
   }
}

The code is partially working, It can only calculate for equal powers. Ex- When called using

int main( ) {
	sum(eq1(2,2,eq1(1,3,(eq1(3,2,nil)))),eq2(1,4,eq2(2,3,eq2(3,4,nil))));
   return 0;
}

Both equations have 3 equal powers which will result in correct answer. But if you change any of the power in one of the equations then, it will not work correctly. As I'm working on one input line based calculation I'm no longer using this 2 equation based calculation. I will post the 1 line input based polynomial addition soon as I feel it may help others. :)

Glad you got that part of it working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.