【问题标题】:Adding two polynomials stored as linked lists添加两个存储为链表的多项式
【发布时间】:2015-03-11 20:44:42
【问题描述】:

我有以下代码..

    import java.io.*;
    class Link

    {

      public int coeff;

      public int exp;

      Link next;

      public Link(int a,int b)
    {
       coeff=a;exp=b;
    }

       public int retcof(){
        return coeff;
    }

       public int retexp() {
        return exp;
    }

       public void displayLink(){
       System.out.print(coeff+"x^"+exp);
    }


    }


     class LinkList{


     Link first,last;


    public LinkList(){
               ;
    }

      public void insertfirst(int x,int y)
    {
      Link newLink=new Link(x,y);

      newLink.next=first;

      first=newLink;
    }

      public void displayList()
    {

      Link x=first;


      while(x!=null)
    {


      x.displayLink();

         x=x.next;

          if(x!=null)

          System.out.print("+");

    }



    }


      /*public void add(LinkList a,LinkList b)

      {
      int p;

      Link current1=a.first;

      Link current2=b.first;

      LinkList qwe=new LinkList();

      while(current2!=null)

      { 

      while(current1!=null)

      { 
       if(current1.retexp()>current2.retexp()) 

        qwe.insertfirst(current1.retcof(),current1.retexp());
       else if(current2.retexp()>current1.retexp())

        qwe.insertfirst(current2.retcof(),current2.retexp());
       else if(current1.retexp()==current2.retexp())

       { 

       p=current1.retcof()+current2.retcof();


     qwe.insertfirst(p,current2.retexp());

       }

         current1=current1.next;



      }

        current2=current2.next;


      }

      qwe.displayList();

      }*/


 public void add(LinkList a,LinkList b)
{
Link current1=a.first;

  Link current2=b.first;

  LinkList qwe=new LinkList();
while (current1 != null || current2 != null) {    
    //now check if one of them has ended    
   if (current1 == null&&current2!=null) //first ended; insert remaining nodes from second; return result    
     {qwe.insertfirst(current2.retcof(),current2.retexp());current2 = current2.next;}
  if (current2 == null&&current1!=null) //second ended, insert remaining nodes from first; return result  
    {qwe.insertfirst(current1.retcof(),current1.retexp());  current1 = current1.next;}
   //otherwise, compare exponents    
   if ((current1 != null && current2 != null)&&(current1.retexp() > current2.retexp())) 
      {qwe.insertfirst(current1.retcof(),current1.retexp()); current1 = current1.next;}    
       //advance the first pointer, but not he second        
   else if ((current1 != null && current2 != null)&&(current1.retexp() < current2.retexp()))   
       {qwe.insertfirst(current2.retcof(),current2.retexp()); current2 = current2.next;}
      //in this case advancing the second pointer, but not the first    
   else if((current1 != null && current2 != null)&&(current1.retexp() == current2.retexp()))//exponents are equal    
       {qwe.insertfirst(current2.retcof()+current1.retcof(),current2.retexp());; current1 = current1.next; current2 = current2.next;}    
      //add the members and advance both pointers    
}
qwe.displayList();
}





    }

      class zz
    {


      public static void main(String [] args)throws IOException
    {

       int degree1,degree2,num1,itr;


        LinkList wow=new LinkList();


        LinkList wow1=new LinkList();

    //wow.insertfirst(1,2);


      System.out.println("Enter the degree of the first polynomial "+" ");


      DataInputStream X=new DataInputStream(System.in);


      String s=X.readLine();

      degree1=Integer.parseInt(s);


      itr=degree1;


      while(itr>=0){ 

      System.out.print("enter the coeff of x^"+itr+" : ");

       s=X.readLine();
       num1=Integer.parseInt(s);

       wow.insertfirst(num1,itr);

       itr--;

      } 


      wow.displayList();


      System.out.println("\n"+"Enter the degree of the second polynomial "+" ");


      s=X.readLine();

      degree2=Integer.parseInt(s);

      itr=degree2;


      while(itr>=0)
    {

      System.out.print("enter the coeff of x^"+itr+" : ");

      s=X.readLine();
      num1=Integer.parseInt(s);

      wow1.insertfirst(num1,itr);
     itr--;

    }


      wow1.displayList();


      System.out.println("\n");


      wow.add(wow,wow1);

    }

    }

编辑:已修复。 add() 函数存在问题,现已纠正!

还有其他有效的方法吗?如何让这段代码更简单,尤其是看起来有点复杂的 add() 函数。

【问题讨论】:

  • 第 140 行是哪一行?
  • @Ryan J 我不知道。这就是编译器一直在说的。上面的代码就是我的全部
  • 你用的是什么IDE?
  • 大多数现代 IDE 和文本编辑器都支持行号。除非是记事本。请说你没有使用记事本...打开行号。
  • @pbabcdefp JRE/JDK 只是编译器。我把它写进记事本,然后在终端中编译。

标签: java linked-list polynomial-math


【解决方案1】:

我在这里看到的最重要的事情是你没有正确处理current 变量中的任何一个null 的情况,以防止 NPE(如你所见)的方式......

您的代码(以下格式更好)在处理null 方面存在一些问题

while (current1 != null || current2 != null) {    
   //now check if one of them has ended    
   if (current1 == null) //first ended; insert remaining nodes from second; return result    
   {
        qwe.insertfirst(current2.retcof(),current2.retexp());
        current2 = current2.next;
   }
   if (current2 == null) //second ended, insert remaining nodes from first; return result  
   {
        qwe.insertfirst(current1.retcof(),current1.retexp());  
        current1 = current1.next;
   }
   //otherwise, compare exponents    
   if (current1.retexp() > current2.retexp()) 
   {
        qwe.insertfirst(current1.retcof(),current1.retexp()); 
        current1 = current1.next;
   }    
   //advance the first pointer, but not he second        
   else if (current1.retexp() < current2.retexp())   
   {
        qwe.insertfirst(current2.retcof(),current2.retexp()); 
        current2 = current2.next;
   }
   //in this case advancing the second pointer, but not the first    
   else //exponents are equal                     
   {        
        qwe.insertfirst(current2.retcof()+current1.retcof(),current2.retexp());
        current1 = current1.next; 
        current2 = current2.next;
   }    
   //add the members and advance both pointers    
}

考虑current2null 的情况

您的代码将正确确定它为空并输入您的第二个if 块,然后前进current1

但是,在随后的 if 块中,您无法防止对 current2 上的字段的访问,因此您最终将获得一个 NPE,在:

 //otherwise, compare exponents    
if (current1.retexp() > current2.retexp())  // right here! you access current2, but it's null :(

如果你的任何一个链接是null,你需要绕过所有这些逻辑,这样你就不会陷入这种混乱。

【讨论】:

  • 好的,所以首先我通过 else-if 阶梯连接了每个 if/else。其次,对于最后两个 else-if(在第一次更正后最后一个 else 将保留 else),我在它们两个中都包含了 (current1 != null &amp;&amp; current2 != null)&amp;&amp;(current1.retexp() &gt; current2.retexp())。就是这样。它解决了这个问题。谢谢你好先生。
  • 同样的问题仍然存在。
  • 我已恢复我的 if-else 更正。我的新更正是:第三个 if 块 if ((current1 != null &amp;&amp; current2 != null)&amp;&amp;(current1.retexp() &gt; current2.retexp())) 在 else-if else if ((current1 != null &amp;&amp; current2 != null)&amp;&amp;(current1.retexp() &lt; current2.retexp())) 之后,最后一个也更改为 else-if else if((current1 != null &amp;&amp; current2 != null)&amp;&amp;(current1.retexp() == current2.retexp()))//exponents are equal 。它仍然给我同样的错误。
  • 进一步编辑:首先是 if (current1 == null&amp;&amp;current2!=null),其次是 if (current2 == null&amp;&amp;current1!=null)。它现在可以工作了!
【解决方案2】:

以下是完整的解析代码。

    import java.io.*;
class Link

{

  public int coeff;

  public int exp;

  Link next;

  public Link(int a,int b)
{
   coeff=a;exp=b;
}

   public int retcof(){
    return coeff;
}

   public int retexp() {
    return exp;
}

   public void displayLink(){
   System.out.print(coeff+"x^"+exp);
}


}


 class LinkList{


 Link first,last;


public LinkList(){
           ;
}

  public void insertfirst(int x,int y)
{
  Link newLink=new Link(x,y);

  newLink.next=first;

  first=newLink;
}

  public void displayList()
{

  Link x=first;


  while(x!=null)
{


  x.displayLink();

     x=x.next;

      if(x!=null)

      System.out.print("+");

}



}



public void add(LinkList a,LinkList b)
{

Link current1=a.first;


   Link current2=b.first;


  LinkList qwe=new LinkList();

  while (current1 != null || current2 != null) {    

   if (current1 == null&&current2!=null)

     {qwe.insertfirst(current2.retcof(),current2.retexp());current2 = current2.next;}
  if (current2 == null&&current1!=null)

    {qwe.insertfirst(current1.retcof(),current1.retexp());  current1 = current1.next;}

   if ((current1 != null && current2 != null)&&(current1.retexp() > current2.retexp())) 

      {qwe.insertfirst(current1.retcof(),current1.retexp()); current1 = current1.next;}    

   else if ((current1 != null && current2 != null)&&(current1.retexp() < current2.retexp()))   

       {qwe.insertfirst(current2.retcof(),current2.retexp()); current2 = current2.next;}

   else if((current1 != null && current2 != null)&&(current1.retexp() == current2.retexp()))//exponents are equal    

       {qwe.insertfirst(current2.retcof()+current1.retcof(),current2.retexp());; current1 = current1.next; current2 = current2.next;}    
}
qwe.displayList();
}





}

  class k
{


  public static void main(String [] args)throws IOException
{

   int degree1,degree2,num1,itr;


    LinkList wow=new LinkList();


    LinkList wow1=new LinkList();


  System.out.println("Enter the degree of the first polynomial "+" ");


  DataInputStream X=new DataInputStream(System.in);


  String s=X.readLine();

  degree1=Integer.parseInt(s);


  itr=degree1;


  while(itr>=0){ 

  System.out.print("enter the coeff of x^"+itr+" : ");

   s=X.readLine();
   num1=Integer.parseInt(s);

   wow.insertfirst(num1,itr);

   itr--;

  } 


  wow.displayList();


  System.out.println("\n"+"Enter the degree of the second polynomial "+" ");


  s=X.readLine();

  degree2=Integer.parseInt(s);

  itr=degree2;


  while(itr>=0)
{

  System.out.print("enter the coeff of x^"+itr+" : ");

  s=X.readLine();
  num1=Integer.parseInt(s);

  wow1.insertfirst(num1,itr);
 itr--;

}


  wow1.displayList();


  System.out.println("\n");


  wow.add(wow,wow1);

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多