Linked list realizes polynomial addition and multiplication

Linked list realizes addition and multiplication

Build a linked list

typedef struct LinkNode{
    
    
	int coef;//系数
	int expn;//幂指数
	LinkNode *next; 
}LinkNode,*LinkList;

In order to make it possible to directly view the output results better, by the way, write a show function to display the results

void show(LinkList L){
    
    //输出多项式链表结点; 
	if(L==NULL||L->next==NULL){
    
    
		printf("0 0\n");
		return;
	}
	LinkNode *r=L->next;
	while(r->next){
    
    
		printf("%d %d ",r->coef,r->expn);
		r=r->next;
	}
	printf("%d %d\n",r->coef,r->expn);
} 

Now let's consider how to implement the addition of polynomials:
First, we assume that the input polynomials are input in ascending order of power, then take out a monomial from the polynomial, and then pass its coefficient and power as two data to the linked list among. Because the word drawing is inconvenient, I just drew it directly (don’t spray the ugly word):
Insert picture description here
Then we compare the two linked lists to see if the power parts are the same. If they are different, insert the one with the smaller power into the new linked list first. If the power is the same, the coefficient part is added and subtracted. At this time, it is necessary to determine whether the result is zero. If the result is zero, discard the node directly, and then traverse in turn until the traversal of the two linked lists ends. (I will not write the inserting node to the new linked list part here!)

LinkList  Listadd(LinkList L1,LinkList L2){
    
    
	LinkNode *front=(LinkNode *)malloc(sizeof(LinkNode));
	LinkNode *rear=front;
	front->next=NULL;
	int co,ex;//系数与幂指数; 
	LinkNode *r1=L1->next,*r2=L2->next;
	if(r1==NULL&&r2==NULL) return NULL;
	while(r1&&r2){
    
    //当两个链表不为空的时候; 
		if(r1->expn==r2->expn){
    
    
			co=r1->coef+r2->coef;
			if(co!=0){
    
    
				ex=r1->expn;
				attach(co,ex,rear);
			}
			r1=r1->next;
			r2=r2->next;
		}
		else if(r1->expn<r2->expn){
    
    
			attach(r1->coef,r1->expn,rear);
			r1=r1->next;
		}
		else{
    
    
			attach(r2->coef,r2->expn,rear);
			r2=r2->next;
		}
	}
	while(r1){
    
    
		attach(r1->coef,r1->expn,rear);
		r1=r1->next;
	}
	while(r2){
    
    
		attach(r2->coef,r2->expn,rear);
		r2=r2->next;
	}
	rear->next=NULL;
	return front;
}

So our addition is over.

Multiplication (difficulty!)

According to our normal thinking habits, it must be to loop through the two linked lists, and then perform a multiplication operation on the nodes in the two linked lists, and then use the number of new linked lists to store the length of one of the linked lists. The purpose expression is like this:
Insert picture description here
But do you feel that we waste too much memory to store it? So we can continue to operate directly in the new linked list stored last time, that is, find a suitable node to insert the newly obtained result.
So we have:

LinkList ListMultiply(LinkList L1,LinkList L2){
    
    
	LinkNode *front=(LinkNode *)malloc(sizeof(LinkNode));
	LinkNode *rear=front;
	front->next=NULL;
	int co,ex;//系数与幂指数; 
	LinkNode *r1=L1->next,*r2=L2->next;
	if(r1==NULL||r2==NULL) return NULL;
	//先用L1第一个结点与L2乘一次; 
	while(r2){
    
    
		co=r2->coef*r1->coef;
		ex=r2->expn+r1->expn;
		attach(co,ex,rear);
		r2=r2->next;
	}
	r1=r1->next;
	while(r1){
    
    //L1第一个结点后面的结点与L2中每一个结点进行乘积。所得结点插入到front初始链表中; 
		r2=L2->next;//重置r2; 
		rear=front;//重置rear,以便于寻找新节点attach的位置; 
		while(r2){
    
    //遍历L2; 
			co=r2->coef*r1->coef;
			ex=r2->expn+r1->expn;//乘积操作; 
			while(rear->next&&ex>rear->next->expn){
    
    //找到链尾或者当前幂指数小于 等于链表中rear->next结点的幂指数 
				rear=rear->next;
			}
			if(rear->next&&ex==rear->next->expn){
    
    //此时已经找到位置;
			//分情况讨论:1.找到一个合适的位置:(1)幂指数相同
				if(co+rear->coef==0){
    
    //找到了一个位置: 
					LinkNode *p=rear->next;
					rear->next=p->next;
					free(p);
				}
				else{
    
    
					rear->next->coef+=co;
				}
			}
			else{
    
    	//2.(1)找到链尾。(2)幂指数不同    
			//即要新建结点; 
				LinkNode *p=(LinkNode*)malloc(sizeof(LinkNode));
				p->coef=co;
				p->expn=ex;
				p->next=rear->next;//两种情况区别在这 
				rear->next=p;
				rear=rear->next;
			}
			r2=r2->next;//遍历 
		} 
		r1=r1->next;//r1的结点乘完;
	}
	return front;
}

Then write the test function yourself! I personally think that the best test function is the one that visualizes the results.

Happy time is always short, let's see you next time! ! !

good good studym,day day up! (study hard, improve every day)

Guess you like

Origin blog.csdn.net/qq_41606378/article/details/107770320