[英]How do I reverse a sequence of attached objects in java
我的 Wagon 对象有 2 个变量,Nextwagon 和 Previouswagon。 这些货车可以使用下一个和上一个货车变量相互连接。 例如,如果我有 4 个货车,货车 2 通过将第一个货车的下一个货车声明为货车 2 来连接到第一个,而第三个则连接到第二个,依此类推。
结果我的序列将是 1,2,3,4。 我的问题是; 我想借助递归方法(结果应为 4,3,2,1.)反转此序列,我该如何实现?
/**
* 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;
}
截取的以下代码是一个测试,如果我的方法正确,该测试应该通过。
@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());
}
下面的测试也应该通过。
@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());
}
我可能会迟到,但我有答案给你。 但是,我不允许用代码回答这个问题。 因为这个作业是一个年级的,明年会用到,是的,我来自同一所学校。 :)
解决方案:每次递归,您都必须从中获取最后一辆货车 (getLastWagonAttached()) 并将这辆货车与其前身 (detachFromPrevious()) 分离。 如果最后一辆货车与当前货车相同,您必须自行归还。
分离当前递归的最后一个货车后,将其附加到被调用的递归的最后一个货车上(注意:每次递归都发生在同一个货车上),然后返回当前递归的最后一个货车。
现在你已经颠倒了顺序。 但不连接到火车的未反转部分。 (注意:如果货车有一个先前的序列)因此,因为您在同一个货车上调用了递归,所以您可以将其附加回原来的前一个货车。
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;
}
您可以看到这里发生了两件事:首先,我们开车到货车列车的末端并挑选最后一辆货车。 然后,我们将它拖到前面,每个货车都将自己附加到之前附加到它的手推车上。 瞧,货车火车颠倒了。
(请注意,我不知道.getNextWagon()
和.attachTo()
实际上是做什么的,因为我无权访问您的源代码 - 我正在做一个有根据的猜测,所以希望如果我错了,你可以为您自己的解决方案实现并纠正它)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.