#include #include #include "poly.h" struct poly *polyCreate() { // initialize empty list return (struct poly *)0; } struct poly *polyDelete(struct poly *p) { while(p) { struct poly *temp = p; // remember current node p = p->p; // move to next node free(temp); // delete current node } return p; } double polyEval(struct poly *p, double x) { double f = 0.0; // iterate over linked list and evaluate each term for( ; p; p = p->p) f += p->a * pow(x, (double)p->n); return f; } struct poly *polySetCoefficient(struct poly *p, int degree, double value) { // empty list or insert at head if(!p || p->n < degree) { struct poly *q = malloc(sizeof(struct poly)); q->a = value; q->n = degree; q->p = p; return q; } // find place to insert // cur points to node after which new node to come struct poly *cur = p; while(cur->p && cur->p->n > degree) cur = cur->p; // node of degree n already present if(cur->p && cur->p->n == degree) cur->p->a = value; // new node needed else { struct poly *q = malloc(sizeof(struct poly)); q->a = value; q->n = degree; // insert into linked list q->p = cur->p; cur->p = q; } return p; } double polyGetCoefficient(struct poly *p, int degree) { for( ; p && p->n > degree; p = p->p) ; if(!p || p->n != degree) return 0.0; else return p->a; }