在这个程序中,"列表的右侧"是唯一可直接访问的部分。但是,在显示列表时,左侧也需要打印在屏幕上。获取左侧数据的唯一方法是通过移动围栏将其转移到右侧。这就是我到目前为止所拥有的:
void display(textList& s)
//! ensures: s = #s and displays the contents of s
{
textList sTemp;
int leftL = s.leftLength();
s.moveToStart();
cout << "<";
for (int i = 0, lastItem = (leftL - 1); i < leftL; i++) {
cout << s.rightFront();
if (i < lastItem)
cout << ",";
s.advance();
} //end for
cout << ">";
cout << "<";
for(int i = 0, z = s.rightLength(), lastItem = (z - 1); i < z; i++) {
Text x;
s.removeRightFront(x);
cout << x;
sTemp.addRightFront(x);
if (i < lastItem) {
cout << ",";
} // end if
} // end for
cout << ">" << endl;
s.transferFrom(sTemp);
} // display
所以基本上我移动栅栏,这样我就可以访问所有数据,然后打印第一个值,然后将栅栏前移一个值,直到达到左侧的长度,然后打印出右侧。
我的问题是,右侧的第一个值总是会丢失。问题是什么?我花了很长时间来解决问题,但我似乎无法找到它;我相当肯定它在代码中用于打印右侧。
答案 0 :(得分:0)
改变了我的做法。而不是操纵“s”,我只是创建了一个副本,我可以乱搞,没有任何反响。然后我在副本上推进围栏,并用与左边相同的方法打印出右侧。
总结。 制作副本。 存储左长度 - z。 把一切都搬到了右边。 for循环(z次)。 打印数据。 高级围栏。 存储的权利长度 - z。 for循环(z次)。 印刷数据。 高级围栏。
void display(textList& s)
//! ensures: s = #s and displays the contents of s
{
textList newS;
newS = s;
int z = newS.leftLength();
newS.moveToStart();
cout << "<";
for (int i = 0, lastItem = (z - 1); i < z; i++) {
cout << newS.rightFront();
if (i < lastItem)
cout << ",";
newS.advance();
} //end for
cout << "> ";
cout << "<";
for (int i = 0, z = newS.rightLength(), lastItem = (z - 1); i < z; i++) {
cout << newS.rightFront();
newS.advance();
if (i < lastItem) {
cout << ",";
} // end if
} // end for
cout << ">" << endl;
} // display