If Screen is half, please rotate the Mobile then it is full program available
9. Design, Develop and Implement a Program in C for the following operations on Singly Circular Linked List (SCLL) with header nodes
b. Find the sum of two polynomials POLY1(x) and POLY2(x) and store the result in POLYSUM(x)
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct Polynomial
{
int coef;
int expon;
}Record;
typedef struct Node *ListPointer;
typedef struct Node
{
Record data;
ListPointer link;
}Node;
ListPointer head1;
ListPointer head2;
ListPointer head3;
Record getNextRecord ( void )
{
Record data;
scanf ( "%d", &data.coef );
scanf ( "%d", &data.expon );
return data;
}
ListPointer getNode ( Record data )
{
ListPointer temp;
temp = ( ListPointer ) malloc ( sizeof ( *temp ) );
if ( temp == NULL )
{
printf ("\nAllocation Failed\n");
getchar (); //getch();
exit(EXIT_FAILURE);
}else
{
temp->data = data;
temp->link = NULL;
}
return temp;
}
void insert(ListPointer targetNode, ListPointer newNode)
{
newNode->link = targetNode->link;
targetNode->link = newNode;
}
void getLastAndPrevNode( ListPointer head, ListPointer *prev, ListPointer *last)
{
ListPointer cur, trail;
cur = trail = head;
if ( head->link != NULL )
{
cur = head->link;
trail = NULL;
while ( cur->link != NULL )
{
trail = cur;
cur = cur->link;
}
}
*prev = trail;
*last = cur;
}
void addRear ( ListPointer head, Record data )
{
ListPointer prev, last, temp;
temp = getNode ( data );
getLastAndPrevNode(head, &prev, &last);
insert(last, temp);
}
void display ( ListPointer head )
{
ListPointer temp = head->link;
printf ( " %dx^%d", temp->data.coef, temp->data.expon );
temp = temp->link;
while ( temp != NULL )
{
if ( temp->data.coef > 0 )
{
printf(" +");
}
printf ( " %dx^%d", temp->data.coef, temp->data.expon );
temp = temp->link;
}
}
int COMPARE ( int a, int b )
{
if ( a < b )
{
return -1;
}
if ( a == b )
{
return 0;
}
return 1;
}
void addPoly ( ListPointer a, ListPointer b )
{
Record data;
int sum;
a = a->link; /* skip header node for a and b */
b = b->link;
while ( a && b )
{
switch ( COMPARE ( a->data.expon, b->data.expon ) )
{
case -1: addRear ( head3, b->data );
b = b->link;
break;
case 0 : sum = a->data.coef + b->data.coef;
data.coef = sum;
data.expon = a->data.expon;
if ( sum )
{
addRear ( head3, data );
}
a = a->link;
b = b->link;
break;
case 1 : addRear ( head3, a->data );
a = a->link;
}
}
for ( ;a; a = a->link )
{
addRear ( head3, a->data );
}
for ( ;b; b = b->link )
{
addRear ( head3, b->data );
}
}
int main ( void )
{
Record data;
int nRecords;
int choice;
int i;
printf("\n.. ADDITION OF TWO POLYNOMIALS ..\n");
head1 = getNode(data);
head2 = getNode(data);
head3 = getNode(data);
printf ( "\nTerms of Polynomial 1: ?\b" );
scanf ( "%d", &nRecords );
printf ( "\nGive 1st record details one by one\n" );
printf ( "\nCOEF\tEXPON\n" );
for ( i = 0; i < nRecords; ++ i )
{
data = getNextRecord ();
addRear ( head1, data );
}
printf ( "\nTerms of Polynomial 2: ?\b" );
scanf ( "%d", &nRecords );
printf ( "\nGive 2nd record details one by one\n" );
printf ( "\nCOEF\tEXPON\n" );
for ( i = 0; i < nRecords; ++ i )
{
data = getNextRecord ();
addRear ( head2, data );
}
printf ( "\n\nPoly - 1\n" );
display ( head1 );
printf ( "\n\nPoly - 2\n" );
display ( head2 );
addPoly ( head1, head2 );
printf ( "\n\nPoly - 3\n" );
display ( head3 );
printf("\n\n");
//getch();
return 0;
}
.. ADDITION OF TWO POLYNOMIALS ..
Terms of Polynomial 1: 3
Give 1st record details one by one
COEF EXPON
3 14
2 8
1 0
Terms of Polynomial 2: 3
Give 2nd record details one by one
COEF EXPON
8 14
-3 10
10 6
Poly - 1
3x^14 + 2x^8 + 1x^0
Poly - 2
8x^14 -3x^10 + 10x^6
Poly - 3
11x^14 -3x^10 + 2x^8 + 10x^6 + 1x^0
.. ADDITION OF TWO POLYNOMIALS ..
Terms of Polynomial 1: 3
Give 1st record details one by one
COEF EXPON
3 3
2 2
1 1
Terms of Polynomial 2: 3
Give 2nd record details one by one
COEF EXPON
3 3
2 2
1 1
Poly - 1
3x^3 + 2x^2 + 1x^1
Poly - 2
3x^3 + 2x^2 + 1x^1
Poly - 3
6x^3 + 4x^2 + 2x^1