C:数据结构实验五

问题描述】编程实现一元多项式的加法运算。

【输入形式】第一个一元多项式A; 第二个一元多项式B。 以(0,0)作为输入结束。

【输出形式】多项式A和多项式B的和。

【样例输入】

    5,3 7,8 9,15 0,0

    2,0 6,3 -7,8 0,0

【样例输出】

    2x^0+11x^3+9x^15

1.合并同类项,对系数处理

2.排序,交换的内容是系数指数都要换

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

typedef struct Term
{
    float coef;
    int expn;
    struct Term   *next;
}*LinkList;
LinkList creat_list()
{
    LinkList L=(LinkList) malloc(sizeof (Term));
    L->next=NULL;
    Term *q=L,*p;
    float cof;
    int exp;
    scanf("%f,%d",&cof,&exp);
    while (cof!=0||exp!=0)
    {
        p=(LinkList) malloc(sizeof (Term));
        p->next=NULL;
        p->coef=cof;
        p->expn=exp;
        q->next=p;
        q=p;
        scanf("%f,%d",&cof,&exp);
    }

    return L;
}
void sum(LinkList &l2,LinkList &l1)
{
    Term *p1,*temp;
    while (l2->next)
    {
        p1=l1;
        while (p1->next&&p1->next->expn!=l2->next->expn!=NULL)
        {
            p1=p1->next;
        }
        temp=l2->next;
        l2->next=temp->next;
        temp->next=NULL;
        if(p1->next==NULL)
        {
            temp->next=p1->next;
            p1->next=temp;
        }else{
            p1->next->coef+=temp->coef;
            free(temp);
            if(p1->next->coef==0){
                temp=p1->next;
                p1->next=temp->next;
                free(temp);
            }
        }

    }
    free(l2);
}
void sort(LinkList &h)
{
    Term* p, * q;
    p = h->next;
    while (p)
    {
        q = p->next;
        while (q)
        {
            if (q->expn < p->expn)
            {
                //交换内容
                int t = q->expn;
                int t2=q->coef;
                q->coef=p->coef;
                q->expn = p->expn;
                p->coef=t2;
                p->expn = t;
            }
            q = q->next;
        }
        p = p->next;
    }

}


void Print(LinkList L)
{
        if (!L || !L->next) {
            printf("0");
        } else {
            Term* t = L->next;
            printf("%dx^%d", (int)t->coef, t->expn);
            t = t->next;
            while (t) {
                if (t->coef > 0) {
                    printf("+%dx^%d", (int)t->coef, t->expn);
                } else if (t->coef < 0) {
                    printf("%dx^%d", (int)t->coef, t->expn);
                }
                t = t->next;
            }
        }
}
int main() {
    LinkList l1=creat_list();
    LinkList l2=creat_list();
    sum(l1,l2);
    sort(l2);
    Print(l2);
    free(l2);
    return 0;
}

【问题描述】
已知非空线性链表第1个链结点指针为list,链结点构造为
struct node{
    datatype data;
    node *link;
};
请写一算法,将该链表中数据域值最大的那个点移到链表的最后面。(假设链表中数据域值最大的链结点惟一)(注意:要求先写出算法的解题思路,然后再写出算法)
【输入形式】
输入为一个整数序列,整数之间以空格隔开,序列以回车结尾,输入-1结束(假设链表中吴数据-1)。
【输出形式】
输出为移动后的整数序列,整数之间以空格隔开,序列以回车结尾。
【样例输入】
3 12 4 9 5 1 -1
【样例输出】
3 4 9 5 1 12
【样例说明】
将序列中最大的数字12移动到序列最后。

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

typedef int datatype;
struct node{
    datatype data;
    node *link;
};
node* creat_list()
{
    struct node* list=(node*) malloc(sizeof (node));
    list->link=NULL;
    datatype a;
    node *q=list;
    scanf("%d",&a);
    while (a!=-1)
    {
        struct node* p=(node*) malloc(sizeof (node));
        p->data=a;
        p->link=NULL;
        q->link=p;
        q=q->link;
        scanf("%d",&a);
    }
    return list;
}
node* find_max(node* l)
{
    l=l->link;
    node *max,*p=l;
    max=(node*) malloc(sizeof (node));
    if(p==NULL||max==NULL)
    {
        return max;
    }
    while (p)
    {
        if(max->data<p->data)
        {
            max->data=p->data;
        }
        p=p->link;
    }
    max->link=NULL;
//    printf("%d",max->data);
    return max;
}
void Print(node* l)
{
    l=l->link;
    while (l)
    {
        printf("%d",l->data);
        if(l->link)
        {
            printf(" ");
        }
        l=l->link;
    }
}
void re_sort(node* l)
{
    node *max=find_max(l),*p,*q;
    struct node* nl=(node*) malloc(sizeof (node));
    nl->link=NULL;
    l=l->link,q=l;
    p=nl;
    while (l)
    {
        if(l->data==max->data)
        {
            l=l->link;
        } else{
            p->link=l;
            p=p->link;
            l=l->link;
        }

    }
    p->link=max;

    Print(nl);
    free(max);
    free(nl);
}

int main() {
    node* list=creat_list();
    re_sort(list);
    free(list);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值