Remove node not working in code

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 8
Reputation: yawjava is an unknown quantity at this point 
Solved Threads: 0
yawjava yawjava is offline Offline
Newbie Poster

Remove node not working in code

 
0
  #1
Nov 3rd, 2008
hi my remove node is not working....can someone help me correct it

// 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);
}
}
}

//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)
public void removeNode(int indexnumber)
{
Node n = head;
int i = 0;
while (i < indexnumber - 1){
n = n.next;
if(n.next == null)
//throw exception
++i;
}
Node A = n;
//(if we encounter a node with the next node pointing to null, thow an exception)
Node B = A.next; //B is the node we want to remove
Node C = B.next; //C is the node right after the node we want to remove
A.next = C;
B.next = 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;
}
}

}
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Remove node not working in code

 
0
  #2
Nov 3rd, 2008
Please post an SSCCE so that it is easier for us to help you. You're posting a lot of extraneous code if all you need help with is removing a node from a linked-list.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: yawjava is an unknown quantity at this point 
Solved Threads: 0
yawjava yawjava is offline Offline
Newbie Poster

Re: Remove node not working in code

 
0
  #3
Nov 3rd, 2008
I have a problem with this remove node code

//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)
public void removeNode(int indexnumber)
{
Node n = head;
int i = 0;
while (i < indexnumber - 1){
n = n.next;
if(n.next == null)
//throw exception
++i;
}
Node A = n;
//(if we encounter a node with the next node pointing to null, thow an exception)
Node B = A.next; //B is the node we want to remove
Node C = B.next; //C is the node right after the node we want to remove
A.next = C;
B.next = null;

}
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Remove node not working in code

 
0
  #4
Nov 3rd, 2008
How is it not working? What errors are you getting if any? Also, you're going to have unexpected behavior in that "if" statement in your loop.

These two code snippets are the same:
  1. // Yours:
  2. int i = 0;
  3. while (i < indexnumber - 1){
  4. n = n.next;
  5. if(n.next == null)
  6. //throw exception
  7. ++i;
  8. }
  1. // Same as:
  2. int i = 0;
  3. while (i < indexnumber - 1) {
  4. n = n.next;
  5. if (n.next == null)
  6. ++i;
  7. }
Notice how the comment was not considered to be the body of the "if" statement.

You can fix it by surrounding the body of the "if" statement with brackets or putting a semi-colon after the statement.
  1. int i = 0;
  2. while (i < indexnumber - 1) {
  3. n = n.next;
  4. if (n.next == null) {
  5. //throw exception
  6. }
  7. ++i;
  8. }

Also, I would recommend using a "for" loop. It's more compact and you would've gotten a compile-time error on your "if" statement—rather than it silently ignoring the error—had you used one.
  1. for (int i = 0; i < indexnumber - 1; i++) {
  2. n = n.next;
  3. if (n.next == null) {
  4. // throw exception
  5. }
  6. }

I still haven't looked at the logic in your method because you still haven't explained what's wrong with it and you haven't shown any interest in solving the problem beyond asking someone to do it for you.
Last edited by destin; Nov 3rd, 2008 at 4:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: yawjava is an unknown quantity at this point 
Solved Threads: 0
yawjava yawjava is offline Offline
Newbie Poster

Re: Remove node not working in code

 
0
  #5
Nov 3rd, 2008
o am sorry the problem is that its suppose to remove a node and when i put in the index it does not remove the node
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Remove node not working in code

 
0
  #6
Nov 3rd, 2008
Right. But what does happen? Is it removing the wrong index? Is an exception being thrown? Is nothing happening? Show some interest and some willingness to think.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: yawjava is an unknown quantity at this point 
Solved Threads: 0
yawjava yawjava is offline Offline
Newbie Poster

Re: Remove node not working in code

 
0
  #7
Nov 4th, 2008
Originally Posted by destin View Post
Right. But what does happen? Is it removing the wrong index? Is an exception being thrown? Is nothing happening? Show some interest and some willingness to think.
Sir i am new to java so please be patient with me. I get this error
Exception in thread "main" java.lang.NullPointerException
at Polynomial.removeNode(Polynomial.java:88)
at PolyTest.main(PolyTest.java:16)
with this code

//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)
public void removeNode(int indexnumber)
{
Node n = head;
for (int i =0; i < indexnumber -1; i++){
n = n.next;
if (n.next == null){
//throw
}
}
Node A = n;
//(if we encounter a node with the next node pointing to null, thow an exception)
Node B = A.next; //B is the node we want to remove
Node C = B.next; //C is the node right after the node we want to remove
A.next = C;
B.next = null;

}
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Remove node not working in code

 
0
  #8
Nov 4th, 2008
Originally Posted by yawjava View Post
Sir i am new to java so please be patient with me. I get this error
  1. Exception in thread "main" java.lang.NullPointerException
  2. at Polynomial.removeNode(Polynomial.java:88)
  3. at PolyTest.main(PolyTest.java:16)
Being new to Java doesn't justify the lack of effort you're putting in to this. Which line is 88? What does it mean if a NullPointerException is being thrown?

Show us that you're thinking and attempting on your own to solve the problem and you will receive a decent answer.
Last edited by destin; Nov 4th, 2008 at 12:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 8
Reputation: yawjava is an unknown quantity at this point 
Solved Threads: 0
yawjava yawjava is offline Offline
Newbie Poster

Re: Remove node not working in code

 
0
  #9
Nov 4th, 2008
Node C = B.next; //C is the node right after the node we want to remove
I have been trying to figure this out for about 2 days and am so tired...It feels like i got it but something is just not right
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 94
Reputation: destin is an unknown quantity at this point 
Solved Threads: 10
destin's Avatar
destin destin is offline Offline
Junior Poster in Training

Re: Remove node not working in code

 
0
  #10
Nov 4th, 2008
What does it mean if a NullPointerException is being thrown?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 640 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC