0

This is a code for polynomial addition using linked lists.

public class LinkedPolynomial{
    private Node first=new Node(0,0);
    private Node last=first;
    private static class Node{
        int coef;
        int exp;
        Node next;
        Node(int coef,int exp){
            this.coef=coef;
            this.exp=exp;
        }
    }
    private LinkedPolynomial(){}
    public LinkedPolynomial(int coef,int exp){
        last.next=new Node(coef,exp);
        last=last.next;
    }
    //return c=a+b
    public LinkedPolynomial plus(LinkedPolynomial b){
        LinkedPolynomial a = this;
        LinkedPolynomial c = new LinkedPolynomial();
        Node x=a.first.next;
        Node y=b.first.next;
        while(x!=null || y!=null){
            Node t=null;
            if(x==null){
                t=new Node(y.coef,y.exp);
                y=y.next;
            }
            else if(y==null){
                t=new Node(x.coef,x.exp);
                x=x.next;
            }
            else if(x.exp>y.exp){
                t=new Node(x.coef,x.exp);
                x=x.next;
            }
            else if {
                t=new Node(y.coef,y.exp);
                y=y.next;
            }
            else{
                int coef=x.coef+y.coef;
                int exp=x.exp;
                x=x.next;
                y=y.next;
                if(coef==0)
                    continue;
                t=new Node(coef,exp);
            }
            c.last.next=t;
            c.last=c.last.next;

        }
        return c;

    }
    public static void main (String args[]){
        LinkedPolynomial zero=new LinkedPolynomial(0,0);
        LinkedPolynomial p1=new LinkedPolynomial(4,3);
    }
}

In the method plus() can someone please explain to me what is done with

LinkedPolynomial a = this;
LinkedPolynomial c = new LinkedPolynomial();
Node x=a.first.next;
Node y=b.first.next;  

What is a.first.next;

What is LinkedPolynomial. Is it a node? Does every LinkedPolynomial has a first and last?

Can someone please explain these to me.

T.C.
  • 133,968
  • 17
  • 288
  • 421
sam_rox
  • 739
  • 3
  • 14
  • 29
  • Is it intentional to have both LinkedPlynomial and LinkedPolynomial? I'm not clear on if you're intending two different classes or it's a bad retype job. – KathyA. Jun 24 '14 at 16:46
  • It's almost certainly a typo. – Ben Jun 24 '14 at 16:48
  • @sam_rox, just look at the LinkedPolynomial class definition, it's really straight forward. It's meant to contain a polynomial like "x^2 + 2x + 3", with each node containing a different term. – Ben Jun 24 '14 at 16:49
  • So the four lines you posted starts off by grabbing the first term of the two polynomials you want to add. – Ben Jun 24 '14 at 16:50

2 Answers2

1
LinkedPolynomial a = this;
LinkedPolynomial c = new LinkedPolynomial();

a and c refer to LinkedPolynomial objects, NOT LinkedLists. They encapsulate the list nodes.

Node x=a.first.next;
Node y=b.first.next;

a.first.next is a node!

The LinkedPolynomial class contains fields that point to Nodes. This is known as encapsulation, and is a good programming practice, so outside code cannot tamper with the nodes directly, corrupting your LinkedPolynomials.

Since a is a LinkedPolynomial, a.first is a Node (with coef = 0 and exp = 0, to be exact). Look at Line 2. a.first.next is also a Node. Line 7 says that each Node holds a pointer to its next Node.

Each LinkedPolynomial does, indeed, have a first and a last. Look at the LinkedPolynomial constructor. To make a LinkedPolynomial, it already has a first and last, and the constructor can change the last if it wants.

prajmus
  • 3,171
  • 3
  • 31
  • 41
Alex T
  • 11
  • 1
  • :If the polynomials are as `LinkedPolynomial p1 = new LinkedPolynomial(4, 3); LinkedPolynomial p2 = new LinkedPolynomial(3, 2); LinkedPolynomial p3 = new LinkedPolynomial(1, 0); LinkedPolynomial p4 = new LinkedPolynomial(2, 1); LinkedPolynomial p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1 LinkedPolynomial q1 = new LinkedPolynomial(3, 2); LinkedPolynomial q2 = new LinkedPolynomial(5, 0); LinkedPolynomial q = q1.plus(q2); ` after ` p1.plus(p2)`does t has coefficient 4 and exponent 3 – sam_rox Jun 24 '14 at 17:39
  • Then with `p1.plus(p2).plus(p3)` x points to t and y points to p3 right?Then the condition that would be executed is `if(x.exp>y.exp)`.Then again t has coefficient 4 and exponent 3 .What's wrong with the way I am going – sam_rox Jun 24 '14 at 17:42
0
  1. LinkedPolynomial a = this; Means it is referring to the current object. So for example if you have

    LinkedPolynomial zero=new LinkedPolynomial(0,0); LinkedPolynomial p1=new LinkedPolynomial(4,3);

zero.plus(p1). this would refer to zero object.

2.LinkedPolynomial c = new LinkedPolynomial(); This is being created to store the value of a+b.

3.According to the code every LinkedPolynomial contains two nodes first and last. So when you do a.first, it is accessing the first node and when you do next after that, it will access node next to first.

Vivin
  • 1,327
  • 2
  • 9
  • 28