I have the following methods
moveNext() //moves the current element "key" to the next element in the list. movePrev() //moves to the previous element in the list. moveToIndex() //Moves the current element to the specified index. insertAfterCurrent(int a) //inserts element after the current element "key" insertBeforeCurrent(int a) //inserts element before the current element "key" inserFront() // inserts to the front of the list i.e. index 0. insertBack() deleteCurrent() getCurrent() deleteFront() deleteBack()
Link A must perform a permutation on Link B. That is,
Doubly A = new Doubly(); //Contains elements {1,1,3,2,1}
Doubly B = new Doubly(); //{1,2,3,4,5}
Now each index in A corresponds to the index in B. List B must be rearranged given the specified elements in A which act as indices to move the current element in B to the specified index in A. Assuming index starting at 1.
For example the first element in A(1) specifies that the first element in B must be in position 1. The next element in A(1) states that the next element in B (2) must be in index 1 as well. So 2 is inserted before 1 and so on. The end result of these operations would leave B in a rearranged state: {5,2,4,1,3}
The rearrangement is done in a function called Shuffle. Now I'm having difficulty trying to come up with an algorithm that would achieve this.
My current shuffle function code
if (A.getLength() > 0 && B.getLength() >0){
if (B.getLength() == A.getLength()){
for (A.moveTo(0); !A.offEnd(); A.moveNext()){
B.moveNext();
}
Now all this is is traverse the A and B lists but after this I am completely lost on how permute B to get the result {5,2,4,1,3}.

New Topic/Question
Reply



MultiQuote



|