An Instance Of Polynomial Using Link List
1.
Designs a Link List based Polynomial and provide operations on it such as creating polynomial, add, fix or remove items, calculating and outputting polynomial. Calculating should include multiplication at least.
2.
A polynomial has many items and items with same power can shrink into one item. So the polynomial can be stored and sorted by power order.
The polynomial should provide operations. It includes most base operations of Link List such as inserting or removing. Sum of the polynomial may be optional.
The insert or arithmetic operations may produce the item change or calculate, so the item should also provide base input/output and arithmetic operations.
3.
Because the polynomial stores items with a list, it can be inherited from an abstract date type (ADT) List. Then add new operations to fit for users. Each item in the polynomial should have member variables to store coefficient and power. Then use Link class to link them into a list.
1)
Base class: (ADT)
//list abstract class
template <class Elem>
class List
{
};
It describes the basic operations on a List.
Node class: (generic design)
template <class T1,class T2>
class Elem
{
};
The items in polynomial provide some operations. The operands of Addition operator must have same power.
Link class: (generic design)
template<class Elem>
class Link
{
};
The class Link linked all elements in a polynomial. For generic design, users can define their element class. Because items in polynomial have fixed structure, and items should provide operations, users had better to use this node class.
Polynomial class: (generic design)
Member variable definitions:
template <class Elem>
class Polyno:public List<Elem>
{
};
Instruction:
“name”: refers to the polynomial name such as letter ‘f’ in ‘f(x)’, it do not have any relationship with other operations except output.
“head”, ”tail”, ”fence”: pointers to the storage of node. head point to the head node, and first node in the polynomial is head->next.
“leftcnt”, ”rightcnt”: refer to the number of elements in the polynomial left or right of fence.
Member function declaration:
template <class Elem>
class Polyno:public List<Elem>
{
};
most of the member function can be known by the name.
init() , removeall() : private member function, designed for other member function only.
insert(), append() : add an element into the polynomial. The polynomial used sorted storage, there is no different between these two functions. Any element to be inserted will be checked and the program determines where to insert or whether to need combination.
remove() : remove the item fence->next. Because locate fence is complex and remove may destroy data, user should beware.
setValue() : set fence->next value. It is mainly for combine items with same power.
clear() : delete all items in the polynomial and initial it.
print() : output polynomial with the form
f(x)=(-3)x^(-6)+7+8x^9+(-17)x^16
插入表情