Write a C function to add two-polynomials represented as circular list with header node

C function to add Two-polynomials represented as Circular list with Header node

Program:-

NODE poly_add(NODE head1, NODE head2, NODE head3)
{
	NODE a, b;
	int coeff;
	a = head1->link;
	b = head2->link;
	while (a != head1 && b != head2)
	{
		if (a->expon == b->expon)
		{
			coeff = a->coeff + b->coeff;
			if (coeff != 0)
				head3 = attach(coeff, a->expon, head3);
			a = a->link;
			b = b->link;
		}
		else if (a->expon > b->expon)
		{
			head3 = attach(a->coeff, a->expon, head3);
			a = a->link;
		}
		else
		{
			head3 = attach(b->coeff, b->expon, head3);
			b = b->link;
		}
	}

	while (a != head1)
	{
		head3 = attach(a->coeff, a->expon, head3);
		a = a->link;
	}

	while (b != head2)
	{
		head3 = attach(b->coeff, b->expon, head3);
		b = b->link;
	}

	return head3;
}

Leave a Reply

Your email address will not be published. Required fields are marked *