sdnu oj 1216 多项式相加 链表

终于过了, 魔鬼链表又臭又长
要学数据结构了,真是…激动

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#define mod 1000000007
using namespace std;

typedef long long ll;
const int N = 1006;
const long long inf = 0x3f3f3f3f;
const double eps = 1e-5;
const double pi = acos(-1);

struct node
{
    int coef, expo;         //系数,指数
    struct node *next;
};

node *creat(int n)     //创建 n 项多项式, 返回头指针
{
    node *head, *q, *pre;
    head = (node*)malloc(sizeof (node)); //理论上是 sizeof (struct node) , 不知道为什么这样也行,就懒得写了
    head -> next = NULL;        //理论初始化
    for(int i = 1; i <= n; ++i)
    {
        node *tp = (node*)malloc(sizeof (node));
        scanf("%d%d", &tp -> coef, &tp -> expo); //一定要加 & 符号, 不知道为啥最近总忘
        tp -> next = NULL;
        pre = head;
        q = head -> next;
        while(q && q -> expo > tp -> expo)  //按指数插入,最终指数 pre > tp, q <= tp
        {
            pre = q;
            q = q -> next;
        }
        if(!q || q -> expo != tp -> expo)
        {
            tp -> next = q;
            pre -> next = tp;
        }
        else
        {
            int sum = q -> coef + tp -> coef;
            if(sum)
                q -> coef = sum;
            else
            {
                delete q;     //清空,详见[https://www.cnblogs.com/matrix-r/archive/2012/10/05/2712022.html](https://www.cnblogs.com/matrix-r/archive/2012/10/05/2712022.html)
                delete tp;
            }
        }
    }
    return head;
}

node* add(node *a, node *b)     //貌似还可以引用, &
{
    node *p1, *p2, *p3, *tp;
    node *head;
    p3 = (node*)malloc(sizeof (node)); //申请新空间啊啊啊啊,或者 p3 = a ,在原基础上修改
    p3 -> next = NULL;
    head = p3;
    p1 = a -> next;
    p2 = b -> next;
    while(p1 && p2)
    {
        if(p1 -> expo == p2 -> expo)
        {
            int sum = p1 -> coef + p2 -> coef;
            if(sum != 0)
            {
                p1 -> coef = sum;
                p3 -> next = p1;
                p3 = p1;
                p1 = p1 -> next;
                tp = p2;
                p2 = p2 -> next;
                delete tp;
            }
            else
            {
                tp = p1;
                p1 = p1 -> next;
                delete tp;
                tp = p2;
                p2 = p2 -> next;
                delete tp;
            }
        }
        else if(p1 -> expo > p2 -> expo)
        {
            p3 -> next = p1;
            p3 = p1;
            p1 = p1 -> next;
        }
        else
        {
            p3 -> next = p2;
            p3 = p2;
            p2 = p2 -> next;
        }
    }
    if(!p1)
    {
        while(p2)            // 把后面的接上
        {
            p3 -> next = p2;
            p3 = p2;
            p2 = p2 -> next;
        }
        p3 -> next = NULL;
    }
    else
    {
        while(p1)
        {
            p3 -> next = p1;
            p3 = p1;
            p1 = p1 -> next;
        }
        p3 -> next = NULL;
    }
    return head;
}

int main()
{
    int m, n;
    scanf("%d%d",&m, &n);
    node *a = creat(m);
    node *b = creat(n);
    node *c = add(a, b);
    while(c -> next)
    {
        c = c -> next;
        int coeff = c -> coef;
        int expoo = c -> expo;
        printf("%d %d\n", coeff, expoo);
    }
    return 0;
}

发布了40 篇原创文章 · 获赞 4 · 访问量 1124

猜你喜欢

转载自blog.csdn.net/xiongshuxian2019/article/details/104467916