数组作链表
一般传统链表的物理结构,是由指针把一个一个的节点相互连接而成:
1 2 3 4 5 6 | struct node { DataType data; node* previous; node* next; } |
其特点是按需分配节点,灵活动态增长。
但是此外,还有另外一种方式是使用数组实现链表,这里所有的node都在预先分配好的数组中,不使用指针,而是用数组下标来指向前一个、下一个元素:
1 2 3 4 5 6 | struct node { DataType data; int previous; int next; } |
其特点是预先分配节点,并且如果需要链表长度随需增加,需要reallocation ,和vector类似。
下面就我自己的一些了解,谈一下其优缺点与应用。
数组作链表有哪些优点
能要省些内存吗?不见得;速度要快吗?没看出来,那么为什么要使用这种不那么直观的方式来实现链表呢?
- 数组的内存布局比较紧凑,能占些局部性原理的光
- 在不提供指针的语言中实现链表结构,如vb等
- 进程间通信,使用index比使用指针是要靠谱 - 一个进程中的指针地址在另外一个进程中是没有意义的
- 对一些内存奇缺应用,当其值类型为整型,且值域与数组index相符时,可以将next指针与data复用,从而节省一些内存
- 整存零取,防止内存碎片的产生(多谢Emacs补充)
实现与应用
Id allocator
这里第一个例子针对上面第四点展开,其主要的应用在于ID的分配与回收,比如数据库表中的每条记录都需要一个unique id,当你增增减减若干次之后,然后新建一个表项,你该分配给它哪个id呢?
- 维持一个id,每增加一行就加1,删行不回收id --- 这样id会无限增加,太浪费了
- 每次分配都遍历一遍,找到最小的那个还没被用过的id --- 这样太浪费时间了
一个比较合理的做法是维护一个“可用ID”的链表,每次增加就从链表中拿一个,每次删除就把被删的ID链接到链表中,但是,对于传统链表结构而言,其节点的定义类似于:
1 2 3 4 5 | struct idnode { int availableID; idnode* next; }; |
这里,因为链表的值的类型与值域都与数组的index相符,我们可以复用其值和next,从而只需一个int数组,而不是一个idnode数组,数组中某个元素的值代表的即是链表节点的值,也是链表的下一个节点下标。下面是一个idallocator的实现,主要提供allocate和free两个函数用来满足上述要求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | const static char LIST_END = -1; template < typename integer> class idallocator { public : idallocator( int _num): num(_num) { array = new integer[num]; // Initialize the linked list. Initially, it is a full linked list with all elements linked one after another. head = 0; for (integer i = 0; i < num; i++) { array[i] = i + 1; } array[num-1] = LIST_END; count = num; } ~idallocator() { delete [] array; } integer allocate() // pop_front, remove a node from the front { int id = head; if (id != LIST_END) { head = array[head]; count--; } return id; } // push_front, add a new node to the front void free ( const integer& id) { array[id] = head; count++; head = id; } // push_front, add a new node to the front bool free_s( const integer& id) { // make sure this id is not in the list before we add it if (exist(id)) return false ; free (id); return true ; } size_t size() { return count; } private : bool exist( const integer& id) { int i = head; while (i != LIST_END) { if (i == id) return true ; i = array[i]; } return false ; } private : integer* array; int num; // totall number of the array int count; // number in the linked list int head; // index of the head of the linked list }; |
Double linked list
用数组实现链表,大多数情况下,是与传统链表类似的,无非是在添加、删除节点后,调整previous,next域的指向。但是有一点,当我在添加一个新的节点时,如何从数组中拿到一个还未被使用过的节点呢?这里有两种方法:
- 如果你看懂了上面的id allocator,你也许已经意识到,使用上面那个idallocator类就可以简单的实现这个需求
- 另外一种方法,其实原理上也类似,就是在这个double linked list类中维护两个链表,一个是已使用的,一个是未使用的
这里有个粗略的实现:arraylist.
参考
- 算法导论10.3
- 另类的链表数据结构以及算法
- 链表结构原理 与 数组模拟链表 的应用
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述