动态列表方法的时间和内存复杂性,Java

时间:2014-01-25 18:45:17

标签: java list dynamic linked-list complexity-theory

请帮助我了解所有这些类方法的O时间和内存复杂性。

第一类是表示多项式成员的节点。

     public class PolyNode {

     private int _power; 
     private double _coefficient; 
     private PolyNode _next;

     public PolyNode(int power,double coefficient, PolyNode next) {

         if (power < 0 || coefficient < 0) {
             _power = 0;
             _coefficient = 0;
         }
         _coefficient = coefficient;
         _power = power;
         _next = next;
     }

     public PolyNode(int power, double coefficient) {

         if (power < 0 || coefficient < 0) {
             _power = 0;
             _coefficient = 0;
         }
         _power = power;
         _coefficient = coefficient;
         _next = null;
     }

     public PolyNode(PolyNode p) {

         _power = p._power;
         _coefficient = p._coefficient;
         _next = null;
     }

     public int getPower() {

         return _power;
     }

     public double getCoefficient() {

         return _coefficient;
     }

     public PolyNode getNext() {

         return _next;
     }

     public void setPower(int power) {

         _power = power;
     }

     public void setCoefficient(double coefficient) {

         _coefficient = coefficient;
     }

     public void setNext(PolyNode next) {

         _next = next;
     }
     public String toString() {

         if(_coefficient == 0) {
             return "";
         }
         if(_power == 0) {
             return "" + _coefficient;
         }

         if(_coefficient == 1) {
             if(_power == 1) {
                 return "x";
             } else {
                 return "x^" + _power;
             }

         }
         if(_coefficient == -1) {
             if(_power == 1) {
                 return "-x";
             } else {
                 return "-x^" + _power;
             }
         }

         if(_power == 1) {
             return _coefficient + "x";
         } else {
             return _coefficient + "x^" + _power;
         }
     }

 }  

第二类是动态列表

 public class Polynom {

     public PolyNode _head;

     public Polynom () {
         _head = null;
     }

     public Polynom (PolyNode p) {
         _head = p;
     }

     public Polynom addNode(PolyNode p) {

         if (_head == null) {

             _head = p;

         } else {

             PolyNode privew = null, next = _head;

             while (next != null) {

                 if(next.getPower() < p.getPower()) {

                     p.setNext(next);

                     if(privew == null) {
                         _head = p;
                     } else {
                         privew.setNext(p);
                     }

                     break;
                 }

                 if(next.getNext() == null) {

                     next.setNext(p);

                     break;
                 }

                 privew = next;
                 next = next.getNext();

             }  

         }
         return this;
     }


     public Polynom multByScalar (int num) {

         PolyNode runner = _head;

         while(runner != null) {

             runner.setCoefficient(runner.getCoefficient()*num);

             runner = runner.getNext();
         }
         return this;
    }

    public Polynom addPol (Polynom other) {

        PolyNode a = _head , b = other._head;

        while(b != null) {

            if(a == null) {
                while(b != null) {
                    addNode( new PolyNode(b) );
                    b = b.getNext();
                }
                break;
            }
            if(a.getPower() < b.getPower()) {

                addNode( new PolyNode(b) );

                b = b.getNext();
            }
            else if(a.getPower() > b.getPower()) {

                a = a.getNext();
            }
            else if(a.getPower() == b.getPower()) {

                a.setCoefficient( a.getCoefficient() + b.getCoefficient() );

                b = b.getNext();
            }

        }
        return this;
    }

    public Polynom multPol (Polynom other) {

        PolyNode a = _head;
        Polynom AllParamByAll = new Polynom();

        while(a != null) {

            PolyNode b = other._head;
            Polynom paramByAll = new Polynom();

            while(b != null) {

                int power = a.getPower() + b.getPower(); 
                double coefficient = a.getCoefficient() * b.getCoefficient();
                paramByAll.addNode( new PolyNode(power,coefficient) );

                b = b.getNext();
            }

            AllParamByAll.addPol(paramByAll);

            a = a.getNext();

        }

        _head = AllParamByAll._head;

        return this;
    }

    public Polynom differential () {

         PolyNode privew = null, next = _head;

         while (next != null) {

             int power = next.getPower() - 1; 
             double coefficient = next.getCoefficient() * next.getPower();

             if(power < 0) {

                 if(privew == null) {
                     _head = next.getNext();

                     next = _head;
                 } else {
                     privew.setNext(next.getNext());

                     next = privew;
                 }

             } else {
                 next.setCoefficient(coefficient);
                 next.setPower(power);
             }

             privew = next;
             next = next.getNext();

         }
         return this;
    }

     public String toString() {

         String ret = "";
         PolyNode runner = _head;
         while (runner != null) {

             ret += runner.getCoefficient() > 0 ? "+" : "";
             ret += runner.toString();
             ret += " ";
             runner = runner.getNext();
         }
         return ret;
     }
}  

我认为第一类方法的O时间和O内存复杂度都是O(1)对吗? 那第二类方法呢?

0 个答案:

没有答案