java - My delete method to remove a specific Node with X age value will not work -
this code, please reference main method , delete method, other methods included show full program. have heard java takes method arguments pass value, issue, , object's properties pass-by-reference.
so change node.age
, since age property, cannot make node
equal node = node.next
? have been @ while, trying learn how different operations , have been stuck on this. if-statement went through, headnode
did not equate headnode.next
wanted to:
public class node { int age; node next; node previous; public static void main (string [] args) { node firstnode = new node(18); node t = firstnode; node randomfatnode = new node(); for(int = 0; < 30; += 3) { node tempnode = new node(i + 21); firstnode.next = tempnode; tempnode.previous = firstnode; firstnode = tempnode; } node tailnode = firstnode; firstnode = t; traverseforward(firstnode); //prints out: traversal forward -> 18 -> 21 -> 24 -> 27 -> 30 -> 33 -> 36 -> 39 -> 42 -> 45 -> 48 -> null deletenode(firstnode, 18); //does not delete first node, has age of 18. traverseforward(firstnode); //prints out: traversal forward -> 18 -> 21 -> 24 -> 27 -> 30 -> 33 -> 36 -> 39 -> 42 -> 45 -> 48 -> null } public node() { this.age = 20; this.next = null; this.previous = null; } public node(int inputage) { this.age = inputage; this.next = null; this.previous = null; } public static void traverseforward(node headnode) { system.out.print("traversal forward -> "); node usethisfortestnode = headnode; while(usethisfortestnode != null) { system.out.print(usethisfortestnode.age + " -> "); usethisfortestnode = usethisfortestnode.next; } system.out.print("null "); system.out.println(); } public static void deletenode(node headnode, int keytodelete) { node temp = headnode; if(temp != null && temp.age == keytodelete) { headnode = headnode.next; system.out.println("test"); //printed out test see if if statement went through. did, node in linked list remained unchanged. } } // other methods }
you need make firstnode class field.
Comments
Post a Comment