c语言多项式乘法,指针赋值时总是出现段错误该如何解决

自己编写了多项式加乘的程序,调试的时候总是会出现段错误。其中加法部分没有问题,但是乘法部分下的一系列函数总会碰到段错误,是我的指针初始化/赋值存在问题吗?请各位指出不足,给出改进方案。题目如下:

img


代码如下:

#include <stdio.h>
#include <stdlib.h>

struct Node{//exp:指数coef:系数
    int exp;
    int coef;
    struct Node *next;
};

struct Poly{//P1:头节点  P2:尾节点
    struct Node *P1;
    struct Node *P2;
};

typedef struct Node *node;
typedef struct Poly *poly;


poly CreatPoly(void);
void ReadPoly(poly p);
void PrintPoly(poly p);
void addP(poly s,int exp,int coef);
int Compare(int exp1,int exp2);
poly PolyAdd(poly p1,poly p2);
poly PolyMultiply(poly p1,poly p2);
int Findshort(poly p1,poly p2);
int GetSize(void);
void RankPoly(poly p);
poly AddAllP(poly p[],int size);


int main()
{
    poly p1=CreatPoly();
    poly p2=CreatPoly();
    ReadPoly(p1);
    ReadPoly(p2);
    poly p3=PolyMultiply(p1,p2);
    PrintPoly(p3);
return 0;
}

poly CreatPoly(void)
{
    poly s=(poly)malloc(sizeof(struct Poly));
    s->P1=NULL;
    s->P2=NULL;
    return s;
}

void ReadPoly(poly p)//co,ex代表系数,指数 ,term是多项式中有多少非零项 
{
    int i=0;
    int term=0,co=0,ex=0;
    scanf("%d",&term);
    for(i=0;i<term;i++)
    {
        scanf("%d",&co);
        scanf("%d",&ex);
        addP(p,ex,co);
    }
}

void PrintPoly(poly p)
{
    node point=p->P1;
    for(;point;point=point->next)
    {
        printf("%d ",point->coef);
        printf("%d ",point->exp);
    }
} 

void addP(poly s,int exp,int coef)
{
    node newnode=(node)malloc(sizeof(struct Node));
    newnode->exp=exp;
    newnode->coef=coef;
    newnode->next=NULL;
    if(s->P1==NULL&&s->P2==NULL)
    {
        s->P1=newnode;
        s->P2=newnode;
        newnode->next=NULL;
    }else{
        s->P2->next=newnode;
        s->P2=newnode;
    }    
}

int Compare(int exp1,int exp2)
{
    if(exp1>exp2)
    {
        return 1;
    }else if(exp1==exp2){
        return 0;
    }else{
        return -1;
    }
}

poly PolyAdd(poly p1,poly p2)//此处P1,P2对应队列中的front,rear 
{
    poly p3=CreatPoly();
    int sum=0;
    node temp1=NULL;
    temp1=p1->P1;
    node temp2=NULL;
    temp2=p2->P1;
    while(temp1!=NULL&&temp2!=NULL)
    {
        switch(Compare(temp1->exp,temp2->exp))
        {
            case 1:
                addP(p3,temp1->exp,temp1->coef);
                temp1=temp1->next;
                break;
            case -1:
                addP(p3,temp2->exp,temp2->coef);
                temp2=temp2->next;
                break;
            case 0:
                sum=temp1->coef+temp2->coef;
                if(sum)
                {
                    addP(p3,temp1->exp,sum);
                }
                temp1=temp1->next;
                temp2=temp2->next;
                break;
        }
    }
    for(;temp1;temp1=temp1->next)
    {
        addP(p3,temp1->exp,temp1->coef);
    }
    for(;temp2;temp2=temp2->next)
    {
        addP(p3,temp2->exp,temp2->coef);
    }
    return p3;
}

poly PolyMultiply(poly p1,poly p2) 
{
    int size=12;
    poly *p=(poly*)malloc(sizeof(struct Poly)*size);
    int multi=0,sum=0;
    int i=0;
    node temp1=p1->P1;
    node temp2=p2->P1;
    switch(Findshort(p1,p2))//这部分进行指数相加,系数相乘。 
    {
        case 1:
            for(i=0;temp1;temp1=temp1->next)
            {    
                p[i]=CreatPoly();
                for(temp2=p2->P1;temp2;temp2=temp2->next)//i用于计存放多项式数 
                {    
                    sum=temp1->exp+temp2->exp;
                    multi=temp1->coef*temp2->coef;
                    addP(p[i],sum,multi);    
                }
                i++;    
            }
            break;
        case 2:
            for(i=0;temp2;temp2->next)
            {
                p[i]=CreatPoly();
                for(temp1=p1->P1;temp1;temp1=temp1->next)//i用于计存放多项式数 
                {
                    sum=temp1->exp+temp2->exp;
                    multi=temp1->coef*temp2->coef;
                    addP(p[i],sum,multi);
                    
                }
                i++;    
            }
            break;
    }
    int count=i+1;
    for(i=0;i<count;i++)//对所有多项式进行按指数降序排序 
    {
        RankPoly(p[i]);
    }
    poly s=AddAllP(p,count);
    RankPoly(s);
    return s;
}

int Findshort(poly p1,poly p2)
{
    node po1=p1->P1;
    node po2=p2->P1;
    for(;po1&&po2;po1=po1->next,po2=po2->next){
    }
    if(po1->next)
    {
        return 2;
    }else{
        return 1 ;
    }
}

int GetSize(void)//此函数用于获取 乘法共产生多少个多项式 
{    
    rewind(stdin);//将读取位置设置在开头 
    int x1=0,x2=0;
    char a[10000];
    scanf("%d",&x1);
    gets(a);//用于跳过第一行的剩下内容 
    scanf("%d",&x2);
    return x1*x2;
}//遇到问题:rewind后要求重复输入,但是我想重复读取第一段输入的内容 

void RankPoly(poly p)//自己设计的排序算法,用于降序排序一个单链表 
{
    node max1=NULL;
    max1=p->P1;
    node max2=NULL;
    max2=p->P1->next;
    int cnt=0,temp=0;
    if(max1==NULL)
    {
        return;
    }
    if(max1==p->P2)
    {
        return;
    }
    while(cnt!=0)
    {
        cnt=0;
        for(;max1&&max2;)
        {
            if(max1->exp<max2->exp)
            {
                temp=max1->exp;
                max1->exp=max2->exp;
                max2->exp=temp;
                temp=max1->coef;
                max1->coef=max2->coef;
                max2->coef=temp;
                max1=max1->next;
                max2=max2->next;
                cnt++;
            }else{
                max1=max1->next;
                max2=max2->next;
            }
        }
    }
    return;
}

poly AddAllP(poly p[],int size)//递归将所有结果多项式加起来得到乘法结果 
{
    int n=size-1;
    if(n!=-1)
    {
        return PolyAdd(p[n],AddAllP(p,size-1));    
    }else{
        poly a=CreatPoly();
        return a;
    }
}