This article is a linear structure of the data structure (application example), based on the integrated notes of the online course.
chestnut:
Design function to find the product and sum of two univariate polynomials
Sample input and output of this question:
Solution ideas
- Polynomial representation
- Program framework
- Read polynomial
- Additive realization
- Multiplication realization
- Polynomial output
One, the representation of the polynomial
(Only indicate non-zero items)
Array:
- Simple programming and easy debugging
- Need to determine the size of the array in advance (if not sure, it will cause a waste of space)
Linked list:
- Dynamic
- Programming is slightly complicated and debugging is more difficult
PS: A better implementation method is: dynamic array
The following describes the linked list representation
Data structure design
typedef struct PolyNode *Polynomial;
struct PolyNode{
int coef;/*系数*/
int expon;/*指数*/
Polynomial link;
};
Second, the program framework construction
int main()
{
读入多项式1
读入多项式2
乘法运算并输出
加法运算并输出
return 0;
}
Functions to be designed :
- Read in a polynomial
- Multiplying two polynomials
- Add two polynomials
- Polynomial output
int main()
{
Polynomial P1,P2,PP,PS;
P1 = ReadPoly();
P2 = ReadPoly();
PP = Mult(P1,P2);
PrintPoly(PP);
PS = Add(P1,P2);
PrintPoly(PS);
return 0;
}
3. How to read polynomials
Input data format: 4 3 4 -5 2 6 1 -2 0 (4 is the number of 4 groups, and then read in one pair of pairs in a loop)
Polynomial ReadPoly()
{
...
scanf("%d",&N);
...
while(N--){
scanf("%d %d",&c,&e);/*每对数据按指数递减顺序读入*/
Attach(c,e,&Rear);/*构造并插入新结点*/
}
...
return P;
}
Thinking: What is the initial value of Rear?
Two treatment methods:
1. The initial value of Rear is NULL
In the Attach function, different processing is done according to whether Rear is NULL (Attach will determine whether Rear is NULL at the beginning, if it is NULL, apply for the node to change Rear to NULL to point Rear to this node; if it is not NULL, directly Insert the new node at the end)
2.Rear points to an empty node
After the end, delete the last empty node
Fourth, how to read in a polynomial
void Attach(int c,int e,Polynomail *pRear)/*pRear是指针的指针*/
{
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));/*申请结点*/
P->coef = c; /*对新结点赋值*/
P->expon = e;
P->link = NULL;
(*pRear)->link = P;
*pRear = P; /*修改pRear值*/
}
Polynomial ReadPoly()
{
Polynomial P,Rear,t;
int c,e,N;
scanf("%d",&N);
P = (Polynomial)malloc(sizeof(struct PolyNode));/*链表头空结点*/
P->link = NULL;
Rear = P;/*第二种处理方法,一开始就是空结点*/
while(N--){
scanf("%d %d",&c,&e);
Attach(c,e,&Rear);/*将当前项插入多项式尾部*/
}
t = P;P = P->link;/*删除临时生成的头结点*/
free(t);
return P;
}
Five, how to add two polynomials
Architecture
Polynomial Add(Polynomial P1,Polynomial P2)
{
...
t1 = P1,t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));/*构造空结点*/
P->link = NULL;
Rear = P;
while(t1 && t2){
if(t1->expon == t2->expon){
...
}
else if(t1->expon > t2->expon){
...
}
else{
...
}
}
while(t1){
...
}
while(t2){
...
}
...
return P;
}
The first while judges the index and calculates the coefficient
The second while and the third while respectively judge the situation that t1 and t2 are empty
method:
1. Convert multiplication operation to addition operation
Multiply the current term of P1 (ci, ei) by the P2 polynomial and add it to the result polynomial
t1 = P1;t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while(t2){
Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
t2 = t2->link;
}
2. Insert item by item
Multiply the current term of P1 (ci, ei) by the current term of P2 (c2i, e2i) and insert it into the resulting polynomial. The key is to find the insertion position
The first result polynomial can be obtained by multiplying the first term of P1 by P2 (as above)
Polynomail Mult(Polynomial P1,Polynomial P2)
{
Polynomial P,Rear,t1,t2,t;
int c,e;
if(!P1||!P2) return NULL;
t1 = P1;t2 = P2;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->link = NULL;
Rear = P;
while(t2){
/*先用P1的第1项乘以P2,得到P*/
Attach(t1->coef*t2->coef,t1->expon+t2->expon,&Rear);
t2 = t2->link;
}
t1 = t1->link;
while(t1){
t2 = P2;Rear = P;
while(t2){
e = t1->expon + t2->expon;/*计算指数*/
c = t1->coef*t2->coef;/*计算系数*/
while(Rear->link&&Rear->link->expon>e)
Rear = Rear->link;
if(Rear->link&&Rear->link->expon == e){
if(Rear->link->coef+c)
Rear->link->coef+=c;
else{
t = Rear->link;
Rear->link=t->link;
free(t);
}
}
else{
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c;t->expon = e;
t->link = Rear->link;
Rear->link = t;Rear = Rear->link;
}
t2 = t2->link;
}
t1 = t1->link;
}
t2 = P;P = P->link;
free(t2);
return P;
}
6. How to output the polynomial
void PrintPoly(Polynomial P)
{
/*输出多项式*/
int flag = 0;/*辅助调整输出格式用*/
if(!P){
printf("0 0\n");
return;
}
while(P){
if(!flag)
flag = 1;
else
printf(" ");
printf("%d %d",P->coef,P->expon);
P = P->link;
}
printf("\n");
}
Disclaimer: Part of the information comes from the Internet, if there is any infringement, please contact me to delete it!
If there are errors, confusions, etc. in the article, criticisms and corrections are welcome!