7-1(链表) 一元多项式的乘法与加法运算(20 point(s))

来源:互联网 发布:淘宝宝贝分类模板 编辑:程序博客网 时间:2024/06/16 03:38

既然说让用链表做,那就用链表做╭(╯^╰)╮


加法是归并算法


乘法是第一个式子的每一项和第二个的整串相加


挺恶心的代码如下:


#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;typedef struct node{    int value,mi;    node *next;}*LinkList,node;void InitList(LinkList &L){   L = new node;   L->mi = -1;   L->value = -1;    L -> next = NULL;}int n,x,y;void Input(LinkList &h1, LinkList &h2){    LinkList l1 = h1;    LinkList l2 = h2;    scanf("%d",&n);    for(int i =0 ; i < n ; i ++){        scanf("%d%d",&x,&y);        node *no;        no = new node;        no->value = x;        no->mi = y;        no->next = NULL;        l1->next = no;        l1 = l1->next;    }    scanf("%d",&n);    for(int i =0 ;i < n ; i++){        scanf("%d%d",&x,&y);        node *no;        no = new node;        no->value = x;        no->mi = y;        no->next = NULL;        l2->next = no;        l2 = l2->next;    }}LinkList add_list(LinkList h1, LinkList h2){    LinkList r1 = h1;    LinkList  r2 = h2;    LinkList l3;InitList(l3);    LinkList h3 = l3;    if(h1->next == NULL){        return r2;    }if(h2->next == NULL){        return r1;    }    r1 = r1->next;r2 = r2->next;    while(r1 != NULL && r2 != NULL){        if(r1->mi > r2->mi){            l3->next = r1;            l3 = l3->next;            r1 = r1->next;        }else if(r1->mi < r2->mi){             l3->next = r2;            l3 = l3->next;            r2 = r2->next;        }else{            node *tmp = new node;            tmp->mi = r1->mi;            tmp->value = r1->value + r2->value;            if(tmp->value == 0){                r1 = r1->next;                r2 = r2->next;            }else{                r1 = r1->next;                r2 = r2->next;                l3->next = tmp;                l3 = l3->next;            }        }    }    if(r1 != NULL){        while(r1 != NULL){            l3 -> next = r1;            l3 = l3->next;            r1 = r1->next;        }    }    if(r2 != NULL){        while(r2 != NULL){            l3 -> next = r2;            l3 = l3->next;            r2 = r2->next;        }    }    return h3;}LinkList multi_list(LinkList h1, LinkList h2){    LinkList l3; InitList(l3);     LinkList l1 = h1;     l1 =  l1->next;    while(l1 != NULL){        LinkList l4;        InitList(l4);        LinkList h4 = l4;        LinkList l2 = h2;        l2 = l2->next;        while(l2 != NULL){            node* no;            no = new node;            no->mi = l1->mi+l2->mi;            no->value = l1->value * l2->value;            l4->next = no;            l4 = l4->next;            l2 = l2->next;        }        l4->next = NULL;        l1 = l1 -> next ;        LinkList hh4 = h4;        l3 = add_list(l3,hh4);    }    return l3;}void Output(LinkList h){    h = h->next;    if(h == NULL){        printf("0 0");    }    else{        int cnt = 0;        while(h != NULL){            if(!cnt){                cout<<h->value<<" "<<h->mi;            }            else{                cout<<" "<<h->value<<" "<<h->mi;            }            cnt++;            h = h->next;        }    }}int main(){    LinkList h1,h2;    InitList(h1);    InitList(h2);    Input(h1,h2);   LinkList h4 = multi_list(h1,h2);   LinkList h3 = add_list(h1,h2);   Output(h4);   printf("\n");   Output(h3);    return 0;}

不努力只能看别人的脸色

阅读全文
0 0