数据结构(C语言)将一个用循环链表表示的稀疏多项式分解成两个多项式,使这两个多项式中各自仅含奇次项或偶次项,并要求利用原

1个回答

  • 呵呵,碰巧到你百度空间看了下,看到了你想知道的这个问题,蒽蒽,下午就给你答案哈~~~

    恩,可以了:

    #include

    #include

    typedef struct term

    {

    double coef;//系数

    int expn; //指数

    struct term *next;

    }term,*polynomial;

    //注:下面的head_p是头指针,而非头结点。其存放的是多项式的第一项

    int depart_polyn(polynomial &head_p,polynomial &Odd_items,polynomial &Even_items)//奇数项,偶数项

    {

    int i=1,j=1;

    polynomial odd,even,p;

    Odd_items=Even_items=NULL;

    p=head_p;

    while(1)

    {

    if(p->expn%2)

    {

    if(i)

    {

    i=0;//使得奇数项头指针只赋值一次

    Odd_items=p;

    odd=Odd_items;

    }

    else

    {

    odd->next=p;

    odd=odd->next;

    }

    p=p->next;

    odd->next=Odd_items;

    }

    else

    {

    if(j)

    {

    j=0;//作用同上

    Even_items=p;

    even=Even_items;

    }

    else

    {

    even->next=p;

    even=even->next;

    }

    p=p->next;

    even->next=Even_items;

    }

    if(p==head_p)

    break;

    }

    return 1;

    }

    void print_polyn(polynomial head)//这函数只是针对main函数内给出的多项式例子编的,处理的系数均为正数。

    {

    polynomial p=head;

    if( !head)

    {

    puts("该表为空:");

    return;

    }

    while(1)

    {

    if(p->next!=head)

    printf("%gm^%d + ",p->coef,p->expn);

    else

    printf("%gm^%dn",p->coef,p->expn);

    p=p->next;

    if(p==head)

    break;

    }

    putchar('n');

    }

    int main()

    {

    int i;

    polynomial head_p,head,Odd_items,Even_items,newbase;

    head_p=(polynomial)malloc(sizeof(term));

    head_p->coef=2.71;

    head_p->expn=0;

    head=head_p;

    //随意创建了一个稀疏多项式,以验证

    for(i=1;icoef=2.71*i;

    newbase->expn=i*13;

    head->next=newbase;

    head=head->next;

    }

    head->next=head_p;//构成循环链表

    puts("原稀疏多项式:");

    print_polyn(head_p);

    if(depart_polyn(head_p,Odd_items,Even_items) )

    {

    puts("分解后的奇数项稀疏多项式:");

    print_polyn(Odd_items);

    puts("分解后的偶数项稀疏多项式:");

    print_polyn(Even_items);

    }

    return 0;

    }

    哪里不懂可追问: