| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 15418 人关注过本帖, 1 人收藏
标题:怎么用链表实现多项式的相加
只看楼主 加入收藏
tianxiao110
Rank: 1
等 级:新手上路
帖 子:63
专家分:1
注 册:2010-7-23
结帖率:83.33%
收藏(1)
 问题点数:0 回复次数:4 
怎么用链表实现多项式的相加
谁能帮帮我编个单链表的程序, 实现多项式相加 A=7+3X+9X*X*X; B=8X+22X*X; (我不会写X的次方,分开写了,希望能理解)
这不是作业贴,我刚刚开始学数据库(C),希望能有人帮我。
搜索更多相关主题的帖子: 链表 多项式 相加 
2010-09-20 16:16
tianxiao110
Rank: 1
等 级:新手上路
帖 子:63
专家分:1
注 册:2010-7-23
收藏
得分:0 
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
typedef struct list
{
    int num;
    int num2;
    struct list *next;
}*link;
/*
链表实现多项式的相加
*/
//建立单链表
link creat_list(int n)
{
    link p1,p2,L;
    int i;
    p2=p1=(link)malloc(sizeof(*p1));
    scanf("%d,%d",&p1->num,&p1->num2);
    L=p1;
    for(i=1;i<n;i++)
    {
         p1=(link)malloc(sizeof(*p1));
         scanf("%d,%d",&p1->num,&p1->num2);
         p2->next=p1;
         p2=p1;
    }
    p1->next=NULL;
    return(L);
}
//单链表的输出函数
void print_list(link head)
{
    link p;
    p=head;
   
    while(p)
    {   
        printf("%d,%d\n",p->num,p->num2);
        
        p=p->next;
   
    }
}
//链表相加函数
link add_list(link head1,link head2)
{}
int main()
{
    link headA,headB;
    printf("请输入第一个多项式\n");
    headA=creat_list(4);
    printf("A\n");
    print_list(headA);
    printf("请输入第二个多项式\n");
    headB=creat_list(3);
    printf("B\n");
    print_list(headB);
    return(0);
}
谁能把相加函数补充完整也好啊  
2010-09-20 16:37
樵头
Rank: 2
来 自:四川广安
等 级:论坛游民
帖 子:7
专家分:28
注 册:2010-9-22
收藏
得分:0 
#include <stdio.h>
#include <malloc.h>

typedef struct
{
    int cff;//系数
    int exp;//指数
} datatype;

typedef struct Node
{
    datatype data_1;
    struct Node *next;
}listnode;//自定义struct数据类型为linklist,而不是定义linklist,所以不能在这里定义pa,pb

typedef listnode* linklist;

linklist create_list()//创建带头结点的链表,返回头结点,尾插法
{   
    linklist head=(linklist)malloc(sizeof(listnode));
    linklist  p, r;
    int xs;
    int zs;
    r=head;
    while(scanf("%d%d",&xs, &zs)
        && xs != 0
        && zs != 0)//输入00是结束
    {
        p=(linklist)malloc(sizeof(listnode));
        p->data_1.cff = xs;
        p->data_1.exp = zs;
        r->next = p;
        r = p;   
    }
    r->next = NULL;
    return (head);
}

void Select_sort(linklist l)//选择排序,由高到低
{
    linklist p, r, q;
    r = l->next;
    p = (linklist)malloc(sizeof(listnode));
    while( r != NULL)
    {  
        q = r->next;
        while( q != NULL)
        {
            if( q->data_1.exp > r->data_1.exp )
            {
                p->data_1.cff = r->data_1.cff;
                p->data_1.exp = r->data_1.exp;
                r->data_1.cff = q->data_1.cff;
                r->data_1.exp = q->data_1.exp;
                q->data_1.cff = p->data_1.cff;
                q->data_1.exp = p->data_1.exp;
            }
            q = q->next;
        }
        r = r->next;
    }
}

void merge_list(linklist l)//合并同类项
{
    linklist r, q, p;
    r = l;
    while( r != NULL && r->next != NULL)
    {  
        q = r->next;
        while( q->next != NULL && q != NULL
            && (q->data_1.exp == q->next->data_1.exp))
        {
            p = q->next;
            q->data_1.cff += p->data_1.cff;
            q->next = p->next;
            free(p);
        }
        r = r->next;
    }

    //清楚系数为0的项
    r = l;
    while(r != NULL && r->next != NULL)
    {
        if( r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
}

linklist add_list(linklist l_fir, linklist l_sec)//表一表二相加并将结果存放在表三中
{
    linklist head=(linklist)malloc(sizeof(listnode));
    linklist p, q, r, s;
    r = head;
    p = l_fir->next;
    q = l_sec->next;
    while( p != NULL && q != NULL )//表一表二中均有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        if(p->data_1.exp == q->data_1.exp)
        {
            
            s->data_1.cff = p->data_1.cff + q->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
            q = q->next;
        }
        else if(p->data_1.exp > q->data_1.exp)
        {  
            s->data_1.cff = p->data_1.cff;
            s->data_1.exp = p->data_1.exp;
            p = p->next;
        }
        else if(p->data_1.exp < q->data_1.exp)
        {
            s->data_1.cff = q->data_1.cff;
            s->data_1.exp = q->data_1.exp;
            q = q->next;
        }
        r->next = s;
        r = s;
    }
    while( p != NULL)//仅表一中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = p->data_1.cff;
        s->data_1.exp = p->data_1.exp;
        p = p->next;
        r->next = s;
        r =s;
    }
    while( q != NULL)//仅表二中有数据
    {
        s = (linklist)malloc(sizeof(listnode));
        s->data_1.cff = q->data_1.cff;
        s->data_1.exp = q->data_1.exp;
        q = q->next;
        r->next = s;
        r =s;
    }
    r->next = NULL;

    //清楚系数为0的项
    r = head;
    while(r != NULL && r->next != NULL)
    {
        if( r->next->data_1.cff == 0)
        {
            q = r->next;
            r->next = q->next;
            free(q);
        }
        r = r->next;
    }
    return (head);
}

void result_out(linklist p)//输出结果
{
    linklist k;
    k = p;
    if(p->next == NULL)
        printf("empty list\n");
    while( p->next != NULL)
    {  
        if( p->next->data_1.cff < 0)
        {
            if(p == k)
                printf("-");
            else
                printf("- ");
            printf("%d^%d ", -p->next->data_1.cff, p->next->data_1.exp);
        }
        else if(p != k && p->next->data_1.cff > 0)
        {   
            printf("+ ");
            printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
        }
        else if(p == k && p->next->data_1.cff > 0)
        {
            printf("%d^%d ", p->next->data_1.cff, p->next->data_1.exp);
        }
        p = p->next;
    }
   
}
void free_list(linklist p)//释放空间
{
   linklist k, q;
   k = p;
   while(p != NULL && p->next != NULL)
   {
       q = p->next;
       p->next = q->next;
       free(q);
   }
}

