C Program to add two polynomials maintained as linked lists. #include <> /* structure representing a node of a linked list. The node can store term of a polynomial */ void poly_append ( struct polynode **, float, int ) ; void main( ) first = second = total = NULL ; /* empty linked lists */ poly_append ( &first, 1.4, 5 ) ; clrscr( ) ; poly_append ( &second, 1.5, 6 ) ; printf ( “\n\n” ) ; /* draws a dashed horizontal line */ poly_add ( first, second, &total ) ; /* adds a term to a polynomial */ /* creates a new node if the list is empty */ /* create new nodes at intermediate stages */ /* assign coefficient and exponent */ /* displays the contents of linked list representing a polynomial */ printf ( “\b\b\b ” ) ; /* erases the last colon */ /* adds two polynomials */ /* if both linked lists are empty */ /* traverse till one of the list ends */ /* store a term of the larger degree polynomial */ /* assign remaining terms of the first polynomial to the result */ /* assign coefficient and exponent */ /* assign remaining terms of the second polynomial to the result */ /* assign coefficient and exponent */ z – > link = NULL ; /* assign NULL at end of resulting linked list */ read more: http://www.c-cplusplus.com/add-two-polynomials-maintained-as-linked-lists
#include <>
#include <>
struct polynode
{
float coeff ;
int exp ;
struct polynode *link ;
} ;
void display_poly ( struct polynode * ) ;
void poly_add ( struct polynode *, struct polynode *, struct polynode ** ) ;
{
struct polynode *first, *second, *total ;
int i = 0 ;
poly_append ( &first, 1.5, 4 ) ;
poly_append ( &first, 1.7, 2 ) ;
poly_append ( &first, 1.8, 1 ) ;
poly_append ( &first, 1.9, 0 ) ;
display_poly ( first ) ;
poly_append ( &second, 2.5, 5 ) ;
poly_append ( &second, -3.5, 4 ) ;
poly_append ( &second, 4.5, 3 ) ;
poly_append ( &second, 6.5, 1 ) ;
display_poly ( second ) ;
printf ( “\n” ) ;
while ( i++ < 79 )
printf ( "-" ) ;
printf ( "\n\n" ) ;
display_poly ( total ) ; /* displays the resultant polynomial */
}
void poly_append ( struct polynode **q, float x, int y )
{
struct polynode *temp ;
temp = *q ;
if ( *q == NULL )
{
*q = malloc ( sizeof ( struct polynode ) ) ;
temp = *q ;
}
else
{
/* traverse the entire linked list */
while ( temp - > link != NULL )
temp = temp – > link ;
temp – > link = malloc ( sizeof ( struct polynode ) ) ;
temp = temp – > link ;
}
temp – > coeff = x ;
temp – > exp = y ;
temp – > link = NULL ;
}
void display_poly ( struct polynode *q )
{
/* traverse till the end of the linked list */
while ( q != NULL )
{
printf ( “%.1f x^%d : “, q – > coeff, q – > exp ) ;
q = q – > link ;
}
}
void poly_add ( struct polynode *x, struct polynode *y, struct polynode **s )
{
struct polynode *z ;
if ( x == NULL && y == NULL )
return ;
while ( x != NULL && y != NULL )
{
/* create a new node if the list is empty */
if ( *s == NULL )
{
*s = malloc ( sizeof ( struct polynode ) ) ;
z = *s ;
}
/* create new nodes at intermediate stages */
else
{
z – > link = malloc ( sizeof ( struct polynode ) ) ;
z = z – > link ;
}
if ( x – > exp <> exp )
{
z – > coeff = y – > coeff ;
z – > exp = y – > exp ;
y = y – > link ; /* go to the next node */
}
else
{
if ( x – > exp > y – > exp )
{
z – > coeff = x – > coeff ;
z – > exp = x – > exp ;
x = x – > link ; /* go to the next node */
}
else
{
/* add the coefficients, when exponents are equal */
if ( x – > exp == y – > exp )
{
/* assigning the added coefficient */
z – > coeff = x – > coeff + y – > coeff ;
z – > exp = x – > exp ;
/* go to the next node */
x = x – > link ;
y = y – > link ;
}
}
}
}
while ( x != NULL )
{
if ( *s == NULL )
{
*s = malloc ( sizeof ( struct polynode ) ) ;
z = *s ;
}
else
{
z – > link = malloc ( sizeof ( struct polynode ) ) ;
z = z – > link ;
}
z – > coeff = x – > coeff ;
z – > exp = x – > exp ;
x = x – > link ; /* go to the next node */
}
while ( y != NULL )
{
if ( *s == NULL )
{
*s = malloc ( sizeof ( struct polynode ) ) ;
z = *s ;
}
else
{
z – > link = malloc ( sizeof ( struct polynode ) ) ;
z = z – > link ;
}
z – > coeff = y – > coeff ;
z – > exp = y – > exp ;
y = y – > link ; /* go to the next node */
}
}
Labels
- ajax (1)
- Assembly Programming (3)
- Books on C++ (6)
- C Programs (35)
- Challenging Question (1)
- Class (1)
- Complete Script (1)
- Computer Graphics (1)
- CSS (2)
- Datastructure (6)
- Download (3)
- e-books (8)
- Errors (1)
- FTP (2)
- HTML (3)
- Interview Questions (8)
- Introduction (1)
- Methods (2)
- OOP (1)
- Operator Overloading (1)
- Operators (1)
- Others (5)
- PHP (4)
- PHP Errors (1)
- PHP Script (1)
- Programming (3)
- Question Bank (6)
- Short Questions (1)
- SQL (1)
- Tips (1)
- Tricks (1)
- Tricky Programs in C (2)
- Useful Scripts (1)
- WAP (1)
- Web (3)
- XHTML (1)
Add two Polynomials maintained as Linked Lists
Sunday, August 29, 2010Posted by Xofth at 10:32 AM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment