又是一道关于线性表结构的题目,在拿到题目时,虽然看见题目的量很短,其实它的实现的代码量还是蛮大的。因此,理解题目方能写的很长的代码。
#include
#include
typedef struct Node *PtrToNode;
struct Node{
int coe;
int exp;
PtrToNode Next;
};
typedef PtrToNode List;
List read();//读入线性表
void Print(List L);//遍历线性表
List add(List L1,List L2);
List mul(List L1,List L2);
int main()
{
List L1,L2,LA,LM;
//L1,L2就是用户想要输入的链表
//LA就是用户的一元多项式加法
//LB就是用户的一元多项式乘法,代码分明
L1 = read();
L2 = read();
LA = add(L1,L2);
LM = mul(L1,L2);
Print(LM);
Print(LA);
}
//一共是四个函数要实现
//第一个函数输入 第二个函数 遍历
//第三个函数 加法 第三个函数乘法
List read()
{
int n;
List s = (List)malloc(sizeof(struct Node));//s是最后要甩出去的链表
s->Next = NULL;//带头结点的链表
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;
}
//第二个函数
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);//题目格式要求
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;//当连个指数才能相加时,你指向谁其实都一样
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;
}
先谈一谈这段代码,这几段代码算是你破解题目的关键
typedef struct Node *PtrToNode;
struct Node{
int coe;
int exp;
PtrToNode Next;
};
typedef PtrToNode List;
List read();//读入线性表
void Print(List L);//遍历线性表
List add(List L1,List L2);
List mul(List L1,List L2);
它的结构体当中,存储的是系数和指数还有下一跳地址,读入线性表这是输入问题
List read()
{
int n;
List s = (List)malloc(sizeof(struct Node));//s是最后要甩出去的链表
s->Next = NULL;//带头结点的链表
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;
}
只要学会去头结点插入就行了,一般结构体都是以头结点的插入,记住即可,然后,读出数据,开始处理,多项式的加法,就是指次数相同,系数相加,指数不同,不能相加,这句话开始编写代码!
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;//当连个指数才能相加时,你指向谁其实都一样
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;
循环里面就是代码想要处理的核心,按照规则,指数加法,系数相乘方可得出答案!