创建没有指针但有索引的链表 [英] Creating a linked list without pointers but indices

查看:95
本文介绍了创建没有指针但有索引的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的老师希望我创建类似链表的内容,但是要使用索引而不是指针。因此 Node 包含 int数据 int索引。 / p>

您能给我一个提示,我怎么做?我知道如何使用指针创建它,但是如果没有指针怎么办?

解决方案

您的结构 Node 会把它作为一个容器。像这样

 结构节点{
int数据;
int指数; //池中条目到下一个节点的索引
}

您可以使用 vector 或创建池的数组

  vector< Node *> ;池//存储指向下一个节点的指针

现在可以访问下一个节点

  Node * nextNode = pool [currNode-> index]; 


My teacher wants me to create something like linked list but using indices instead of pointers. So the Node contains int data and int index.

Can you drop me a hint how would I do that? I know how to create it with pointers but how to do without them? He mentioned pool as a container though.

解决方案

Your struct Node would be like this

struct Node {
    int data;
    int index; // index of the entry in the pool to the next node
}

You can use a vector or array to create a pool

vector<Node*> pool; // stores the pointer to next nodes

Now to access the next node you can do

Node* nextNode = pool[currNode->index];

这篇关于创建没有指针但有索引的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