The sequential storage structure and realization of linear table

Sequential storage structure definition of linear table

1. Introduction of linear table

Linear table is the most basic, simplest, and most commonly used data structure.
The relationship between the data elements in the linear table is a one-to-one relationship, that is, except for the first and last data elements, other data elements are connected end to end (note that this sentence only applies to most linear tables , But not all. For example, the circular linked list is also a linear list at the logical level (chain storage at the storage level), but the tail pointer of the last data element points to the first node).

2. What is a sequence table?

1 : The sequential storage of a linear table is called a sequential list. The basic idea is to store the data elements of the linear table sequentially with a memory cycle unit of a segment of address continuous storage unit, as shown in Figure 1. Assuming that each element of the sequence table occupies c storage units, the storage address of the i-th element is:
LOC( ai )=LOC( a1 )+( i-1)*c

LOC(ai)=LOC(a1)+(i-1)*c

Insert picture description here

                          图1  顺序表中元素ai的存储地址

2 : It is easy to see that the storage address of the data element in the sequence table is a linear function of its sequence number. As long as the starting place (base address) of the storage sequence table is determined, the time to calculate the storage address of any element is equal The storage structure with this characteristic is called random access structure.

Three, the length of the array and the length of the linear table

1 : Usually a one-dimensional array is used to implement the sequence table, that is, the adjacent elements in the linear table are stored in adjacent positions in the array, which results in a sequence number between the data element and the array subscript where it is placed. One correspondence is shown in Figure 2. It should be emphasized that the subscript of the array in C language starts from 0, and the sequence number of the element in the linear table starts from 1, that is to say, the i-th element in the linear table is stored in the array as subscript i -1 position.

2 : The array needs to allocate a fixed-length array space, therefore, the length of the array space for storing the linear table must be determined. Because the insertion operation can be performed in the linear table, the length of the array must be greater than the length of the current linear table. Use MaxSize to represent the length of the array, and use to length
represent the length of the linear table, as shown in Figure 2.

                         图2  数组的长度和线性表的长度具有不同含义

The storage structure definition of the sequence table is given below:

#define MaxSize 100    //假设顺序表最多存放100个元素
typedef int Datarype; //定义线性表的数据类型,假设为int型
typedef struct
{
    
    
 Datarype data[Maxsize]; //存放数据元素的数组
 int length;             //线性表的长度
 }SeqList;               //顺序表类型

Guess you like

Origin blog.csdn.net/qq_45848361/article/details/109874195