Ex. Polynomial

The main question is to solve: how to add two polynomials?

Interface

The Implementation of some operators for two polynomials in linked list.

class PolyNode(coef: int | None = None, expon: int | None = None)

The atomic element of the linked list.

__init__(coef: int | None = None, expon: int | None = None)

Initialize an empty node of the linked list

Variables:
  • coef – The coefficient of the polynomial. Note that there is no term having a coef valued 0.

  • expon – The exponential of the polynomial.

  • next – The pointer to the next node.

class Polynomial(coef: Any, expon: Any)

A linked list stores the information of the polynomial.

__init__(coef: Any, expon: Any) None

Initialize an linked list that contains only one single node.

A multi-terms polynomial can be created by adding some single polynomials.

Variables:

head – The pointer directly to the data node of the linked list that implements the polynomial. It’s None when either of the parameters is not an int.

__str__() str

Implementation of the built-in function print().

Returns:

The string object need to be printed.

__add__(polynomial: Polynomial) Polynomial

Implementation of the addition (+) operators.

Variables:

polynomial – The polynomial to be added.

__mul__(polynomial: Polynomial) Polynomial

Implementation of the multiplication (*) operator.

Variables:

polynomial – The polynomial to be multiplied.