多项式加法——java静态链表

多项式的加法

import java.util.Scanner;

public class PoLynomial {
    private static Note []notes = new Note[10000];

    /*
    * 打印链表
    * */
    private static void show(int head) {
        int i = notes[head].cursor;
        System.out.print("多项式 = ");
        while (notes[i].cursor != 0) {
            System.out.print(notes[i].p+"x^"+notes[i].e+" + ");
            i = notes[i].cursor;
        }
        System.out.println(notes[i].p+"x^"+notes[i].e);
    }
    /*
    * 回收节点(将指定的节点再链接回未使用的链表)
    * */
    private static void free(int i) {
        notes[i].cursor = 0;
        notes[0].cursor = i;
    }
    /*
    * 分配节点
    * */
    private static int malloc() {
        if (notes[0].cursor == 0) {
            return -1;
        }
        int i = notes[0].cursor;
        notes[0].cursor = notes[i].cursor;
        return i;
    }
    /*
    * 将未使用的数组链接成一个链表
    * */
    private static  void initLinkList() {
        for (int i = 0; i < notes.length - 1; i ++) {
            notes[i] = new Note(i + 1);
        }
        notes[notes.length - 1] = new Note(0);
    }
    /*
    * 按序建立链表
    * */
    private static int orderCreate() {
        Scanner sc = new Scanner(System.in);
        int head = malloc();
        notes[head].cursor = 0;
        int n = sc.nextInt();
        for ( int i = 0; i < n; i ++) {
            int p = sc.nextInt();
            int e = sc.nextInt();
            int j = malloc();
            notes[j].p = p;
            notes[j].e = e;
            int b = head;
            int k = notes[head].cursor;
            while ( k != 0 && e > notes[k].e) {
                b = k;
                k = notes[k].cursor;
            }
            notes[j].cursor = notes[b].cursor;
            notes[b].cursor = j;
        }
        return head;
    }
    /*
    * 合并多项式
    * 1、按指数从小到大的顺序分别建立两个多项式链表
    * 2、按序指数从小到大(指数相同的则时两个系数相加,如果相加结果为零则两个节点都不要,不为零则要链表a的节点)合并两个链表
    * */
    public static void main(String[] args) {
        initLinkList();
        //1、
        int heada = orderCreate();
        int headb = orderCreate();
        show(heada);
        show(headb);
        //2、
        int a = notes[heada].cursor;
        int b = notes[headb].cursor;
        int head = heada;
        int tail = head;
        while (a != 0 && b != 0) {
            if ( notes[a].e > notes[b].e ) {
                int n = notes[b].cursor;
                notes[b].cursor = 0;
                notes[tail].cursor =  b;
                tail = b;
                b = n;
            } else if ( notes[a].e < notes[b].e ) {
                int n = notes[a].cursor;
                notes[a].cursor = 0;
                notes[tail].cursor = a;
                tail = a;
                a = n;
            } else {
                int p = notes[a].p + notes[b].p;
                if ( p == 0 ){
                    int na = notes[a].cursor;
                    int nb = notes[b].cursor;
                    free(a);
                    free(b);
                    a = na;
                    b = nb;
                } else {
                    int na = notes[a].cursor;
                    int nb = notes[b].cursor;
                    notes[a].p = p;
                    notes[a].cursor = 0;
                    notes[tail].cursor = a;
                    tail = a;
                    free(b);
                    a = na;
                    b = nb;
                }
            }
        }
        if ( a != 0) {
            notes[tail].cursor = a;
        } else {
            notes[tail].cursor = b;
        }
        show(head);

    }
    /*
    * 节点
    * */
    static class Note {
        int p;    //系数
        int e;    //指数
        int cursor;  //指向下一个节点的指针

        public Note(int cursor) {
            this.cursor = cursor;
        }
    }
}


发布了57 篇原创文章 · 获赞 55 · 访问量 1958

猜你喜欢

转载自blog.csdn.net/qq_40561126/article/details/104242075