Wednesday, 13 June 2012

Polynomial evaluation using Linked List in C







Here are the instructions of the program:

Recall that a polynomial is a sum of terms in the form, f(x) = a0x0 + a1x1 + a2x2 + … + anxn. You are to write a program that will manipulate polynomials. The application should instantiate polynomials as linked lists of terms. The application should allow the user to do the following

1. Build a polynomial by entering it term by term. The function must first check to see if there is a term with that exponent in the polynomial (find). If there is not, the function will insert the term. If there is, the function will first remove the existing term and then insert the new term.

2. Input polynomials to produce a new polynomial. The function will be passed the polynomial and return their sum.

3. Print out the polynomial in the form
term1 + term2 + … + termN

4. Evaluate the polynomial that was the result of the given polynomial expression  a value from the user

5. Clear the polynomials and start again

The user should be able to repeat the process above as many times as he or she wants.

Now I have finished the implementation and the header files:

/*
* Evaluation of polynomial using singly linked list
* Author :Ravi Kiran 
* on:5 April 2011
—————–STEPS——————————–
* Create :- Inputs : Integer coefficient and power of x
* Evaluation of polynomial
* Display the polynomial
*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>


struct node *create();
float evaluate(struct node *);
float traversal(struct node *);


struct node
{
  int coeff, expo;
  char variable;
  struct node *next;
};


void main()
 {
  struct node *h;


  clrscr();
  h = create();
  printf("\nThe Result of your polynomial is :%f",traversal(h));


  getch();
 }


struct node *create()
{
  int again;
  struct node *temp, *first, *newnode;
  first=(struct node*)malloc(sizeof(struct node));


  printf("\nEnter coefficient and exponent of the node:\n");
  scanf("%d %s %d",&(first->coeff),&(first->variable),&(first->expo));
  first->next = NULL;
  temp = first;


  printf("\nDo you want to create next node?(YES=1)/(NO=0)\n");
  scanf("%d",&again);
  while(again)
   {
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter coefficient,variable and exponent of next node\n");
scanf("%d %s %d",&newnode->coeff,&newnode->variable,&newnode->expo);
newnode->next = NULL;


temp->next=newnode;
temp=newnode;


printf("\nDo you want to create next node?(YES=1)/(NO=0)\n");
scanf("%d",&again);
   }


return(first);
}


float traversal(struct node *first)
{
struct node *temp;
float sum,x;
sum=0;
temp=first;
while(temp!= NULL)
{
x=evaluate(temp);
temp=temp->next;
sum=sum+x;
}
return(sum);
}




float evaluate(struct node *temp)
{
float value, z;
int exp = temp->expo;
int coeff = temp->coeff;


printf("\nEnter the value of variable %c : ",temp->variable);
scanf("%f",&value);


z = powl(value,exp);


return(z * coeff);
}

5 comments:

  1. Really great post, I simply unearthed your site and needed to say that I have truly appreciated perusing your blog entries.
    AWS Training in pune
    AWS Online Training

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. It’s hard to come by well-informed people for this topic, however, you seem like you know what you’re talking about! Thanks
    Gadgets

    ReplyDelete
  4. I do agree your blog for quiz programming concepts, which is very helpful to grow up your knowledge. keep sharing more.
    aws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery

    ReplyDelete
  5. The post is written in very a good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.


    Java training in Chennai

    Java training in Bangalore

    Java training in Hyderabad

    Java Training in Coimbatore

    Java Online Training

    ReplyDelete