Learn data structures-> linear tables-> introduction to linear tables
A linear table is a typical data structure. The basic feature of a linear structure is that the data elements in a linear table are ordered and limited. In a linear structure, there is only one "start data element" and one "last data element", except that the start data element has no direct precursor, And the last data element has no direct successor, the rest of the data elements have only one direct precursor and direct successor.
The linear table has the following basic features:
1>. There must be a unique "start data element" in a linear structure ";
2>. There must be a unique "last data element" in a linear structure ";
3>. Except the first data element, other elements have a unique direct precursor;
4>. Except for the last data element, each element has a unique direct successor.
I. Linear table definition
Linear Structure example:
①. Uppercase English alphabet: A = {A, B, C,..., x, y, z };
②. Seven days in a week: a = {Monday, Tuesday, Wednesday, thureday, Friday, Saturday, Sunday };
In this way, from a group of nodes with a length of n (n ≥ 0) A1, A2 ,..., an finite sequence is called a linear table. Each member of a linear table is called a node of a linear table.
When n = 0, the linear table is empty.
If n> 0, A1 is the first element of the linear table, and an is the last element) both have the only direct precursor A (I-1) and direct successor a (I + 1 ).
II. Logical Structure of a linear table
in a linear table, a data element can be composed of several data items, such as a score table. In this linear table, any data element AI contains student ID, name, course name, score, and other data items.
the length of a linear table can be increased or shortened as needed, and each element in a non-empty linear table has an exact position. That is to say, data elements in a linear table can be accessed, inserted, deleted, and other operations.
the types of data elements in a linear table can be varied, but the data elements in a linear table must have the same characteristics, that is, the elements in a linear table belong to the same data object, there is an ordinal relationship between adjacent data elements.
3. Basic operations on Linear tables
For a linear table, the possible operations are as follows:
● Create a linear table;
● Judge whether ten linear tables are empty;
● Determine the length of a linear table;
● Find the k-th element;
● Search for the specified element;
● Delete the k-th element;
● Insert a new element before the K element;
● Destroy a linear table.
Therefore, we can first define the following basic operations for a linear table:
1>. initialize the linear tableInitialist (l)
Initial Condition: None
Operation Result: Construct an empty or required linear table L and return the linear table L.
2>. Empty linear tableSetnulllist (l)
Initial Condition: The linear table l already exists.
Operation structure: Leave the existing linear table L empty and return the linear table L.
3>. Calculate the length of a linear table.Getlistlength (l)
Initial Condition: The linear table l already exists.
Operation structure: returns the number of data elements in linear table L.
4>. Find the I-th node of the linear table.Getlistitem (L, I)
Initial Condition: The linear table l already exists, and 1 ≤ I ≤ n
Operation Result: if the initial condition is met, the I-th data element is returned. Otherwise, null is returned.
5>. Obtain the direct precursor of the data element whose median value is X in a linear table.Getitemprior (L, X)
Initial Condition: The linear table l already exists, and the position I of Data X must be 2 ≤ I ≤ n
Operation Result: If X is the data element in the linear table L and is not the first, the direct precursor of the data element is returned. Otherwise, null is returned.
6>. Obtain the direct successor of the data element whose median value is X in a linear table.Getitemnext (L, X)
Initial Condition: The linear table l already exists, and the position I of Data X must be 1 ≤ I ≤ n-1
Operation Result: If X is the data element in the linear table L and is not the last one, the direct successor of the data element is returned. Otherwise, null is returned.
7>. Insert nodesInsertitem (L, I, B)
Initial Condition: The linear table l already exists, and 1 ≤ I ≤ n + 1
Operation Result: A new data element with a value of B is inserted before the I data in the linear table L. The length of the linear table L is increased by 1.
8>. delete a nodeDeleteitem (L, I)
Initial Condition: The linear table l already exists, and 1 ≤ I ≤ n
Operation Result: the I-th Data Element in the linear table L is deleted, and the length of the linear table L is reduced by 1.
In subsequent linear table operations, you can combine the above basic operations.
4. ADT description of linear table
A linear table can be described using an abstract data type (ADT). The description is as follows:
ABSTRACT Data Type ADT linearlist
{
Data Object: D = {ai | ai ε elemset, I = 1, 2,..., n, n ≥ 0}
Data Relationship: r = {<A (I-1), AI> | A (I-1), AI in D, I = 2, 3,..., n}
Basic operations:
Create (): Create an empty linear table
Destroy (): Destroy a table
Isempty (): If the table is empty, true is returned; otherwise, false is returned.
Length (): Returns the number of elements in the table.
Find (K, X): Find the K element in the table. If it is found, save it to X. If it does not exist, false is returned.
Index (X): Returns the position of element x In the table. If no table exists, 0 is returned.
Delete (K, X): Delete the K element in the table and save it to X. The function returns the modified linear table.
Insert (K, X): Insert X before k elements. The function returns the modified linear table.
}
V. Sequential storage of linear tables
In a computer, you can use the sequential Storage Structure and chained storage structure to represent linear tables.
The sequential storage of linear tables refers to the use of a set of continuous storage units to store linear tables. For example, linear tables n = {10, 20, 30, 40, 50, 60, 70, 80, 90,100} storage:
Sequential storage stores each element of a linear table one by one in a continuous storage unit. A linear table stored in sequential storage is also called an ordered table. Its features are as follows:
①. The logical sequence of a linear table is the same as that of a physical table;
②. The relationship between data elements is achieved through the adjacent relationship of physical locations.
Assume that each element in a linear table occupies C storage units and uses the storage address of the first unit as the storage start position of the data element, then the storage location LOC (AI) of the I element in a linear table and the storage location LOC (A (I-1) of the I-1 element satisfy the following relationship:
LOC (AI) = LOC (A (I-1) + C)
Assume that the storage address of the first data element A1 in a linear table is LOC (A1), and each data element occupies C storage units in the memory of the computer. The characteristics of the linear table can be seen, the storage address of the I-th Data Element in the memory of the computer in a linear table is:
LOC (AI) = LOC (A1) + (I-1) * C
Therefore, once the starting address of a linear table is determined and the storage unit size occupied by each element in the computer memory is determined, the storage address of any data element in the linear table can be calculated. Therefore, the sequential storage structure of a linear table can be understood as the Random storage structure of a linear table.
In advancedProgramIn the design language, arrays have random access. Therefore, an array is usually used to describe the sequential storage structure in the data structure. In this way, the sequential storage structure of a linear table is defined:
# DefineMaclen <maximum possible length of a linear table>TypedefStructSequenlist {elementtype elements [maxlen];//Data Element value data typeIntLength;//Number of current elements in a linear table} List;
Elementtype can be either a simple data type or a composite data type.
①.Simple data type example:
CharElementtype [128];
②.Examples of composite data types:
TypedefStructEtype {CharNo [10];CharName [60];FloatScore;} elementtype;
6. Chain storage of linear tables
Chained Storage refers to the use of a group of arbitrary storage units (continuous or discontinuous) in a computer to store the data elements of a linear table, the domain sequence storage structure of the chain storage structure is mainly different from the following two aspects:
1>. The physical locations of two consecutive elements do not need to be adjacent;
2> the storage order of nodes in the storage unit is not required to be consistent with that in the linear table. That is to say, in the chained storage structure, the storage location of elements can be arbitrary.
The chain storage structure stores linear tables L = {a, B, c, d, e:
In the chain storage structure, only knowing the location of one element and the location of other elements cannot be determined. In order to display the order and location of each node in the linear table, in addition to saving the value of a node, a message pointing to the address of the next node must be provided, so that each node is divided into two fields. One is the domain that stores the information of data elements, called the data domain, the second is the domain that stores the address of the successor node, which is called the pointer domain. The information stored in the pointer domain is called the pointer or chain. Link n nodes to form a linked list ). The node Structure of the linked list is as follows:FigureShows the structure of the linked list.Figure B.
In this way, a linked list containing only one pointer field is called a single-chain table. In this single-linked list, the node with the node value of A is the direct precursor node of the node with the node value of B, and the node with the node value of C is the successor node of the node with the node value of B. A node with a node value of a does not have a direct precursor node, and a node with a node value of E does not have a direct successor node.
According to the preceding descriptions, the chain storage structure of a linear table can be defined:
TypedefStructNode {elementtype data;//Data domain of a nodeStructNode * next;//Pointer field of a node} Linklist;
In this way, the following figure shows the storage structure of the linear table L = {A0, A1, A2,...,:
Sometimes, for ease of operation, people always attach a node before the first node of the linked list, called the head node ). The data field of the header node can store additional information, such as the length of the linked list, without any information ,:
--------------------
WID, 2012.10.07
Previous Article: Learning data structures-> introduction to Algorithms