Thursday, 15 May 2014

How to move Index element to end of Singly List while shifting all other elements java -


The index is trying to move the element at the end of the list, while changing all the other elements. {1, 2, 3, 4, 5} and I pass this 2 index, it will return {1, 2, 4, 5, 3}. Public indexes can be sold illegally; exceptions {if (index LT; 0; index> size ()) new invalid orientation exception ("+"); Node ref = first; Node other = first; For {int i = 0; i & lt; index; i ++} {ref = ref.next; For (int j = 0; j & lt; size () - 1; j ++) {if (otherRef.next! = Null) Other Ref = otherRef.next; }} E temp = ref.data; Ref.data = otherRef.data; OtherRef.data = temporary;

}

} The code I have written changes the index and the element on the last element, returning {1, 2, 5, 4, 3}

Thanks for the help and keep in mind, I am very new to coding, all help is highly appreciated.

You will need to update the references during looping, not at the end.

This is a small algorithm that saves you double loop. I swap it to the code, not the data , but there is no real difference in the result. I'm not sure what you have about an approach or other, but if you have to remove the nodes There are ways for you, you usually work with references and do not assume.

I did not compile it, so please excuse any error, but I think that something like this should be done:

  Public Zero Datatlast (Int. Index) illegally concluded {if (index LT; 0; index> size ()) new invalid record exception ("+"); Node refToMove = first; Node previousRef = null; For (int i = 0; i & lt; index & amp; refotoov! = Null; i ++) {previousRef = refToMove; RefToMove = refToMove.next; } If (refToMove! = Null & amp; previous Rough! = Null) {node nextRef = refToMove.next; While (nextRef! = Null) {previousRef.next = nextRef; Node tempRef = next riff.Nax; NextRef.next = refToMove; RefToMove.next = tempRef; NextRef = tempRef; }}   

}

No comments:

Post a Comment