The addition of polynomial is a simple to explode algorithm exercise, after learning the chain list will generally be the wave pull to discuss the sparse, dense with what to express, and finally generally as a list of exercises appear. In fact, using arrays to represent the polynomial is obviously unreasonable, most of the polynomial must not be so close, the list is almost the only choice.
In a language such as C + +, directly build a Poly node class, and then build a poly class (including a head node), basically is a single-linked list implementation, for the addition of the polynomial, and the chain of ordered linked list more like, multiplication is more simple, one by one by the past on the line.
The reason this process is simple is that the encapsulation of the class (C with the struct and some functions for this simple problem is basically no different), of course, a map can also simulate this structure.
when we choose racket to achieve the problem is much more interesting , and, all the hierarchy of the implementation of the package itself, the approximate process is: from the polynomial node term construction, to the polynomial list terms, and finally terms and a variable (for example, X Cons together , each step to do a good package, more useless, this is a worthwhile project, the important thing is not the polynomial itself, but from the experience of encapsulation and abstraction of the skills and meaning.
In the racket language, the syntax is very simple to construct an unusually complex data structure and functions, so that there is a strong expression, of course, the designer's abstract packaging skills, otherwise the code is a bit larger than the number of bugs can not be seen.
The code is not posted here, SICP the second chapter at the end of this example, and there are rational numbers system, symbolic derivation and other examples, are a good example of understanding the nature of closures and data abstraction, strongly recommended.
(If you have not learned scheme, it is very good to start directly from SICP, because it will only give you a little bit of grammar to create complex structures, only to continue to introduce you to the grammar, the process of grammar is to serve the mind, So there is absolutely no need to first go to the scheme grammar and then see.
Polynomial addition--c++ and racket