Data Structure: chain storage structure of linear tables and reverse of Single-Chain tables

Source: Internet
Author: User

In order to express the logical relationship between each data element AI and its direct successor element ai + 1, in addition to storing its own information, data AI also needs to store a command to indicate its

Information of the direct successor (that is, the storage location of the direct successor ). These two pieces of information constitute the storage image of the data element AI, known as the node ). N node chains form a linked list,

This is the chain storage structure of a linear table (A1, A2,..., an). Because each node of this linked list contains only one pointer field, it is called a single-chain table.

The storage location of the first node in the linked list is called the head pointer. To facilitate operations on the linked list, such as the special case of deleting the first node

(The first node does not have a precursor, but to remove a node, you must first find its precursor to perform the removal operation.) A node is often attached to the first node of a single-chain table,

It is called a header node, so the header Pointer Points to the header node.


The advantages and disadvantages of a single-chain table and an array are basically the opposite. That is, a single-chain table has a high insertion and deletion efficiency, while searching requires traversal, which is less efficient.


Example program: (adapted from "big talk Data Structure", added chain table inversion, etc)

C ++ code
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Include <iostream>
# Include <stdlib. h>
# Include <time. h>
Using namespace STD;

Typedef int elemtype;

Typedef struct Node
{
Elemtype data;
Struct node * next;
} Node;

Typedef node * nodeptr;

