我应该在不使用结构的情况下制作链表,而只是使用数组。一切都按照讲师的要求进行,除了 Tom 应该是列表的末尾,并且需要显示 if 语句。这是我唯一无法上类的地方。预先感谢您的帮助。
#include<iostream>
#include<string>
using namespace std;
int main()
{
int position[10] = {0,1,2,3,4,5,6,7,8,9};
string names[10] = {"dick", "Harry", "Sam", "Tom"};
int link[10] = {1, 2, 3, 99, 5, 6, 7, 8, 9, 10, };
int stkptr = 0;
for(int i = 0; i < 10; i++)
{
if(stkptr == 99)
cout<<"You have reached the end of the list."<<endl;
else
stkptr = link[stkptr];
cout << names[i] << " is in position "
<<position[stkptr] << " and is linked to " << names[stkptr] << endl;
}
return 0;
}
最佳答案
您的链接设置不正确。
link[3]
中的99表示链表中没有第4个节点,只有第3个。
您想将 99 移到最后一个有效链接之后。
提示:您的列表为:0 -> 1 -> 2 -> 99,仅显示有效节点 0、1、2。
提示 2:用笔和纸画出来。
如果您喜欢这个答案,请单击它旁边的复选标记。
关于c++ - 我卡在自定义链表上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15005879/