首页 > 代码库 > 基于数组实现的单链表(兼具Boost单元测试)
基于数组实现的单链表(兼具Boost单元测试)
对于单链表,我们大多时候会用指针来实现(可参考基于指针实现的单链表)。现在我们就来看看怎么用数组来实现单链表。
1. 定义单链表中结点的数据结构
1 typedef int ElementType; 2 class NodeType 3 { 4 public: 5 ElementType data; 6 int next; 7 };
该结点包括了两个元素,其一是数据,另一个是指向下一个结点的“指针”(在这篇文章中实际上是指用于实现单链表的数组的下标。)
2. 定义一个的数组
1 const int CAPACITY = 1024; 2 NodeType node[CAPACITY];
在内存中该数组的结构如下图:
3. 定义指向单链表第1个结点的“指针”
1 int head;
4. 一个简单的单链表例子
我们假设单链表只有3个结点,并且3个结点从左到右分别位于数组node中第0、1、2位置,如下图:
head指向链表中的第1个结点。该结点存储于数组node的第0位置,data为111,next指向下一结点。其他以此类推。
需要注意的是链表的中最后一个结点(即上图中的第3个结点)的next被置为-1,表示不指向任何结点。
更为重要的是,这三个结点是可以位于数组node中的任一位置的,只要他们之间保持这种链接的关系。
5. 实际程序实现
在实际程序实现中,为了便于判断数组中哪一个元素已经被使用,我在原单链表的结点数据结构中又添加了一个判断位,如下:
1 typedef int ElementType; 2 class NodeType 3 { 4 public: 5 NodeType() { data = http://www.mamicode.com/0; next = NULL_VALUE; isFree = true; }; 6 ~NodeType() {}; 7 ElementType data; 8 int next; 9 bool isFree; // 标志位,为true表示该结点可用,反之不可用 10 };
其余的程序摘录如下:
Boost单元测试代码测试如下:
个人“数据结构与算法”系列博文:
线性表:
顺序表:
基于静态分配的数组的顺序表(兼具Boost单元测试)
基于动态分配的数组的顺序表(兼具Boost单元测试)
链表:
http://www.wenjuan.com/s/MrmyYv
http://www.wenjuan.com/s/qUFzyi
http://www.wenjuan.com/s/MrmyYv
http://www.wenjuan.com/s/AVfUBfe
http://www.wenjuan.com/s/ZVfm2iF
http://www.wenjuan.com/s/QBV7Nf
http://www.wenjuan.com/s/2U3Q3u
http://www.wenjuan.com/s/3meE3y
http://www.wenjuan.com/s/M3Qr6fb
http://www.wenjuan.com/s/AZnuya
http://www.wenjuan.com/s/A77ZRz
http://www.wenjuan.com/s/bEVfIb
http://www.wenjuan.com/s/AbAzAjS
http://www.wenjuan.com/s/v6zY3m
http://www.wenjuan.com/s/ENBBZb
http://www.wenjuan.com/s/3mMbQf
http://www.wenjuan.com/s/MvMfMn
http://www.wenjuan.com/s/iAJZJj
http://www.wenjuan.com/s/n67zYj
http://www.wenjuan.com/s/3Qj6Fz
http://www.wenjuan.com/s/viimYv
http://www.wenjuan.com/s/Qrqm6b
http://www.wenjuan.com/s/JFNR32
http://www.wenjuan.com/s/Njemmi
http://www.wenjuan.com/s/aMNnIb
http://www.wenjuan.com/s/jMRrYz
http://www.wenjuan.com/s/VJJFrq
http://www.wenjuan.com/s/QVBjIne
http://www.wenjuan.com/s/yamU3y
http://www.wenjuan.com/s/ey2A7j
http://www.wenjuan.com/s/INFJBf
http://www.wenjuan.com/s/RBZJru
http://www.wenjuan.com/s/7z2YZf
http://www.wenjuan.com/s/UfyMba
http://www.wenjuan.com/s/B3IN3i
http://www.wenjuan.com/s/AbA7Zb
http://www.wenjuan.com/s/QbaiQf
http://www.wenjuan.com/s/z2mmIv
http://www.wenjuan.com/s/EnUbYn
http://www.wenjuan.com/s/eQfYNr
http://www.wenjuan.com/s/A7nAZn
http://www.wenjuan.com/s/yAviqe
http://www.wenjuan.com/s/e2YrIr
http://www.wenjuan.com/s/A7nAZn
http://www.wenjuan.com/s/yAviqe
http://www.wenjuan.com/s/YRb6R3
http://www.wenjuan.com/s/YrquQj
http://www.wenjuan.com/s/UJbIRvP
http://www.wenjuan.com/s/r2mMri
http://www.wenjuan.com/s/aaqUru
http://www.wenjuan.com/s/YnmYN3
http://www.wenjuan.com/s/fqUree
http://www.wenjuan.com/s/v6neyi
http://www.wenjuan.com/s/Vjyiii
http://www.wenjuan.com/s/f6JzEv
http://www.wenjuan.com/s/Rb6Fby
http://www.wenjuan.com/s/JZNvIj
http://www.wenjuan.com/s/vumErq
http://www.wenjuan.com/s/NjQNfmH
http://www.wenjuan.com/s/raYNJr
http://www.wenjuan.com/s/EBVRfq
http://www.wenjuan.com/s/7Z3m6b
http://www.wenjuan.com/s/JB7bEj
http://www.wenjuan.com/s/q6ZRJ3
http://www.wenjuan.com/s/yQRjaq
http://www.wenjuan.com/s/me6fqe
http://www.wenjuan.com/s/Y7nIzu
http://www.wenjuan.com/s/YZfaQn
http://www.wenjuan.com/s/JRfQNz
http://www.wenjuan.com/s/RVjU7r
http://www.wenjuan.com/s/InqU3q
http://www.wenjuan.com/s/673qui
http://www.wenjuan.com/s/FJvQzi
http://www.wenjuan.com/s/7nIVVn
http://www.wenjuan.com/s/VJJJVr
http://www.wenjuan.com/s/V32eQr
http://www.wenjuan.com/s/eYNzm2
http://www.wenjuan.com/s/FfYrQv
http://www.wenjuan.com/s/VnIJNj
http://www.wenjuan.com/s/qUfYNv
http://www.wenjuan.com/s/vA3MVv
http://www.wenjuan.com/s/6jMNrm
http://www.wenjuan.com/s/uqiuae
http://www.wenjuan.com/s/jAfiqy
http://www.wenjuan.com/s/MfeiEn
http://www.wenjuan.com/s/BRb2Mr
http://www.wenjuan.com/s/m6nAju
http://www.wenjuan.com/s/VZvUBv
http://www.wenjuan.com/s/6JniEv
http://www.wenjuan.com/s/VBnmym
http://www.wenjuan.com/s/vy2ae2
http://www.wenjuan.com/s/e2mMrq
http://www.wenjuan.com/s/6B732y
http://www.wenjuan.com/s/UFvEbu
http://www.wenjuan.com/s/rq6vya
http://www.wenjuan.com/s/mqaeY3
http://www.wenjuan.com/s/f2eUfq
http://www.wenjuan.com/s/FBBbme
http://www.wenjuan.com/s/uQVreq3
http://www.wenjuan.com/s/ieIZZb
http://www.wenjuan.com/s/jqIbai
http://www.wenjuan.com/s/NBNvAv
http://www.wenjuan.com/s/qMr6jm
http://www.wenjuan.com/s/n2YrQb
http://www.wenjuan.com/s/yAfYRn
http://www.wenjuan.com/s/nQFVn2
http://www.wenjuan.com/s/UzIV7f
http://www.wenjuan.com/s/JVZ3uq
http://www.wenjuan.com/s/UvaAZv
http://www.wenjuan.com/s/f2eUfq
http://www.wenjuan.com/s/IrQBniM
http://www.wenjuan.com/s/mqaeY3
http://www.wenjuan.com/s/rq6vya
http://www.wenjuan.com/s/UFvEbu
http://www.wenjuan.com/s/r6VjM3
http://www.wenjuan.com/s/UBJ7Fz
http://www.wenjuan.com/s/zi2i22
http://www.wenjuan.com/s/mIRfAj
http://www.wenjuan.com/s/67RbU3
http://www.wenjuan.com/s/Ina2me
http://www.wenjuan.com/s/z6byMj
http://www.wenjuan.com/s/I3IZF3
http://www.wenjuan.com/s/BjqeiyO
http://www.wenjuan.com/s/RNfUNnE
http://www.wenjuan.com/s/RNfUNnE
http://www.wenjuan.com/s/7Rj6ne
http://www.wenjuan.com/s/FnA7fu
http://www.wenjuan.com/s/NJjEVn
http://www.wenjuan.com/s/6nAJzq
http://www.wenjuan.com/s/jIjqY3
http://www.wenjuan.com/s/jieaye
http://www.wenjuan.com/s/RvmIju
http://www.wenjuan.com/s/JJzaUz
http://www.wenjuan.com/s/fQZ3ya
http://www.wenjuan.com/s/NnqMjm
http://www.wenjuan.com/s/auARzy
http://www.wenjuan.com/s/vaEFJ3
http://www.wenjuan.com/s/vaEFJ3
http://www.wenjuan.com/s/AnuMVfR
http://www.wenjuan.com/s/7vaya2S
http://www.wenjuan.com/s/q6Z3Qb
http://www.wenjuan.com/s/AnMnMz
http://www.wenjuan.com/s/A3IrQr
hthttp://www.wenjuan.com/s/AV73qi
http://www.wenjuan.com/s/QRjmUn
http://www.wenjuan.com/s/vUR32e
http://www.wenjuan.com/s/Y7Rbii
http://www.wenjuan.com/s/2umyqm
http://www.wenjuan.com/s/rIBZvq
http://www.wenjuan.com/s/eemA3i
http://www.wenjuan.com/s/EVBV3i
http://www.wenjuan.com/s/3EbI7zL
http://www.wenjuan.com/s/mYriAf
http://www.wenjuan.com/s/eYNviqb
http://www.wenjuan.com/s/fQ3Qzu
http://www.wenjuan.com/s/eUBN3e
http://c.tieba.baidu.com/p/3373993333
http://c.tieba.baidu.com/p/3372926228
http://www.wenjuan.com/s/2YvYf2
http://www.wenjuan.com/s/r6zMzi
http://www.wenjuan.com/s/7fu2AfU
http://www.wenjuan.com/s/rUneiy
http://www.wenjuan.com/s/7Fre6z
http://www.wenjuan.com/s/fyaIva
http://www.wenjuan.com/s/RZFnEz
http://www.wenjuan.com/s/ia6rYz
http://www.wenjuan.com/s/jimURr
http://www.wenjuan.com/s/YbARfe
http://www.wenjuan.com/s/Iz67jq
http://www.wenjuan.com/s/F7n632
http://www.wenjuan.com/s/NbIZVj
http://www.wenjuan.com/s/6BjEbu
http://www.wenjuan.com/s/vuqUjy
http://www.wenjuan.com/s/YbaiIr
http://www.wenjuan.com/s/3aquEb
http://www.wenjuan.com/s/Nf2UNb
http://www.wenjuan.com/s/YNFNJz
http://www.wenjuan.com/s/n6j6R3
http://www.wenjuan.com/s/aUz2Ib
http://www.wenjuan.com/s/quqm6b
http://www.wenjuan.com/s/vMnqay
http://www.wenjuan.com/s/YVFVvm
http://www.wenjuan.com/s/buEzqq
基于数组实现的单链表(兼具Boost单元测试)