reset password
Author Message
RandomAccess
Posts: 101
Posted 14:10 Mar 16, 2018 |

I created a while loop in the regroup method to search for while main nodes need to be swapped, with the following conditions:

while(right != null){
                System.out.println();
                System.out.println(toString());
                System.out.println();
                
                if(left == null){
                    first = left;
                    left = right;
                    right = last;
                    if(last != null){
                        last = last.getRight();
                    }
                }
                
                else if(left.getCategory1().compareTo(right.getCategory1()) > 0){
                    //swapping code goes here
                }

It works fine for the most part, but for some reason there's a specific condition for when the first node is null, and the head's value is higher than the next node's, where it deletes the next node, which is weird because I'm using the same conditions for both, just without the first node which is null, as shown below:

else if(first == null && last != null){//<-this condition deletes a node
                        Node<E1, E2, E3> tempLeft = left;
                        Node<E1, E2, E3> tempRight = right;
                        
                        last.setLeft(tempLeft);
                        System.out.println("1: deleted here?\n" + toString() + "\n\n");//<- toString prints I used to pinpoint where the node was deleted
                        last.getLeft().setRight(last); //<-location I pinpointed for where the node is erased
                        System.out.println("2: deleted here?\n" + toString() + "\n\n");
                        last.getLeft().setLeft(tempRight);
                        last.getLeft().getLeft().setRight(tempLeft);
                        last.getLeft().getLeft().setLeft(null);
                        
                        first = null;
                        left = null;
                        right = head;
                        last = right.getRight();
                    }

else{//<-this condition doesn't delete a node and does what it's supposed to, it's for when neither the first node or last node I'm checking are null
                        Node<E1, E2, E3> tempFirst = first;
                        Node<E1, E2, E3> tempLeft = left;
                        Node<E1, E2, E3> tempRight = right;
                        
                        last.setLeft(tempLeft);
                        last.getLeft().setRight(last);//<-this does not delete the node despite being written the exact same way
                        last.getLeft().setLeft(tempRight);
                        last.getLeft().getLeft().setRight(tempLeft);
                        last.getLeft().getLeft().setLeft(tempFirst);
                        last.getLeft().getLeft().getLeft().setRight(tempRight);
                        
                        
                        first = null;
                        left = null;
                        right = head;
                        last = right.getRight();
                    }

Am I missing something? I figured two statements written the exact same way with the exact same variables would do the exact same thing. I don't know why it does this. and it troubles me.

Last edited by RandomAccess at 14:12 Mar 16, 2018.
kknaur
Posts: 540
Posted 14:32 Mar 16, 2018 |

Maybe a different method for regrouping is in order.  Instead of swapping nodes, why not iterate through your list and place the nodes one by one into a new configuration. 

You may need to hand trace your code to see what is going on, and failing that, you may need to rewrite the method piece by piece and check to see that each part is working before moving on to the next part.