当0为输入时,方法冻结

时间:2014-04-03 04:45:18

标签: java logic freeze

对于编程课程,我必须使用LinkedList编写多项式类。一切都工作正常,但只要两个多项式的项包含0作为幂(7 ^ 0),该方法就会冻结。我很确定问题是在simplifyPoly()方法中产生的,特别是因为它试图将新术语添加到新的多项式中。我不明白为什么会发生这种情况。我希望有人可以回答这个问题并告诉我为什么会发生这种情况,或者我明天晚上之前如何解决这个问题,因为那是它到期的时候!提前谢谢!

public class Main3{
    public static void main(String[]args){
        Polynomial poly = new Polynomial();

        poly.addValue(2,3);
        poly.addValue(4,2);
        poly.addValue(5,1);
        poly.addValue(7,0);

        poly.printPoly();

        Polynomial poly2 = new Polynomial();

        poly2.addValue(4,3);
        poly2.addValue(6,2);
        poly2.addValue(5,0);

        poly2.printPoly();

        Polynomial sum = poly.addPoly(poly2);

        sum.printPoly();

        System.out.println("Degree of first printed Polynomial "+poly.degPoly());
    }
}

class Polynomial {
    private LinkedList list;
    private int size;

    public Polynomial(){
        this.list = new LinkedList();
        this.size = this.list.getSize();
    }

    public LinkedList getList(){
        return this.list;
    }

    public int getSize(){
        return this.size;
    }

    public int degPoly(){
        return this.list.getHead().getNext().getDegree();
    }

    public void addValue(int c, int d){
        this.list.addValue(c,d);
        this.size = this.list.getSize();
    }

    public void removeValue(int c, int d){
        this.list.deleteValue(c,d);
        this.size = this.list.getSize();
    }

    public void printPoly(){
        this.list.printList();
    }

    public Polynomial addPoly(Polynomial b){
        Polynomial one = this.simplifyPoly('a');
        Polynomial two = b.simplifyPoly('a');
        Polynomial sum = new Polynomial();
        for (Node current = one.getList().getHead().getNext(); current != one.getList().getTail(); current = current.getNext()){
            sum.addValue(current.getCoefficient(), current.getDegree());
        }
        for (Node current = two.getList().getHead().getNext(); current != two.getList().getTail(); current = current.getNext()){
            sum.addValue(current.getCoefficient(), current.getDegree());
        }
        sum = sum.simplifyPoly('a');
        return sum;
    }

    public Polynomial subPoly(Polynomial b){
        Polynomial one = this.simplifyPoly('a');
        Polynomial two = b.simplifyPoly('a');
        Polynomial difference = new Polynomial();
        for (Node current = one.getList().getHead().getNext(); current != one.getList().getTail(); current = current.getNext()){
            difference.addValue(current.getCoefficient(), current.getDegree());
        }
        for (Node current = two.getList().getHead().getNext(); current != two.getList().getTail(); current = current.getNext()){
            difference.addValue(current.getCoefficient(), current.getDegree());
        }
        difference = difference.simplifyPoly('s');
        return difference;
    }

    public Polynomial multPoly(Polynomial b){
        Polynomial one = this.simplifyPoly('a');
        Polynomial two = b.simplifyPoly('a');
        Polynomial product = new Polynomial();
        for (Node current = one.getList().getHead().getNext(); current != one.getList().getTail(); current = current.getNext()){
            for (Node current2 = two.getList().getHead().getNext(); current2 != two.getList().getTail(); current2 = current2.getNext()){
                product.addValue((current.getCoefficient() * current2.getCoefficient()), (current.getDegree() + current2.getDegree()));
            }
        }
        product = product.simplifyPoly('a');
        return product;
    }

    public Polynomial simplifyPoly(char caller){
        Polynomial temp = new Polynomial();
        Node current = this.list.getHead().getNext(); current = current.getNext();
        Node prior = this.list.getHead().getNext();
        while (prior != this.list.getTail()){
            if (prior.getDegree() == current.getDegree()){
                if (caller == 'a') {
                    int coefficient = prior.getCoefficient() + current.getCoefficient();
                    temp.addValue(coefficient, current.getDegree());
                } else if (caller == 's'){
                    temp.addValue((prior.getCoefficient() - current.getCoefficient()), current.getDegree());
                }
                prior = prior.getNext(); prior = prior.getNext();
                current = current.getNext(); current = current.getNext();
            } else {
                temp.addValue(prior.getCoefficient(), prior.getDegree());
                prior = prior.getNext();
                current = current.getNext();
            }
        }
        return temp;
    }
}

class LinkedList {
    private Node head;
    private Node tail;
    private int size;

    public LinkedList(){
        this.head = new Node(this.tail);
        this.tail = new Node(this.head);
        this.size = 0;
    }

    public Node getHead(){
        return this.head;
    }

    public Node getTail(){
        return this.tail;
    }

    public int getSize(){
        return this.size;
    }

