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
* Create :- Inputs : Integer coefficient and power of x
* Evaluation of polynomial
* Display the polynomial

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;

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


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");
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;


printf("\nDo you want to create next node?(YES=1)/(NO=0)\n");


float traversal(struct node *first)
struct node *temp;
float sum,x;
while(temp!= NULL)

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

z = powl(value,exp);

return(z * coeff);

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.