void main()
{
    linklist pa, pb, pc;
   
    printf("请输入第一个多项式:\n");
    pa = create_list();
    Select_sort(pa);
    merge_list(pa);
    printf("排序化简后的多项式为:\n");
    printf("                      \n");
    result_out(pa);
    printf("\n                    \n\n");
   
    printf("请输入第二个多项式:\n");
    pb = create_list();
    Select_sort(pb);
    merge_list(pb);
    printf("排序化简后的多项式为:\n");
    printf("                       \n");
    result_out(pb);
    printf("\n                     \n\n");
   
    printf("相加后的多项式为:\n");
    printf("                       \n");
    pc = add_list(pa,pb);
    result_out(pc);
    printf("\n                      \n\n");


   free_list(pa);
   free(pa);
   free_list(pb);
   free(pb);
   free_list(pc);
   free(pc);
}
2010-10-08 08:15
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<malloc.h>
typedef struct linklist
{
    int coef;//系数
    int exp; //指数
    struct linklist *next;
}nodelink;
nodelink *create()//创建单链表
{
    nodelink *head,*s,*t;
    int c,e;
    head=(nodelink *)malloc(sizeof(nodelink));//哨兵头结点
    head->next=NULL;
    t=head;//始终指向单链表的尾部
    printf("请输入c和e的值,当系数e等于-1为结束条件:\n");
    scanf("%d%d",&c,&e);
    while(e!=-1)
    {
        s=(nodelink *)malloc(sizeof(nodelink));
        s->coef=c;
        s->exp=e;
        s->next=NULL;
        t->next=s;
        t=s;
        printf("请继续输入c和e的值,当系数e等于-1为结束条件:\n");
         scanf("%d%d",&c,&e);
    }
    return head;
}
void printhead(nodelink *head)//输出连表数据
{
    nodelink *phead;
    phead=head->next;//指向第一个元素结点
    int pcount=1;//结点计数器
    while(phead!=NULL)
    {
        printf("第%d个结点的系数和指数分别为:",pcount);
        printf("%2d%4d\n",phead->coef,phead->exp);
        phead=phead->next;
        pcount++;
    }
    printf("\n");
}
nodelink *ADD(nodelink *heada,nodelink *headb)//实现多项式相加
{

    nodelink *p,*q,*prea,*preb,*temp;
    int sum;
    p=heada->next;//分别使p和q指向第一个元素结点
    q=headb->next;
    prea=heada;
    free(headb);//释放headb链表的头结点
    while(p!=NULL&&q!=NULL)
    {
        if(p->exp<q->exp)
        {
            prea=p;
            p=p->next;
        }
        else if(p->exp==q->exp)
        {
              sum=p->coef+q->coef;
          if(sum!=0)
          {
             p->coef=sum;
             preb=q;
             prea=p;
             p=p->next;
             q=q->next;
             free(preb);
          }
         else//如果和为零
         {
            temp=p;
            p=p->next;
            prea->next=p;
            free(temp);
            preb=q;
            q=q->next;
            free(preb);
         }
        }
        else //若p->exp大于q->exp
        {
            preb=q;
            q=q->next;
            preb->next=p;
            prea->next=preb;
            prea=prea->next;
        }
    }
    if(q!=NULL)
    prea->next=q;
    return heada;
}
void main()
{
    nodelink *heada,*headb,*headc;
    heada=create();
    printhead(heada);
    headb=create();
    printhead(headb);
    headc=ADD(heada,headb);
    printf("相加后的的链表为:\n");
    printhead(headc);
}
    
2010-10-08 09:52
遮天云
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:农村一小伙
等 级:贵宾
威 望:12
帖 子:1132
专家分:2671
注 册:2010-6-1
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef struct node{
int xi;
int zi;
struct node *next;
}Node;

Node *Creat()
{ //尾插法建立多项式链表
Node *head,*p,*pre;
int fg,tag;
head=(Node *)malloc(sizeof(Node));
head->next=NULL;
pre=head;
printf("请按照指数从小到大输入多项式并以0 0结束:\n");
scanf("%d%d",&fg,&tag);
while(fg)
{
p=(Node *)malloc(sizeof(Node));
p->xi=fg;
p->zi=tag;
p->next=pre->next;
pre->next=p;
pre=p;
scanf("%d%d",&fg,&tag);
}
if(fg==0&&tag==0)
printf("\n输入结束:\n");
return head;
}

void Read(Node *head) //读取链表中的数据
{
Node *p=head->next;
while(p)
{
printf("%dX~%d->",p->xi,p->zi);
p=p->next;
}
printf("NULL\n");
}

Node *Add(Node *head1,Node *head2)
{ //多项式相加的实现
Node *p,*head,*p1,*p2;int sum;
head=(Node *)malloc(sizeof(Node));
p=head;
p1=head1->next;
p2=head2->next;
while(p1&&p2) //当两多项式都存在时
{
if(p1->zi==p2->zi) //指数是否相等 相等怎么处理
{
sum=p1->xi+p2->xi;
if(sum)
{
p1->xi=sum;
p->next=p1;
p=p1;
}
p1=p1->next;
p2=p2->next;
}
else //不相等分两种情况
{
if(p1->zi<p2->zi)
{ //分别怎么处理
p->next=p1;
p=p1;
p1=p1->next;
}
else
{
p->next=p2;
p=p2;
p2=p2->next;
}

}
}
if(p1) p->next=p1; //将1中剩余结点接到和链表中 因为最终只剩下一段链表多项式,只需要将
else p->next=p2; //将2中剩余结点接到和链表中 这段链的链头接到目标链表就可以了
return head;
}

void main()
{ //主函数
Node *head,*p1,*p2;
p1=Creat();
Read(p1);
p2=Creat();
Read(p2);
head=Add(p1,p2);
printf("相加后为:\n");
Read(head);
getch();
}

这个方法不是我做的!上一个是我的
2010-10-08 09:53
快速回复:怎么用链表实现多项式的相加
数据加载中...
 
   



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

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