Bool initlist (nodeptr * NPP)
{
* NPP = (nodeptr) malloc (sizeof (node);/* generate a header node and point * NPP to this header node */
If (! (* NPP ))
Return false; // allocation failed

(* NPP)-> next = NULL;/* the pointer field is empty */

Return true;
}

Bool listempty (nodeptr np)
{
If (NP-> next = NULL)
Return true;
Else
Return false;
}

Bool clearlist (nodeptr * NPP)
{
Cout <"clear list..." <Endl;
Nodeptr P = * NPP;
If (! (P-> next ))
Return true;
While (p-> next)/* not at the end of the table */
{
Nodeptr q = p-> next;
P-> next = Q-> next;
Free (Q );
}

Return true;
}

Int listlength (nodeptr np)
{
Cout <"list's length :";
Nodeptr P = NP-> next;/* P points to the first node */
Int I = 0;

While (P)
{
P = p-> next;
++ I;
}

Return I;
}
/* Operation result: Use PTR to return the value of the POS data element in NP */
Bool getelem (nodeptr NP, int POs, elemtype * PTR)
{
Cout <"get item from POS" <POS <":";
Nodeptr P = NP-> next;
Int I = 1;
/* When P is not empty or the counter I is not equal to POS, the loop continues */
While (P & I <POS)
{
P = p-> next;
++ I;
}

If (! P)
Return false;
* PTR = p-> data;/* obtain the data of the POs element */
Return true;
}
/* Return the order of the 1st data elements in the NP that match the ELEM. */
/* If such a data element does not exist, the returned value is 0 */
Int locateelem (nodeptr NP, elemtype ELEM)
{
Cout <"item" <ELEM <"'s pos :";
Nodeptr P = NP-> next;
Int I = 1;

While (P & P-> data! = ELEM)
{
P = p-> next;
++ I;
}

If (! P)
Return 0;
Return I;
}
/* Operation result: a new data element ELEM is inserted before the POs position in NP. The length of NP is increased by 1 */
Bool listinsert (nodeptr * NPP, int POs, elemtype ELEM)
{
Cout <"Insert list POS" <POS <"item" <ELEM <Endl;
Nodeptr P = * NPP;
Int I = 1;

While (P & I <POS)
{
P = p-> next;
++ I;
}

If (! P)
Return false;

Nodeptr in = (nodeptr) malloc (sizeof (node ));
In-> DATA = ELEM;
In-> next = p-> next;/* assign the successor node of P to the successor node of in */
P-> next = In;/* assign the in value to the successor of p */

Return true;
}
/* Delete the POS data element of NPP and return its value with PTR. The length of NPP is reduced by 1 */
Bool listdelete (nodeptr * NPP, int POs, elemtype * PTR)
{
Cout <"delete list item in POS" <POS <Endl;
Nodeptr P = * NPP;
Int I = 1;

While (P & I <POS)
{
P = p-> next;
++ I;
}

If (! P)
Return false;

Nodeptr q = p-> next;
* PTR = Q-> data;
P-> next = Q-> next;/* assign the Q successor to p */
Free (Q );

Return true;
}

Bool listtraverse (nodeptr np)
{
Cout <"list's items :";
Nodeptr P = NP-> next;
While (P)
{
Cout <p-> data <'';
P = p-> next;
}
Cout <Endl;
Return true;
}
/* Randomly generate N element values and create a single-chain linear table NPP (Head Insertion Method) with table header nodes )*/
Void createlisthead (nodeptr * NPP, int num)
{
Cout <"create list from head..." <Endl;
If (* NPP! = NULL)
Free (* NPP );
* NPP = (nodeptr) malloc (sizeof (node ));
(* NPP)-> next = NULL;/* create a single-chain table with the leading node first */
Srand (Time (null ));

For (INT I = 0; I <num; I ++)
{
Nodeptr P = (nodeptr) malloc (sizeof (node ));
P-> DATA = rand () % 100 + 1; // Random Number
P-> next = (* NPP)-> next;
(* NPP)-> next = P;/* insert to the header */
}

}
/* Randomly generate N element values, and create a single-chain linear table NPP (tail Insertion Method) with table header nodes )*/
Void createlisttail (nodeptr * NPP, int num)
{
Cout <"create list from tail..." <Endl;
If (* NPP! = NULL)
Free (* NPP );
* NPP = (nodeptr) malloc (sizeof (node ));
(* NPP)-> next = NULL;
Srand (Time (null ));

Nodeptr tail = * NPP;/* tail is the node pointing to the end */
For (INT I = 0; I <num; I ++)
{
Nodeptr P = (nodeptr) malloc (sizeof (node ));
P-> DATA = rand () % 100 + 1;
Tail-> next = P;/* point the pointer to the End Node of the table to the new node */
Tail = P;/* define the current new node as the end node of the table */
}

Tail-> next = NULL;
}
/* Reverse a single-chain table */
Nodeptr reverselist (nodeptr head)
{
Cout <"reverse list..." <Endl;
If (null = head-> next | null = head-> next)
Return head;
Nodeptr P;
Nodeptr Q;
Nodeptr R;
P = head-> next;
Q = p-> next;
Head-> next = NULL;
While (q)
{
R = Q-> next ;//
Q-> next = P;
P = Q ;//
Q = r ;//
}
Head-> next = P;
Return head;
}

Int main (void)
{
Nodeptr NP; // header pointer
Initlist (& NP );
For (INT I = 1; I <5; I ++)
Listinsert (& NP, I, I );

If (! Listempty (NP ))
Cout <listlength (NP) <Endl;
Listtraverse (NP );

Cout <locateelem (NP, 3) <Endl;

Int get;
Getelem (NP, 2, & get );
Cout <get <Endl;

Clearlist (& NP );
Cout <listlength (NP) <Endl;

Createlisthead (& NP, 10 );
Listtraverse (NP );
Int result;
Listdelete (& NP, 5, & result );
Listtraverse (NP );

Clearlist (& NP );
Createlisttail (& NP, 10 );
Listtraverse (NP );
Listtraverse (reverselist (NP ));

Clearlist (& NP );

Return 0;
}


Output:


Note: The single-chain table reversal method used in the program is to use p and q to work together, so that the two nodes are directed to the reverse, and the remaining linked list is recorded with R. It is a single-chain table reversal condition that does not include the header node. There are minor differences between single-chain table reversal with the header node. Be careful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.