    public boolean isEmpty(){
        return (this.head.getNext() == this.tail || this.tail.getNext() == this.head || this.size == 0);
    }



    public void addValue(int c, int d){
        Node temp = new Node(c, d, null);

        if (isEmpty()){
            temp.setNext(this.tail);
            this.head.setNext(temp);
            this.tail.setNext(temp);
        } else if (this.head.getNext().getDegree() < d){
            temp.setNext(this.head.getNext());
            this.head.setNext(temp);
        } else if (this.tail.getNext().getDegree() > d){
            temp.setNext(this.tail);
            this.tail.getNext().setNext(temp);
            this.tail.setNext(temp);
        } else {
            Node current = this.head.getNext(); current = current.getNext();
            Node prior = this.head.getNext();

            while (current.getDegree() >= d){
                //if (current.getDegree() == d){
                //  current.setCoefficient((current.getCoefficient() + c));
                //}
                current = current.getNext();
                prior = prior.getNext();
            }

            temp.setNext(current);
            prior.setNext(temp);
        }
        size++;
    }

    public void deleteValue(int c, int d){
        Node current = this.head.getNext();current = current.getNext();
        Node prior = this.head.getNext();
        if (!isEmpty()){
            while (current != this.tail){
                if (current.getDegree() == d && current.getCoefficient() == c){
                    prior.setNext(current.getNext());
                } else {
                    current = current.getNext();
                    prior = prior.getNext();
                }
            }
        }
        this.size--;
    }

    public boolean findNode(int c, int d){
        Node current = this.head.getNext();
        while (current != tail){
            if (current.getCoefficient() == c && current.getDegree() == d){
                return true;
            } else {
                current = current.getNext();
            }
        }
        return false;
    }

    public void printList(){
        if (isEmpty()) {System.out.println("No terms in equation"); return;} // breaks method if the list is empty
        Node current = this.getHead().getNext();
        while (current != this.tail){
            if (current.getNext() != this.tail){
                System.out.print(current.getCoefficient()+"x^"+current.getDegree()+" + ");
                current = current.getNext();
            } else if (current.getDegree() == 1 && this.tail != current.getNext()) {
                System.out.print(current.getCoefficient()+" + ");
                current = current.getNext();
            } else if (current.getDegree() == 1 && this.tail == current.getNext()) {
                System.out.println(current.getCoefficient());
                current = current.getNext();
            } else if (current.getDegree() == 0 && this.tail != current.getNext()) {
                System.out.print(1+" + ");
                current = current.getNext();
            } else if (current.getDegree() == 0 && this.tail == current.getNext()) {
                System.out.println(1);
                current = current.getNext();
            } else {
                System.out.println(current.getCoefficient()+"x^"+current.getDegree());
                current = current.getNext();
            }
        }
    }
}

class Node {
    private int coefficient;
    private int degree;
    private Node next;

    public Node(int c, int d, Node n){
        this.coefficient = c;
        this.degree = d;
        this.next = n;
    }

    public Node(Node n){
        this.next = n;
    }

    public int getCoefficient(){
        return this.coefficient;
    }

    public int getDegree(){
        return this.degree;
    }

    public Node getNext(){
        return this.next;
    }

    public void setCoefficient(int c){
        this.coefficient = c;
    }

    public void setDegree(int d){
        this.degree = d;
    }

    public void setNext(Node n){
        this.next = n;
    }
}

2 个答案:

答案 0 :(得分:1)

您的问题出在您的LinkedList构造函数中。

public LinkedList(){
    this.head = new Node(this.tail);
    this.tail = new Node(this.head);
    this.size = 0;
}

因此,您在此处创建了两个节点,tail处的节点指向head处的节点。这意味着在您开始向其中插入数据之前,列表是循环的。即使有两个节点,您也会断言大小为零。

但是因为列表是循环的,所以一旦你试图遍历它,你就会陷入无限循环。

答案 1 :(得分:0)

您的代码存在一些问题,我会指出一些问题,但除非您遵循David Wallace的建议并使用调试器逐步完成它,并且更好地评论它,否则它将无法使它们正常工作。 1 - 此代码

 } else if (this.tail.getNext().getDegree() > d){
            temp.setNext(this.tail);
            this.tail.getNext().setNext(temp);
            this.tail.setNext(temp);
        } else {

会使你的尾巴指向温度,指向尾部。因为你试图遍历那个列表,所以在打印你的poly2之后,你会在你的simplPoly()里面的simplPoly()方法中导致无限循环。 2 - 如果您尝试通过执行

来解决此问题
this.tail = temp

,你现在会遇到另一个无限循环 poly.addValue(5,0) 因为你正在做一个while循环,当0度大于或等于0时继续前进:

 while (current.getDegree() >= d){
            //if (current.getDegree() == d){
            //  current.setCoefficient((current.getCoefficient() + c));
            //}
            current = current.getNext();
            prior = prior.getNext();
        }

无论如何,那只是让你去。找到一个关于如何使用java调试器的好教程,以便您可以逐步完成代码并更好地理解它。