For single-linked lists, we use pointers most of the time (refer to a single linked list based on pointer implementations). Now let's see how to use arrays to implement single-linked lists.
1. Define data structures for nodes in a single-linked list
1typedefintElementType;2 classNodeType3 {4 Public:5 ElementType data;6 intNext;7};
The node consists of two elements, one of which is the data, and the other refers to the "pointer" to the next node (in this article it is actually the subscript used to implement an array of single-linked lists.) )
2. Define an array of
1 Const int 1024x768 ; 2 NodeType node[capacity];
The structure of the array in memory is as follows:
3. Define a "pointer" to the 1th node of the single-link list
1 int head;
4. A simple single-link list example
We assume that the single-linked list has only 3 nodes, and that 3 nodes are located in the No. 0, 1, and 2 positions of the array node, respectively, from left to right, such as:
The head points to the 1th node in the list. The node is stored in the No. 0 position of the array node, and data is 111,next points to the next node. others and so on.
Note that the next of the last node in the list (that is, the 3rd node in the list) is set to 1, which means that it does not point to any nodes.
More importantly, these three nodes can be in any position in the array node, as long as they maintain the relationship between the links.
5. Actual program implementation
In the actual program implementation, in order to make it easier to determine which element in the array has been used, I added a judgment bit to the node data structure of the original single-linked list, as follows:
1typedefintElementType;2 classNodeType3 {4 Public:5NodeType () {data =0; Next = Null_value; Isfree =true; };6~NodeType () {};7 ElementType data;8 intNext;9 BOOLIsfree;//flag bit, true indicates that the node is available, and vice versa .Ten};
The remaining procedures are excerpted as follows:
linkedlist.h Linkedlist.cpp
The Boost unit test code is tested as follows:
BoostUnitTest.cpp
Personal "data structures and Algorithms" series Blog:
Linear table:
Sequential table:
Sequential table of arrays based on static allocations (both boost unit test)
Sequential table based on dynamically allocated arrays (with boost unit test)
Linked list:
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
Array-based single-link list (Boost unit test)