c++ - 我卡在自定义链表上

标签 c++ arrays

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

相关文章:

iphone - 如何检测数组是否不为空?

C++ 类变量不打印分配的正确值

c++ - 正则表达式很奇怪

c++ - XCode4,保存文件

c++ - 在 C++ 中缩小 2D vector

javascript - 提醒数组内的数据细节

c++ - std::string::operator[] 的结果地址是否指向一个可写的、以 nul 结尾的缓冲区?

java - 迭代数组并打印不同的字符串输出

javascript - 如何使用angularjs将html文件转换为字节数组

javascript - ab 是一个数字,求 (a+b)^n = ab