Is a Linked-List implementation without using pointers possible or not?(不使用指针的链表实现是否可行?)
问题描述
我的问题很简单,是否可以使用 C++,在不使用指针(下一个节点)的情况下实现链表数据结构?为了进一步限定我的问题,我的意思是可以仅使用类实例化来创建链表数据结构.
My question is very simple, can one using C++, implement a link-list data structure without using pointers (next nodes)? To further qualify my question, I'm mean can one create a Linked-List data structure using only class instantiations.
一个常见的节点定义可能是这样的:
A common node definition might be like so:
template<typename T>
struct node
{
T t;
node<T>* next;
node<T>* prev;
};
我知道 std::list
等,我只是想知道它是否可能 - 如果有的话如何?代码示例将不胜感激.
I'm aware of std::list
etc, I'm just curious to know if its possible or not - and if so how? Code examples will be greatly appreciated.
更多说明:
- 插入应该是 O(1).
- 遍历不应该超过 O(n).
- 实节点和空节点应该是可微的.
- 链表的大小应仅受可用内存量的限制.
推荐答案
当然,如果你不介意链表有一个最大大小,你可以静态分配一个链表节点数组,然后使用整数索引到数组作为每个节点的上一个"和下一个"值,而不是指针.我过去这样做是为了节省一点内存(因为整数可以是 2 或 4 个字节,而在 64 位系统上,指针将是 8 个字节)
Sure, if you don't mind the linked list having a maximum size, you could statically allocate an array of list nodes, and then use integer indices into the array as your "previous" and "next" values for each node, rather than pointers. I've done in this in the past to save a bit of memory (since an integer can be either 2 or 4 bytes, whereas on a 64-bit system a pointer will be 8 bytes)
这篇关于不使用指针的链表实现是否可行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不使用指针的链表实现是否可行?


基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05