HW1A - p.85 3.6 Write a function to add two polynomials. Do...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Write a function to add two polynomials. Do not destroy the input. Use a linked list implementation. If the polynomials have M and N terms, respectively, what is the time complexity of your program? typedef struct Node *PtrToNode; struct Node { int Coefficient; int Exponent; PtrToNode Next; }; typedef PtrToNode Polynomial; /* Nodes are sorted in decreasing order of exponents.*/ Polynomial Add ( Polynomial a, Polynomial b ) { /* return a polynomial which is the sum of a and b*/ /*use linked list with a head node */ PtrToNode front, tail; int sum; /* create a head node */ tail = malloc( sizeof (Node)); if ( tail ==Null) FatalError( "The memory is full"); front = tail; front->Exponent=-1; a=a->Next; b=b->Next; while ( a && b ) switch ( COMPARE(a-> Exponent, b-> Exponent) ) { case -1: Attach(b-> Coefficient, b-> Exponent, &tail); b = b-> Next; break ; case 0: sum = a-> Coefficient + b-> Coefficient; if ( sum ) Attach(sum, a-> Exponent, &tail); a = a-> Next; b = b-> Next; break ; case
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/16/2011 for the course CS 136 taught by Professor Yuechen during the Winter '08 term at Zhejiang University.

Page1 / 3

HW1A - p.85 3.6 Write a function to add two polynomials. Do...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online