Data Structure: sequential storage structure of linear tables

Source: Internet
Author: User

The data object set of a linear table is {A1, A2,... an}. The type of each element is ype. Except for the first element A1, each element has only one direct precursor element. Except for the last element an, each element has only one direct successor element. The relationship between data elements is one-to-one.


Advantages and disadvantages of the sequential storage structure of linear tables:

Advantage: you do not need to add additional storage space to represent the logical relationship between elements in the table. You can quickly access the element O (1) at any position in the table)

Disadvantages: The insert and delete operations require moving a large number of elements O (N). When the length of a linear table changes greatly, it is difficult to determine the storage space capacity, resulting in "Fragmentation" of the storage space"


The sample program is as follows (adapted from the big talk Data Structure):

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
# Include <iostream>
Using namespace STD;

# Define maxsize 20

Typedef int elemtype;

Typedef struct
{
Elemtype data [maxsize];
Int length;
} Sqlist;

/* Initialize the ordered linear table */
Bool initlist (sqlist * PTR)
{
For (INT I = 0; I <maxsize; I ++)
PTR-> data [I] = 0;
PTR-> length = 0;
Return true;
}

Bool listempty (sqlist sq)
{
If (sq. Length = 0)
Return true;
Else
Return false;
}

Bool clearlist (sqlist * PTR)
{
For (INT I = 0; I <PTR-> length; I ++)
PTR-> data [I] = 0;
PTR-> length = 0;
Return true;
}
/* Use PTR to return the value of the second POS data element in sq. Note that POS refers to the position, and the array of the second position starts from 0 */
Bool getelem (sqlist SQ, int POs, elemtype * PTR)
{
If (sq. Length = 0 | POS <1 | POS> sq. length)
Return false;
* PTR = SQ. Data [pos-1];
Return true;
}
/* Return the order of the 1st data elements that meet the ELEM requirements in sq. If such a data element does not exist, the return value is 0 */
Int locate (sqlist SQ, elemtype ELEM)
{
For (INT I = 0; I <sq. length; I ++)
{
If (sq. Data [I] = ELEM)
Return I + 1;
}
Return 0;
}
/* Insert a new data element ELEM before the POs position in sq. The length of L is increased by 1 */
Bool listinsert (sqlist * PTR, int POs, elemtype ELEM)
{
If (PTR-> length = maxsize)/* The ordered linear table is full */
Return false;
If (Pos <1 | POS> PTR-> Length + 1)
Return false;
If (Pos <= PTR-> length)
{
/* Move one data element after the position to be inserted to the backend */
For (INT I = PTR-> length-1; I> = pos-1; I --)
{
PTR-> data [I + 1] = PTR-> data [I];
}
}

PTR-> data [pos-1] = ELEM;/* Insert new elements */

PTR-> length ++;
Return true;
}
/* Delete the second POS data element of PS and return its value with PE. The length of PS is reduced by 1 */
Bool listdelete (sqlist * ps, int POs, elemtype * PE)
{
If (Pos <1 | POS> PS-> length)
Return false;
* Pe = ps-> data [pos-1];
/* Move the deleted position successor element forward */
For (INT I = Pos; I <ps-> length; I ++)
PS-> data [I-1] = ps-> data [I];

PS-> length --;

Return true;

}

Int listlength (sqlist sq)
{
Return sq. length;
}

/* Insert all the elements in the Pb but not in the PA of the linear table into the PA */
Void unionlist (sqlist * pA, sqlist * pb)
{
Int Lena = pa-> length;
Int lenb = Pb-> length;
Int item;

For (INT I = 0; I <lenb; I ++)
{
If (getelem (* pb, I + 1, & item ))
{
If (locate (* pA, item) = 0)
Listinsert (Pa, ++ Lena, item );
}
}

}

Int main (void)
{
Sqlist sq;
Initlist (& sq );
For (INT I = 1; I <5; I ++)
Listinsert (& SQ, I, I );

If (! Listempty (SQ ))
{
Cout <"SQ:" <Endl;
For (INT I = 0; I <listlength (SQ); I ++)
Cout <sq. Data [I] <'';
}
Cout <Endl;

Int Pos = locate (SQ, 2 );
If (Pos! = 0)
{
Int result;
Listdelete (& SQ, POs, & result );
Cout <"delete:" <result <Endl;
}

If (! Listempty (SQ ))
{
Cout <"SQ:" <Endl;
For (INT I = 0; I <listlength (SQ); I ++)
Cout <sq. Data [I] <'';
}
Cout <Endl;

Sqlist sq2;
Initlist (& sq2 );
For (INT I = 1; I <4; I ++)
Listinsert (& sq2, I, 6 );
Listinsert (& sq2, 4, 7 );
If (! Listempty (sq2 ))
{
Cout <"sq2:" <Endl;
For (INT I = 0; I <listlength (sq2); I ++)
Cout <sq2.data [I] <'';
}
Cout <Endl;

Unionlist (& SQ, & sq2 );

If (! Listempty (SQ ))
{
Cout <"SQ:" <Endl;
For (INT I = 0; I <listlength (SQ); I ++)
Cout <sq. Data [I] <'';
}
Cout <Endl;

Return 0;
}

Output:


Note:

1. The position POS mentioned in the program should be the serial number plus 1. For example, the first element serial number is 0 and the position is 1.

2. the functions mentioned in the program are the most basic operations. A combination of basic operations can be used for more complex operations, the unionlist above is the union of A and B in two linear table sets.

3. confusing for beginners: insertion or deletion does not actually perform memory operations, but only moves and overwrites elements. The length member records the length of the current linear table, however, the total length is determined by maxsize. The linear table is stored on the stack and will be released when the function returns.



Related Article

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.