三次旋转应该将 3 个节点一起旋转,因此,从中间到前、从前到后、从后到中间,如果长度 %3 不为 0,则剩余的一两个节点不会旋转。
<块引用>123456 -> 231564。
<块引用>1234567 ->2315647。
当我运行 Valgrind 时会发生这种情况,并且没有显示内存泄漏。 当测试在列表中使用我的tripleRotate 函数时会发生错误。
template <typename T>
void List<T>::tripleRotate() {
// @todo Graded in MP3.1
if (length_ < 3) {
return;
}
ListNode* curr = head_;
int i = 0;
ListNode* one;
ListNode* two;
ListNode* three;
while (curr != NULL) {
one = curr;
two = curr->next;
three = curr->next->next;
one = two;
two = three;
three = curr;
if (i == 0) {
head_ = one;
i++;
}
for (int i = 0; i <= 3; i++) {
curr = curr->next;
}
if (curr == NULL && length_ % 3 == 0) {
tail_ = three;
}
}
}
这是测试中的一行
TEST_CASE("List::triplerotate simple", "[weight=10][part=1][valgrind]") {
List<int> list;
for (int i = 1; i <= 6; i++)
list.insertBack(i);
list.tripleRotate();
stringstream s;
list.print(s);
REQUIRE("< 2 3 1 5 6 4 >" == s.str());
}