| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 922 人关注过本帖
标题:一元稀疏多项式 链表
只看楼主 加入收藏
diaoxue
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-6-1
收藏
 问题点数:0 回复次数:1 
一元稀疏多项式 链表
帮忙看下表的创建和合并(相加)
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  

typedef struct LNode  
{  
    int coef;  
    int expn;
    struct LNode *next;
}LNode,*LInkList;

ListNode CreateFromTail(LinkList *L)  
{
    int m;  
    int n;  
    l=(LinkList)malloc(sizeof(LinkList));  
    l->next=NULL;
    tail=l;
    printf("\nplease put the coef:");  
    scanf("%d",&m);  
    printf("\nplease put the expn:");  
    scanf("%d",&n);  
    while(m)  
    {  
        p=(LinkList)malloc(sizeof(LinkList));  
        p->coef=m;  
        p->expn=n;  
        tail->next=p;  
        tail=p;  
        printf("\nplease put the coef:");  
        scanf("%d",&m);  
        printf("\nplease put the expn:");  
        scanf("%d",&n);  
    }  
    tail->next=NULL;  
    return l;  
}  

ListNode MergeList(LinkList *la,LinkList *lb)  
{  
    ListNode pa,pb,temp;  
    int sum=0;  
    pa=la->next;  
    pb=la->next;  
    while(pa&&pb)  
    {  
        if(pa->expn > pb->expn)  
        {  
            pa=pa->next;  
        }  
        else if(pa->expn < pb->expn)  
        {  
            temp=pa->next;
            pa->next=pb;
            pa=temp;
            pb=pb->next;
        }  
        else  
        {  
            if((pa->coef+pb->coef)!=0)  
            {  
                sum=pa->coef+pb->coef;  
                pa->coef=sum;   
                pa=pa->next;  
                pb=pb->next;  
            }  
            else  
            {  
                pa=pa->next;  
                pb=pb->next;  
            }  
        }  
    }  
    if(pb)  
        ra->next=pb;  
    free(lb);  
    return la;  
}  

void Display(LinkList *L)
{
    LNode p;
    p=L->next;
    if(p!=NULL)  
    {
        if(p->coef<0)
            printf("(%d)*x^%d",p->coef,p->expn);
        else
            printf("%d*x^%d",p->coef,p->expn);
        p=p->next;  
    }
    if(p!=NULL)
    {
        if(p->coef<0)
            printf("+(%d)*x^%d",p->coef,p->expn);
        else
            printf("+%d*x^%d",p->coef,p->expn);
        p=p->next;
    }
}

int main()  
{  
    LinkList m,n,l;  

    printf("the first equation:");  
    CreateFromTail(m);
    Display(l);
    printf("\n");
    
    printf("the second equation:");  
    CreateFromTail(n);
    Display(l);
    printf("\n");

    l=MergeList(m,n);  
    Display(l);
    return 0;
}
搜索更多相关主题的帖子: 链表 多项式 
2008-05-17 15:39
diaoxue
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-6-1
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>  

typedef struct LNode  
{  
    int coef;  
    int expn;
    struct LNode *next;
}LNode,*LInkList;

LinkList CreateFromTail(LinkList *L)  
{
    int m;  
    int n;  
    l=(LinkList)malloc(sizeof(LinkList));  
    l->next=NULL;
    tail=l;
    printf("\nplease put the coef:");  
    scanf("%d",&m);  
    printf("\nplease put the expn:");  
    scanf("%d",&n);  
    while(m)  
    {  
        p=(LinkList)malloc(sizeof(LinkList));  
        p->coef=m;  
        p->expn=n;  
        tail->next=p;  
        tail=p;  
        printf("\nplease put the coef:");  
        scanf("%d",&m);  
        printf("\nplease put the expn:");  
        scanf("%d",&n);  
    }  
    tail->next=NULL;  
    return l;  
}  

ListNode MergeList(LinkList *la,LinkList *lb)  
{  
    ListNode pa,pb,temp;  
    int sum=0;  
    pa=la->next;  
    pb=la->next;  
    while(pa&&pb)  
    {  
        if(pa->expn > pb->expn)  
        {  
            pa=pa->next;  
        }  
        else if(pa->expn < pb->expn)  
        {  
            pb->next=pa->next;
            pa->next=pb;
            pa=pa->next;
            pb=pb->next;
        }  
        else  
        {  
            if((pa->coef+pb->coef)!=0)  
            {  
                sum=pa->coef+pb->coef;  
                pa->coef=sum;   
                pa=pa->next;  
                pb=pb->next;  
            }  
            else  
            {  
                pa=pa->next;  
                pb=pb->next;  
            }  
        }  
    }  
    if(pb)  
        pa->next=pb;  
    free(lb);  
    return la;  
}  

void Display(LinkList *L)
{
    LNode p;
    p=L->next;
    if(p!=NULL)  
    {
        if(p->coef<0)
            printf("(%d)*x^%d",p->coef,p->expn);
        else
            printf("%d*x^%d",p->coef,p->expn);
        p=p->next;  
    }
    if(p!=NULL)
    {
        if(p->coef<0)
            printf("+(%d)*x^%d",p->coef,p->expn);
        else
            printf("+%d*x^%d",p->coef,p->expn);
        p=p->next;
    }
}

int main()  
{  
    LinkList m,n,l;  

    printf("the first equation:");  
    CreateFromTail(m);
    Display(l);
    printf("\n");
   
    printf("the second equation:");  
    CreateFromTail(n);
    Display(l);
    printf("\n");

    l=MergeList(m,n);  
    Display(l);
    return 0;
}
改了下,还是不行。

上善若水,水善利万物而不争,处众人之所恶
2008-05-18 15:07
快速回复:一元稀疏多项式 链表
数据加载中...
 
   



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

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