(c language) 02 - multiplication and addition of linear structure 2-ary polynomials (20 points) (detailed explanation)

It's also a question about the structure of linear table. When we get the question, although we see the amount of question is very short, in fact, the amount of code it implements is quite large. Therefore, understanding the topic can write a long code.

#include<stdio.h>
#include<stdlib.h>
typedef struct Node *PtrToNode;
struct Node{
	int coe;
	int exp;
	PtrToNode Next;
};
typedef PtrToNode List;
List read();//Read in linear table
void Print(List L);//Ergodic linear table
List add(List L1,List L2);
List mul(List L1,List L2);
int main()
{
	List L1,L2,LA,LM;
	//L1 and L2 are the linked lists that users want to input
	//LA is the user's one variable polynomial addition
	//LB is the multiplication of user's unary polynomials, with clear code
	L1 = read();
	L2 = read();
	LA = add(L1,L2);
	LM = mul(L1,L2);
	Print(LM);
	Print(LA);
	
}
//There are four functions to implement
//First function input second function traversal 
//Third function addition third function multiplication
List read()
{
	int n;
	List s = (List)malloc(sizeof(struct Node));//s is the last list to be thrown out
	s->Next = NULL;//List With Header node 
	scanf("%d",&n);
	if(n){
		List m = s;
		for(int i=0;i<n;i++){
			List p =(List)malloc(sizeof(struct Node));
			scanf("%d%d",&(p->coe),&(p->exp));
			m->Next = p;
			m = p;
		}
		m->Next = NULL;
	}
	return s;	
}
//Second function
void Print(List L){
	List r = L->Next;
	if(r){
		List p = (List)malloc(sizeof(struct Node));
		p = L;
		if(p->Next){
			p = p->Next;
			printf("%d %d",p->coe,p->exp);//Title format requirements
			while(p->Next){
				p = p->Next;
				printf(" %d %d",p->coe,p->exp);
			}
		}
	}else{
		printf("0 0");
	}
	printf("\n");
}
List add(List L1,List L2){
	List pa,pb,pc,L;
	L = (List)malloc(sizeof(struct Node));
	L->Next = NULL;
	pa = L1->Next;
	pb = L2->Next;
	pc = L;
	while(pa && pb)
	{
		if(pa->exp<pb->exp){
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pb->coe;
			temp->exp = pb->exp;
			pc->Next = temp;
			pc = temp;
			pb = pb->Next;
		}
		else if(pa->exp == pb->exp){
			int a = pa->coe+pb->coe;
			if(a!=0){
				List temp = (List)malloc(sizeof(struct Node));
				temp->Next = NULL;
				temp->coe = a;
				temp->exp = pa->exp;//When you add up indexes, you point to the same people
				pc->Next = temp;
				pc = temp;				
			}
			pb=pb->Next;
			pa=pa->Next;
		}else{
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pa->coe;
			temp->exp = pa->exp;
			pc->Next = temp;
			pc = temp;
			pa = pa->Next;			
		}
		
	}
	if(pa){
		while(pa){
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pa->coe;
			temp->exp = pa->exp;
			pc->Next = temp;
			pc = temp;
			pa = pa->Next;	
		}
	}else{
		while(pb){
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pb->coe;
			temp->exp = pb->exp;
			pc->Next = temp;
			pc = temp;
			pb = pb->Next;			
		}
	}
	pc->Next = NULL;
	return L;
}
List mul(List L1,List L2){
	List p1,p2,p3,L,Lm;
	L = (List)malloc(sizeof(struct Node));
	p1 = L1->Next;
	p2 = L2->Next;
	L->Next = NULL;
	if(p1&&p2){
		for(p1=L1->Next;p1;p1=p1->Next){
			Lm = (List)malloc(sizeof(struct Node));
			Lm->Next = NULL;
			p3 = Lm;
			for(p2=L2->Next;p2;p2=p2->Next){
				List p4=(List)malloc(sizeof(struct Node));
				p4->coe = p1->coe*p2->coe;
				p4->exp = p1->exp+p2->exp;
				p3->Next = p4;
				p3=p4;
			}
			p3->Next = NULL;
			L=add(L,Lm);
			free(Lm);
		}
	}
	return L;
}

