• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PTA7-6 C语言多项式的加法,单链表phead的两种实现

武飞扬头像
程序员早早
帮助1

题目

学新通

C语言两种实现

头节点保存数据

// polyA和polyB多项式相加,每个输入分别有系数和指数,以0,0结束
//相加规则,每个多项式按照指数从小到大展示,同指数的系数相加,系数为0则该项移除。
//head为第一个有数节点
#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
    int coef;
    int exp;
    struct Node *next;
} *node;

node createList() {
    int coef, exp;
    node head,a, b; 
    head = NULL;
    while (1)
    {
        scanf("%d,%d", &coef, &exp);
        if(coef==exp && exp==0) {
            break;
        }
        b = (node)malloc(sizeof(node));
        b->coef = coef;
        b->exp = exp;
        b->next = NULL;
        if(head==NULL) {
            a=head=b;
        } else {
            a->next = b; 
            a = b;
        }
    }
    return head;
}
//冒泡排序,指数小在前
void sortExp(node head) {
    node a,b; 
    int coef, exp;
    for (a=head; a->next !=NULL; a = a->next)
    {
        for(b = a->next; b != NULL; b = b->next){
            if(b->exp < a->exp) {
                coef = a->coef;
                exp = a->exp;
                a->coef = b->coef;
                a->exp = b->exp;
                b->coef = coef;
                b->exp = exp;
            }
        }
    }
    
}

node polyAdd(node head1, node head2) {
    node a1, b1, a2, b2, head, p; 
    a1 = head1;
    b1 = a1->next;
    a2 = head2;
    b2 = a2->next;
    head = (node)malloc(sizeof(node));
    head->next = NULL;
    p = head;
    while ( a1 != NULL && a2 != NULL)
    {
        if (a1->exp < a2->exp) {
            p->next = a1;
            a1 = a1->next;
            p = p->next;
        } else if (a2->exp < a1->exp) {
            p->next = a2;
            a2 = a2->next;
            p = p->next;
        } else if (a1->coef a2->coef==0) {
            node tmp1 = a1->next;
            node tmp2 = a2->next;
            free(a1);
            free(a2);
            a1 = tmp1;
            a2 = tmp2;
        } else {
            a1->coef  = a2->coef;
            p->next = a1;
            a1 = a1->next;
            node tmp = a2->next;
            free(a2);
            a2 = tmp;
            p = p->next;
        }
    }
    if (a1 != NULL) {
        p->next = a1;
    } else {
        p->next = a2;
    }
    return head->next;
}

void print(node head) {
    node p = head;
    while(p) {
        printf("%d,%d ", p->coef, p->exp);
        p = p->next;
    }
}

int main() {
    node h1, h2,h;
    h1 = createList();
    sortExp(h1);
    h2 = createList();
    sortExp(h2);
    h = polyAdd(h1, h2);
    print(h);
    return 0;
}
学新通

头节点哨兵占位

// polyA和polyB多项式相加,每个输入分别有系数和指数,以0,0结束
//相加规则,每个多项式按照指数从小到大展示,同指数的系数相加,系数为0则该项移除。
//head->next为第一个有数节点
#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
    int coef;
    int exp;
    struct Node *next;
} *node;

void createList(node head) {
    int coef,exp;
    node a,b; 
    a = head;
    while (1)
    {
        scanf("%d,%d", &coef, &exp);
        if(coef==exp&&exp==0) {
            break;
        }
        b = (node)malloc(sizeof(node));
        b->coef = coef;
        b->exp = exp;
        b->next = NULL;
        a->next = b;
        a = b; 
    }
    
}

//冒泡排序,指数小在前
void sortExp(node head) {
    node a, b; 
    int coef, exp;
    for(a = head->next; a->next; a = a->next) {
        for(b=a->next; b; b = b->next) {
            if(b->exp < a->exp) {
                coef = b->coef;
                exp = b->exp;
                a->coef = b->coef;
                a->exp = b->exp;
                b->coef = coef;
                b->exp = exp;
            }
        }
    }
}

void print(node head) {
    node p = head->next;
    while (p->next)
    {
        printf("%d,%d ", p->coef, p->exp);
        p = p->next;
    }
    printf("%d,%d", p->coef, p->exp);
    
}

node polyAdd(node h1, node h2) {
    node a, b, head, p;
    a = h1->next;
    b = h2->next;
    head = (node)malloc(sizeof(node));
    head->next = NULL;
    p = head;
    while (a && b)
    {
        //a指数小
        if (a->exp < b->exp) {
            p->next = a;
            a = a->next;
            p = p->next;
        }
        //b指数小
        else if (b->exp < a->exp) {
            p->next = b; 
            b = b->next;
            p = p->next;
        }
        //指数相等,系数和为0
        else if (a->coef b->coef==0) {
            node t1 = a->next;
            node t2 = b->next;
            free(a);
            free(b);
            a = t1;
            b = t2;
        }
        //指数相等,系数和不为0
        else {
            a->coef  = b->coef;
            p->next = a;
            p = p->next;
            a = a->next;
            node t = b->next;
            free(b);
            b = t;
        }
    }
    //剩余节点连接
    if (a) {
        p->next = a;
    } else {
        p->next = b; 
    }
    return head;
}

int main() {
    node h1, h2, h;
    h1 = (node)malloc(sizeof(node));
    h2 = (node)malloc(sizeof(node));
    h1->next = NULL;
    h2->next = NULL;
    createList(h1);
    sortExp(h1);
    createList(h2);
    sortExp(h2);
    h = polyAdd(h1, h2);
    print(h);
    return 0;
}
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfijcei
系列文章
更多 icon
同类精品
更多 icon
继续加载