Polynomial Addition
Algorithm
Input inputs polynomial expression in terms of coefficient and power Output Sum of two polynomial expressions Complexity O(n). create()// inputs polynomial expression in terms of coefficient and power 1. Repeat while(1) a. [Input the coff and pow]. b. if (node→pow==0) then: i. Set ptr=node, node=NULL, ptr→link:=node and break. c. [whether to input more coeff?] cin>>ch. d. if NO then Set ptr=node, node=NULL, ptr→link:=node and break. e. Set ptr:=node and ptr→link:=node. 2. [End] sum()//adds two polynomials pointed by ptr1 and ptr2 1. [allocate memory for polynomial storing sum of two polynomials]. 2. [Initialize segment variables] Set ptr1 := start1, ptr2 := start2 and start3 := node. 3. Repeat while(ptr1!=NULL && ptr2!=NULL) a. Set ptr := node. b. If (ptr1→pow>ptr2→pow) then : i. Set node→coff := ptr2→coff and node→pow := ptr2→pow. ii. Set ptr2 := ptr2→link. c. Else if (ptr1→pow<ptr2→pow) then : i. Set node→coff := ptr1→coff and node→pow := ptr1→pow. ii. Set ptr1 := ptr1→link. d. Else i. Set node→coff := ptr2→coff+ ptr1→coff and node→pow := ptr2→pow. ii. Set ptr1 := ptr1→link and ptr2:=ptr2→link. e. Set ptr→link := node. 4. If (ptr1==NULL) then: a. Repeat while(ptr2!=NULL) i. Set node→coff := ptr2→coff and node→pow := ptr2→pow. ii. Set ptr := node. iii. [Allocate memory for node] iv. Set ptr→link:=node. 5. Else if (ptr2==NULL) then: a. Repeat while(ptr1!=NULL) i. Set node→coff := ptr1→coff and node→pow := ptr1→pow. ii. Set ptr := node. iii. [Allocate memory for node] iv. Set ptr→link:=node. 6. Set node:=NULL and ptr→link := node. 7. Exit
Algorithm Description