Hi am new at java and I was wondering what would be a code to remove it if i provide the coeff

I know is

-Given the index you want to remove
-set the node at the previous index to point to the node at the next index
-set the index you want to remove as pointing to nothing, this includes setting its previous and next nodes to null

Can someone code this for me to see

// Polynomial.java
public class Polynomial
{
    private int degree;
    private Node head;

    public static Polynomial addPolys(Polynomial poly1, Polynomial poly2)
    {
        Polynomial polyRes = new Polynomial();
        int power = (poly1.degree > poly2.degree) ? poly1.degree : poly2.degree;

        while (power >= 0)
        {
            Node n1 = poly1.head;
            while (n1 != null)
            {
                if (n1.power == power)
                    break;
                n1 = n1.next;
            }

            Node n2 = poly2.head;
            while (n2 != null)
            {
                if (n2.power == power)
                    break;
                n2 = n2.next;
            }

            if ((n1 != null) && (n2 != null))
                polyRes.addNode(n1.coeff + n2.coeff, n1.power);
            else if (n1 != null)
                polyRes.addNode(n1.coeff, n1.power);
            else if (n2 != null)
                polyRes.addNode(n2.coeff, n2.power);

            power--;
        }
        return polyRes;        
    }   

    public void addNode(int coeff, int power)
    {
        if (head == null) // start the list
        {
            head = new Node(coeff, power, null);
            degree = power;
        }
        else
        {
            Node n = head;
            Node last = null;

            while (n != null)
            {
                if (power > n.power) // insert in list
                {
                    if (last == null)
                        head = new Node(coeff, power, n);
                    else
                        last.next = new Node(coeff, power, n);
                    degree = power;
                    break;
                }
                last = n;
                n = n.next;
            }
            if (n == null) // append to list
            {
                last.next = new Node(coeff, power, null);
            }
        }
    }

    public String toString()
    {
        StringBuffer strBuf = new StringBuffer();
        Node n = head;
        boolean first = true;

        while (n != null)
        {
            int absCoef = n.coeff;

            if (!first)
            {
                if (n.coeff >= 0)
                    strBuf.append(" + ");
                else
                {
                    absCoef = -absCoef;
                    strBuf.append(" - ");
                }
            }
            strBuf.append(absCoef).append("x^^").append(n.power);
            first = false;
            n = n.next;
        }
        return strBuf.toString();

    }

    public class Node
    {
        public int coeff;
        public int power;
        public Node next;

        public Node(int coeff, int power, Node next)
        {
            this.coeff = coeff;
            this.power = power;
            this.next = next;
        }
    }

}

// PolyTest.java
public class PolyTest
{

    public static void main(String[] args)
    {
        Polynomial poly1 = new Polynomial();
        poly1.addNode(3, 2);
        poly1.addNode(4, 1);
        poly1.addNode(4, 0);

        Polynomial poly2 = new Polynomial();
        poly2.addNode(3, 3);
        poly2.addNode(-4, 2);
        poly2.addNode(3, 0);

        Polynomial poly3 = Polynomial.addPolys(poly1, poly2);

        System.out.println(poly3);    
    }

}

Have you attempted this yet? Also, please use code tags to post code.

Have you attempted this yet? Also, please use code tags to post code.

Like I know what to do but i dont know how I will code it

//loop though the nodes until we got to the desired index - 1 and keep the reference of the node as A (A is the node right before the node we want to remove)
node temp = head
while (i < desiredIndex - 1){
temp = temp.next
if(temp.next == null)
//throw exception
++i
}
A = temp
//(if we encounter a node with the next node pointing to null, thow an exception)
B = A.next //B is the node we want to remove
C = B.next //C is the node right after the node we want to remove
A.next = C
B.next = null

what do i replace A,B,C with...please keep note i am brand new at java

A = temp
//(if we encounter a node with the next node pointing to null, thow an exception)
B = A.next //B is the node we want to remove
C = B.next //C is the node right after the node we want to remove
A.next = C
B.next = null

If you are using the LinkedList class, use yourObjectName.remove(index); . It will do all the necessary operations for you, such as making the previous and next nodes point to each other. Since I see that you're not using that class, I would strongly recommend that you do so. Otherwise, you're wasting your time. However, if you insist on doing it your way,

code to remove your node:

public Node removeNode(int index){
//the reason its silly for you not to keep some sort of a list is because when you want to remove a Node (the way you're doing it), you have to traverse the list until you get to the 'previous node', store that, then traverse until you get to the 'next node', store that, then set the previous node to point to the next node. Then, you're done, since Java does the garbage collection for you (you don't need to free anything).
//If that last sentence doesn't make sense, forget about it for now. 

http://www.faqs.org/docs/javap/c11/s2.html

-Edit-

Actually, see the delete() method at the end of that FAQ.


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.