C++ program to add two polynomials maintained using Linked Lists

#include <iostream.h>
class poly
{
	private :
		struct polynode
		{
			float coeff ;
			int exp ;
			polynode *link ;
		} *p ;
	public :
		poly( ) ;
		void poly_append ( float c, int e ) ;
		void display_poly( ) ;
		void poly_add( poly &l1, poly &l2 ) ;
		~poly( ) ;
} ;
poly :: poly( )
{
	p = NULL ;
}
void poly :: poly_append ( float c, int e )
{
	polynode *temp = p ;
	if ( temp == NULL )
	{
		temp = new polynode ;
		p = temp ;
	}
	else
	{
		while ( temp -> link != NULL )
			temp = temp -> link ;
		temp -> link = new polynode ;
		temp = temp -> link ;
	}
	temp -> coeff = c ;
	temp -> exp = e ;
	temp -> link = NULL ;
}
void poly :: display_poly( )
{
	polynode *temp = p ;
	int f = 0 ;
 
	cout << endl ;
	while ( temp != NULL )
	{
		if ( f != 0 )
		{
			if ( temp -> coeff > 0 )
				cout << " + " ;
			else
				cout << " " ;
		}
		if ( temp -> exp != 0 )
			cout << temp -> coeff << "x^" << temp -> exp ;
		else
			cout << temp -> coeff ;
		temp = temp -> link ;
		f = 1 ;
	}
}
void poly :: poly_add ( poly &l1, poly &l2 )
{
	polynode *z ;
	if ( l1.p == NULL && l2.p == NULL )
		return ;
	polynode *temp1, *temp2 ;
	temp1 = l1.p ;
	temp2 = l2.p ;
	while ( temp1 != NULL && temp2 != NULL )
	{
		if ( p == NULL )
		{
			p = new polynode ;
			z = p ;
		}
		else
		{
			z -> link = new polynode ;
			z = z -> link ;
		}
		if ( temp1 -> exp < temp2 -> exp )
		{
			z -> coeff = temp2 -> coeff ;
			z -> exp = temp2 -> exp ;
			temp2 = temp2 -> link ;
		}
		else
		{
			if ( temp1 -> exp > temp2 -> exp )
			{
				z -> coeff = temp1 -> coeff ;
				z -> exp = temp1 -> exp ;
				temp1 = temp1 -> link ;
			}
			else
			{
				if ( temp1 -> exp == temp2 -> exp )
				{
					z -> coeff = temp1 -> coeff + temp2 -> coeff ;
					z -> exp = temp1 -> exp ;
					temp1 = temp1 -> link ;
					temp2 = temp2 -> link ;
				}
			}
		}
	}
	while ( temp1 != NULL )
	{
		if ( p == NULL )
		{
			p = new polynode ;
			z = p ;
		}
		else
		{
			z -> link = new polynode ;
			z = z -> link ;
		}
		z -> coeff = temp1 -> coeff ;
		z -> exp = temp1 -> exp ;
		temp1 = temp1 -> link ;
	}
	while ( temp2 != NULL )
	{
		if ( p == NULL )
		{
			p = new polynode ;
			z = p ;
		}
		else
		{
			z -> link = new  polynode ;
			z = z -> link ;
		}
		z -> coeff = temp2 -> coeff ;
		z -> exp = temp2 -> exp ;
		temp2 = temp2 -> link ;
	}
	z -> link = NULL ;
}
poly :: ~poly( )
{
	polynode *q ;
	while ( p != NULL )
	{
		q = p -> link ;
		delete p ;
		p = q ;
	}
}
void main( )
{
	poly p1 ;
	p1.poly_append ( 1.4, 5 ) ;
	p1.poly_append ( 1.5, 4 ) ;
	p1.poly_append ( 1.7, 2 ) ;
	p1.poly_append ( 1.8, 1 ) ;
	p1.poly_append ( 1.9, 0 ) ;
	cout << "\nFirst polynomial:" ;
	p1.display_poly( ) ;
	poly p2 ;
	p2.poly_append ( 1.5, 6 ) ;
	p2.poly_append ( 2.5, 5 ) ;
	p2.poly_append ( -3.5, 4 ) ;
	p2.poly_append ( 4.5, 3 ) ;
	p2.poly_append ( 6.5, 1 ) ;
	cout << "\nSecond polynomial:" ;
	p2.display_poly( ) ;
	poly p3 ;
	p3.poly_add ( p1, p2 ) ;
	cout << "\nResultant polynomial: " ;
	p3.display_poly( ) ;
}

12 Responses to “C++ program to add two polynomials maintained using Linked Lists”

  1. craigdattons

    hi there is this the type your after please email me
    craigy

    Reply
    • Christina

      26 mai 2011, 17h16 [aseudo]1fotech[/pseudo]i€œnâ€tavait en aucun cas vocation à devenir une entreprise de musique ou de cinéma“[/citation]Tout est dis ici … Facebook sort totalement de son premier but : le réseau social. Si ça continue à ce rythme, ils vont concurrencer google dans tous les domaines -_-

      Reply
  2. michaelmastonie

    well grant here is the web address address , they have 30% disc at the moment , tell them michael recommened you

    Reply
  3. melonybereford

    hi kevin if you are still in need of them i think this is the web address
    filling address ,ring them if you need them in a hurry ,say martin netsims said you would sort him out

    Reply
    • Interesting polisbisities, and will probably allow people to take the steps they need to get help at their own pace. (Like WebMD, only more concrete!) I hope this is the start of something great. Like Fallen Angels, I think it will help with stigma; it might also be good for, say, children of people with these disorders who are just beginning to manifest symptoms.

      Reply
    • That’s just rude, how could he do that? First he interrupts the library, back-chats the librarian and treats Emma like a scab! That’s just mean!! Poor Emma, but I bet this will backfire on Pat The Rat’s face once Jason tells Emma he likes her! (I can tell theirs a little somin’ somin’! ) But shame, I also felt like crying for the poor girl, please send her my wishes Nikki. Tell her “Lulu’s here, don’t worry girl. He be tr&!#ini#8217;p&p8221;

      Reply
  4. – Oh, Brianna! She is so lovely. Love her locks=) We haven’t chatted in so long! Would love to catch up in the new year – maybe a coffee or lunch date. How did 4 years go by so quickly? This is Lilah in a few months, to;o#8230&Four sounds so much bigger than three…

    Reply

Leave a Reply