简体   繁体   中英

How do I reverse a sequence of attached objects in java

My Wagon object has 2 variables, Nextwagon and Previouswagon. These wagons can be attached to each other using next and previous wagon variables. For example if I have 4 wagons, wagon 2 is attached to the first by declaring the nextwagon of the first as wagon 2 and the third is attached to the second and so forth.

As a result my sequence would be 1,2,3,4. My question is; I want to reverse this sequence with the help of a recursive method (which should result as 4,3,2,1.), how can I achieve this?

 /**
 * reverses the order in the sequence of wagons from this Wagon until its final successor.
 * The reversed sequence is attached again to the predecessor of this Wagon, if any.
 * no action if this Wagon has no succeeding next wagon attached.
 * @return the new start Wagon of the reversed sequence (with is the former last Wagon of the original sequence)
 */
public Wagon reverseSequence() {
    // TODO provide a recursive implementation


    return null;
}

The following code snipped is a test that should pass if my method is correct.

@Test
public void T05_WholeSequenceOfFourShouldBeReversed() {
    passengerWagon2.attachTo(passengerWagon1);
    passengerWagon3.attachTo(passengerWagon2);
    passengerWagon4.attachTo(passengerWagon3);

    // reverse full sequence
    Wagon rev = passengerWagon1.reverseSequence();

    assertEquals(4, rev.getSequenceLength());
    assertEquals(passengerWagon4, rev);
    assertEquals(passengerWagon3, rev.getNextWagon());
    assertFalse(rev.hasPreviousWagon());

    assertEquals(passengerWagon2, passengerWagon3.getNextWagon());
    assertEquals(passengerWagon4, passengerWagon3.getPreviousWagon());

    assertEquals(passengerWagon1, passengerWagon2.getNextWagon());
    assertEquals(passengerWagon3, passengerWagon2.getPreviousWagon());

    assertFalse(passengerWagon1.hasNextWagon());
    assertEquals(passengerWagon2, passengerWagon1.getPreviousWagon());
}

The following test should also pass.

@Test
public void T05_PartiallyReverseASequenceOfFour() {
    passengerWagon2.attachTo(passengerWagon1);
    passengerWagon3.attachTo(passengerWagon2);
    passengerWagon4.attachTo(passengerWagon3);

    // reverse part of the sequence
    Wagon rev = passengerWagon3.reverseSequence();
    assertEquals(2, rev.getSequenceLength());
    assertEquals(passengerWagon4, rev);

    assertEquals(passengerWagon3, rev.getNextWagon());
    assertEquals(passengerWagon2, rev.getPreviousWagon());

    assertFalse(passengerWagon3.hasNextWagon());
    assertEquals(passengerWagon4, passengerWagon3.getPreviousWagon());

    assertEquals(4, passengerWagon1.getSequenceLength());
    assertFalse(passengerWagon1.hasPreviousWagon());
    assertEquals(passengerWagon2, passengerWagon1.getNextWagon());

    assertEquals(passengerWagon1, passengerWagon2.getPreviousWagon());
    assertEquals(passengerWagon4, passengerWagon2.getNextWagon());
}

I might be late, but I have the answer for you. However, I am not allowed to answer this with code. Because this assignment is for a grade and will be used next year and yes I'm from the same school. :)

The solution: Every recursion you have to get the last wagon (getLastWagonAttached()) from and detach this wagon from its predecessors (detachFromPrevious()). If the last wagon is the same as the current wagon you have to return itself.

After detaching the current recursion's last wagon, attach it to the called recursion's last wagon (Note: Every recursion happens on the same wagon) And then return the current recursion's last wagon.

Now you've reversed the sequence. But is not connected to the not reversed part of the train. (Note: If the wagon had a previous sequence) So because you called the recursion on the same wagon you can attach it back to its original previous wagon.

public Wagon reverseSequence() {
    // first, get the next wagon in the sequence
    Wagon nextWagon = this.getNextWagon();
    // if it is the last wagon in the train, then it should be the first wagon
    // in the new sequence. Return it. This is the recursive base case.
    if (nextWagon == null) {
        return this;
    }
    // otherwise, do three things:
    // (1) reverse the part of the wagon train after this
    // (2) attach this wagon to _after_ what was previously the next wagon,
    //       since after being reversed it will be at the end of the train
    //       before this wagon
    // (3) return the front of that wagon train
    Wagon front = nextWagon.reverseSequence();
    this.attachTo(nextWagon);
    return front;
}

You can see two things happening here: first, we drive to the end of the wagon train and pick out the last wagon. Then, we drag it to the front, and each wagon cart attaches itself to the cart that was previously attached to it. And voila, the wagon train is reversed.

(note that I don't know what .getNextWagon() and .attachTo() actually do , since I don't have access to your source code - I'm making an educated guess, so hopefully if I'm wrong you can realize and correct it for your own solution)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM