void display()
{
list *newlist;
newlist = first;
cout << endl;
do
{
if (newlist == NULL)
cout << "List is empty" << endl;
else
{
cout << "Name is: " << newlist->name << " ";
cout << "Age is: " << newlist->age << " ";
cout << "Height is: " << newlist->height;
if (newlist==current)
cout<<" <-- Current position ";
cout<< endl;
newlist = newlist->next;
}
}
while(newlist!=NULL);
cout << "End of List" << endl;
}我在这里的帮助下更正了我的整个代码,关于当前的初始化,我喜欢这个网站!只有一个问题,我只想在当前节点上显示光标。但是这段代码甚至没有显示所写的“<-current position”。当我去掉"if(newlist==current)“时,所有节点的”<--当前位置“都出现了。所以我有点困惑,我应该把这个放在哪里…我还必须在其他节点之间插入一个节点。有没有人可以给我一个代码,告诉我如何在我移动当前位置或当前指向的位置显示“当前位置”?
发布于 2011-02-26 23:51:23
在你之前的帖子中,我更正了你的源代码。请阅读我提供的代码中的注释。这将回答您的问题。参见here。
您可能已将当前值初始化为NULL,但在添加元素时,还需要将当前值设置为正确的值。例如,当您在链表中插入第一个值时,current = first。现在的问题是,您正在将newlist与NULL进行比较,它的计算结果永远不会为true。所以它永远不会打印你的当前位置。
发布于 2011-02-26 23:55:52
如果current确实指向列表中的一个节点,那么该代码应该可以很好地工作。
你应该可以通过输入下面这样的命令来找到答案:
cout << newlist << " " << current << endl;之前:
newlist = newlist->next;https://stackoverflow.com/questions/5127865
复制相似问题