sam_rox
sam_rox

Reputation: 747

Polynomial addition using Linked List

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.

Upvotes: 0

Views: 4288

Answers (2)

Alex T
Alex T

Reputation: 11

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.

Upvotes: 1

Vivin
Vivin

Reputation: 1367

  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.

Upvotes: 0

Related Questions