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

6 comments:

  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.

    ReplyDelete
  2. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    rpa training in bangalore
    best rpa training in bangalore
    RPA training in bangalore
    rpa course in bangalore
    rpa training in chennai
    rpa online training

    ReplyDelete
  3. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python training in chennai
    python course in chennai
    python training in bangalore

    ReplyDelete
  4. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Training in Bangalore | Best AWS Amazon Web Services…
    Amazon Web Services (AWS) Training in Pune India
    AWS Training | AWS Training and Certification | AWS online training
    AWS Training in Bangalore cost| Aws training in Bangalore with placements

    ReplyDelete
  5. 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