Let's talk about this code first, which is the key for you to solve the problem

typedef struct Node *PtrToNode;
struct Node{
	int coe;
	int exp;
	PtrToNode Next;
};
typedef PtrToNode List;
List read();//Read in linear table
void Print(List L);//Ergodic linear table
List add(List L1,List L2);
List mul(List L1,List L2);

In its structure, it stores coefficients and exponents and the next hop address. Read it into the linear table. This is the input problem

List read()
{
	int n;
	List s = (List)malloc(sizeof(struct Node));//s is the last list to be thrown out
	s->Next = NULL;//List With Header node 
	scanf("%d",&n);
	if(n){
		List m = s;
		for(int i=0;i<n;i++){
			List p =(List)malloc(sizeof(struct Node));
			scanf("%d%d",&(p->coe),&(p->exp));
			m->Next = p;
			m = p;
		}
		m->Next = NULL;
	}
	return s;	
}

As long as you learn to insert without the head node, the general structure is to insert with the head node, just remember, then read out the data, start processing, polynomial addition, that is, the same degree, coefficient addition, index difference, can not add, this sentence starts to write the code!

List add(List L1,List L2){
	List pa,pb,pc,L;
	L = (List)malloc(sizeof(struct Node));
	L->Next = NULL;
	pa = L1->Next;
	pb = L2->Next;
	pc = L;
	while(pa && pb)
	{
		if(pa->exp<pb->exp){
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pb->coe;
			temp->exp = pb->exp;
			pc->Next = temp;
			pc = temp;
			pb = pb->Next;
		}
		else if(pa->exp == pb->exp){
			int a = pa->coe+pb->coe;
			if(a!=0){
				List temp = (List)malloc(sizeof(struct Node));
				temp->Next = NULL;
				temp->coe = a;
				temp->exp = pa->exp;//When you add up indexes, you point to the same people
				pc->Next = temp;
				pc = temp;				
			}
			pb=pb->Next;
			pa=pa->Next;
		}else{
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pa->coe;
			temp->exp = pa->exp;
			pc->Next = temp;
			pc = temp;
			pa = pa->Next;			
		}
		
	}
	if(pa){
		while(pa){
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pa->coe;
			temp->exp = pa->exp;
			pc->Next = temp;
			pc = temp;
			pa = pa->Next;	
		}
	}else{
		while(pb){
			List temp = (List)malloc(sizeof(struct Node));
			temp->Next = NULL;
			temp->coe = pb->coe;
			temp->exp = pb->exp;
			pc->Next = temp;
			pc = temp;
			pb = pb->Next;			
		}
	}
	pc->Next = NULL;
	return L;
}

This is to consider the greater than, less than and equal to the three cases respectively. It is also the insertion problem of the linked list. This code needs to make corresponding understanding
Finally, it's the multiplication of polynomials. The multiplication formula is to multiply the coefficients one by one, add the times to each other, and finally sort out with the polynomial addition

List mul(List L1,List L2){
	List p1,p2,p3,L,Lm;
	L = (List)malloc(sizeof(struct Node));
	p1 = L1->Next;
	p2 = L2->Next;
	L->Next = NULL;
	if(p1&&p2){
		for(p1=L1->Next;p1;p1=p1->Next){
			Lm = (List)malloc(sizeof(struct Node));
			Lm->Next = NULL;
			p3 = Lm;
			for(p2=L2->Next;p2;p2=p2->Next){
				List p4=(List)malloc(sizeof(struct Node));
				p4->coe = p1->coe*p2->coe;
				p4->exp = p1->exp+p2->exp;
				p3->Next = p4;
				p3=p4;
			}
			p3->Next = NULL;
			L=add(L,Lm);
			free(Lm);
		}
	}
	return L;

Loop inside is the core of the code to deal with, according to the rules, index addition, coefficient multiplier can get the answer!

Published 31 original articles, won praise 2, visitors 421
Private letter follow

Tags: P4 less

Posted on Sun, 15 Mar 2020 07:26:51 -0400 by amclean