OPERATIONS ON SINGLY CIRCULAR LINKED LIST (SCLL) WITH HEADER NODES: REPRESENT AND EVALUATE A POLYNOMIAL.

 If Screen is half, please rotate the Mobile then it is full program available

OPERATIONS ON SINGLY CIRCULAR LINKED LIST (SCLL) WITH HEADER NODES: REPRESENT AND EVALUATE A POLYNOMIAL.

9. Design, Develop and Implement a Program in C for the following   operations on Singly Circular Linked List (SCLL) with header nodes. 

    a. Represent and Evaluate a Polynomial.


#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;


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 HornerRule ( int x )

{

    ListPointer temp;

    int p;

    

    p = head1->link->data.coef;    

    temp = head1->link->link; 


    while ( temp != NULL )

    {

        p = x * p + temp->data.coef;

        temp = temp->link;

    }


    return p;

}



int main ( void )

{

    Record data;

    int nRecords;

    int choice;

    int x, i;

    

    printf("\n.. POLYNOMIAL EVALUATION ..\n");

    

    head1 = 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 ( "\nEnter the value for x: ?\b");

    scanf ( "%d", &x );


    printf ( "\n\nPoly - 1\n" );

    display ( head1 );


    printf ( "\n\nResult: %d", HornerRule ( x ) );


    printf("\n\n");


    //getch();

    return 0;

}




/*    

.. POLYNOMIAL EVALUATION ..


Terms of Polynomial 1: 5


Give 1st record details one by one


COEF    EXPON

2      4

-1      3

3       2

1       1

-5      0


Enter the value for x: 3


Poly - 1

 2x^4 -1x^3 + 3x^2 + 1x^1 -5x^0


Result: 160


*/


Post a Comment

Previous Post Next Post

Contact Form