我应该在不使用结构的情况下创建一个链表,而只是使用数组。一切都按照讲师的要求进行,除了 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;
}