| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖
标题:多项式合并出错
只看楼主 加入收藏
小J
Rank: 8Rank: 8
等 级:等待验证会员
威 望:1
帖 子:282
专家分:704
注 册:2009-6-18
结帖率:100%
收藏
 问题点数:0 回复次数:1 
多项式合并出错
#include "stdio.h"
#include "malloc.h"


typedef struct node{
    int xishu;
    int zhishu;
    struct node *next;
    struct node *prior;
}NODE;

NODE *create_list()
{
    int a,m,n;
    NODE *head,*p,*q;
    head=(NODE *)malloc(sizeof(NODE));
    q=head;
    q->prior=NULL;
    printf("请输入链表的长度:");
    scanf("%d",&a);
    if(a>0)
    {
        while(a>0)
        {
            printf("请输入多项式的系数和指数:");
            scanf("%d%d",&m,&n);
            p=(NODE *)malloc(sizeof(NODE));
            p->xishu=m;
            p->zhishu=n;
            q->next=p;
            p->prior=q;
            q=p;
            a--;
        }
        q->next=NULL;
    }
    return head;
}



void show_list(NODE *head)
{
    NODE *p;
    p=head->next;
    while(p!=NULL)
    {
        printf("%dx^%d  ",p->xishu,p->zhishu);
        p=p->next;
    }
    printf("\n");
}



NODE *connect(NODE *head1,NODE *head2)
{
    NODE *p,*q;
    p=head1->next;
    q=head2->next;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    p->next=q;
    q->prior=p;
    return head1;
}


NODE *add(NODE *head)
{
    int x=0;
    NODE *p,*q;
    p=head->next;//将p指向第一个节点
    q=p->next;//将q指向第二个节点
    while(q!=NULL)
    {
        while(q!=NULL)
        {
            if(p->zhishu==q->zhishu)   //判断p和q的指数是否相等
            {
                x=p->xishu+q->xishu;  //如果相等就相加
                if(x!=0)   //判断相加后的结果是否为零
                {
                    if(p->next!=q)   //q不是p的直接后继时释放q
                    {
                        p->xishu=x;
                        (q->next)->prior=q->prior;  //释放掉节点q
                        (q->prior)->next=q->next;
                        free(q);
                    }
                    else      //q是p的直接后继释放q
                    {
                        p->xishu=x;
                        (q->next)->prior=p;
                        p->next=q->next;
                        free(q);
                    }
                }
                else   //如果相加后的结果等于零就同时释放p和q的节点
                {
                    if(p->next!=q)//q不是p的直接后继释放q和p
                    {
                        (p->next)->prior=p->prior;
                        (p->prior)->next=p->next;
                        (q->next)->prior=q->prior;
                        (q->prior)->next=q->next;
                        free(p);
                        free(q);
                    }
                    else//q是p的直接后继时释放p和q
                    {
                        (q->next)->prior=p->prior;
                        (p->prior)->next=q->next;
                        free(p);
                        free(q);
                    }
                }
            }
            else
                q=q->next;
        }
        p=p->next;
        q=p->next;
    }
    return head;
}


main()
{
    NODE *a,*b,*c,*d;
    a=create_list();
    b=create_list();
    c=connect(a,b);
    show_list(c);
    d=add(c);
    show_list(d);
    getchar();
}

这是我做的多项式的合并。我把两个多项式分别放在两个双向链表里。然后将两个链表合并,再合并新链表的系数相同的项
但不知道出什么问题总是出不来。提示是那个add函数出错了!各位高手帮帮忙啊!
搜索更多相关主题的帖子: 多项式 
2009-11-08 19:45
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
我把这个函数给你改改,给成如下。你这种太麻烦了,别用双向链表,单链表就可以,别让两个链表合并,那样会增加很多判断,可以改成,把链表b与a依次比较相加或插入,最后把剩余的元素连接到a后。
NODE *add(NODE *head)
{
    int x=0;
    NODE *p,*q,*t;
    p=head->next;/*将p指向第一个节点 */
    q=p->next;/*将q指向第二个节点  */
    while(q!=NULL)
    {
        while(q!=NULL)
        {         t=q->next;       /////////////再中间你把q free了,      
            if(p->zhishu==q->zhishu)   /*判断p和q的指数是否相等-*/
            {
                x=p->xishu+q->xishu;  /*如果相等就相加 */
                if(x!=0)   /*判断相加后的结果是否为零  */
                {
                    if(p->next!=q)   /*q不是p的直接后继时释放q  */
                    {   if(q!=NULL)
                       {                   ///////////////再每个if else 中判断q是否是最后一个元素,删除的方式是不同的。
                        p->xishu=x;
                        (q->next)->prior=q->prior;  /*释放掉节点q */
                        (q->prior)->next=q->next;

                        free(q);
                       }
                       else
                       {
                           p->xishu=x;
                           q->prior->next=NULL;
                       }
                    }
                    else      /*q是p的直接后继释放q  */
                    {   if(q!=NULL){
                        p->xishu=x;
                        (q->next)->prior=p;
                        p->next=q->next;
                        free(q);          }
                        else {
                            p->xishu=x;p->next=NULL;}
                    }
                }
                else   /*如果相加后的结果等于零就同时释放p和q的节点 */
                {
                    if(p->next!=q)/*q不是p的直接后继释放q和p  */
                    {   if(q!=NULL){
                        (p->next)->prior=p->prior;
                        (p->prior)->next=p->next;
                        (q->next)->prior=q->prior;
                        (q->prior)->next=q->next;
                        free(p);
                        free(q);          }
                        else { (p->next)->prior=p->prior;
                        (p->prior)->next=p->next;
                            q->prior->next=NULL;   }
                    }
                    else/*q是p的直接后继时释放p和q */
                    {   if(q!=NULL){
                        (q->next)->prior=p->prior;
                        (p->prior)->next=q->next;
                        free(p);
                        free(q);          }
                        else {
                            p->prior->next=NULL;}
                    }
                }
            }

                q=t;      ///////////t保存q的下一个节点。
        }
        p=p->next;
        q=p->next;
    }
    return head;
}

离恨恰如春草,更行更远还生。
2009-11-11 16:51
快速回复:多项式合并出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037809 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved