zoukankan      html  css  js  c++  java
  • 链表实现多项式相加

    function Node(coef,expon) {
        this.coef = coef; // 系数
        this.expon = expon; // 指数
        this.next = null;
    }
    List.attach = function(node) {
        let current = this.head;
        while(current.next) {
            current =  current.next;
        }
        current.next = node;
        return this;
    }
    List.add = function(list1,list2) {  // 两个多项式 list
        let list_out = new List(), t1 = list1.head, t2 = list2.head;
        console.log(t1,t2);
        let current = list_out.head;
        
        while(t1 && t2) {
            while(current.next) {
                current = current.next;
            }
            current.next =  new Node();
            if(t1.expon == t2.expon) {
                current.next.expon = t1.expon;
                current.next.coef = t1.coef + t2.coef;
                t1 = t1.next;
                t2 = t2.next;
            } else if(t1.expon > t2.expon) {
                current.next.expon = t1.expon;
                current.next.coef = t1.coef;
                t1 = t1.next;
            } else {
                current.next.expon = t2.expon;
                current.next.coef = t2.coef;
                t2 = t2.next;
            }
        }
        
        while(t1) {
            while(current.next) {
                current = current.next;
            }
            current.next =  new Node();
            current.next.expon = t1.expon;
            current.next.coef = t1.coef;
            t1 = t1.next;
        }
        while(t2) {
            while(current.next) {
                current = current.next;
            }
            current.next =  new Node();
            current.next.expon = t2.expon;
            current.next.coef = t2.coef;
            t2 = t2.next;
        }
        list_out.head = list_out.head.next;
        return list_out.head;
    }
    function List() {
        this.head = new Node();
        this.attach = List.attach;
        this.add = List.add;
    }
    // 3x^4 - 5x^2 + 6x - 2
    // 5x^20 - 7x^4 + 3x
    let arr1 = [[3,4],[-5,2],[6,1],[-2,0]]
    let list1 = new List();
    arr1.forEach((item,index) => {
        list1.attach(new Node(item[0],item[1]))
    })
    list1.head = list1.head.next;
    
    let arr2 = [[5,20],[-7,4],[3,1]]
    let list2 = new List();
    arr2.forEach((item,index) => {
        list2.attach(new Node(item[0],item[1]))
    })
    list2.head = list2.head.next;
    
    let list_out = list1.add(list1,list2);
    let result = '';
    while(list_out) {
        if(!result) {
            result += `(${list_out.coef}x^${list_out.expon})`;
        } else {
            result += `+(${list_out.coef}x^${list_out.expon})`;
        }
        list_out = list_out.next;
    }
    console.log("多项式之和 = " + result);
    // 亲测有效,多指教!
  • 相关阅读:
    利用Openssh后门 劫持root密码
    CentOS 6.9 升级OpenSSH版本 关闭ssh服务后门
    CentOS 7 实现zabbix agent 自动添加,并链接到指定的模版
    CentOS 7 zabbix添加监控服务器
    CentOS 7 Squid代理服务器反向代理
    CentOS 7 Squid代理服务器正向代理-透明代理
    CentOS 7 Squid代理服务器正向代理-传统代理
    CentOS 7 搭建Squid代理服务器
    CentOS 7 配置DHCP中继代理服务
    CentOS 7 配置DHCP
  • 原文地址:https://www.cnblogs.com/strivegys/p/13197063.html
Copyright © 2011-2022 走看看