Data structure THREE

Linear table

A linear table is a finite sequence of N data elements of the same type

Keywords [Finite sequence of the same type]

1. The same data type
In the definition of linear table, we see that n data elements from a0 to an-1 are elements with the same attributes.
For example, they can all be numbers, such as (23, 14, 66, 5, 99);
they can also be characters, such as (A, B, C...Z); of
course, they can also be data elements with more complex structures, such as students , Commodities, equipment.
The same data type means that when stored in memory, each element will occupy the same memory space, which is convenient for subsequent query positioning.

2. Finite
The number n of data elements in the linear table is defined as the length of the money table, and n is a finite value.
When n=0, the linear table is empty.
In a non-empty linear table, each data element has a unique serial number in the linear table.
For example, the serial number of a0 is 0, and the serial number of ai is i.
In a linear table with n>0 data elements, the range of the data element number is [0, n-1].

3.Sequence order
There is an order-pair relationship between adjacent data elements of the linear table,
that is, ai-1 is the direct predecessor of ai, then ai is the direct follow-up of ai-1,
and at the same time ai is the direct of ai+1 The precursor, ai+1 is the direct follow-up of ai.
The only end of the element a0 that has no direct predecessor is called the head, and the
only end that has no subsequent element an-1 is called the tail.
Except for the header and footer elements, any element has and only has a direct predecessor and a direct successor.

Storage structure of linear table

1. Sequence table—Sequential storage structure

Features : Allocate continuous space in the memory, only store data, no need to store address information. The location implies the address.
Advantages:
1. Save storage space, because the storage unit allocated to the data is used to store the data of the node (regardless of the fact that the size of the array in the C/C++ language needs to be specified), the logical relationship between the nodes does not occupy additional storage space.
2. The index search efficiency is high, that is, each node corresponds to a serial number, and the storage address of the node can be directly calculated from the serial number.

Disadvantages:
1. Insert and delete operations need to move elements, and the efficiency is low.
2. A fixed amount of space must be allocated in advance. If there are few storage elements, it may lead to idle waste.
3. The efficiency of searching according to the content is low because it needs to compare and judge one by one.
The time complexity of the array query by content is O(n)

2 linked list-chain storage structure

Features : The storage of data elements corresponds to discontinuous storage space, and each storage node corresponds to a data element that needs to be stored.
Each node is composed of data field and pointer field. The logical relationship between the elements is reflected by the link relationship between the storage nodes.
Logically adjacent nodes need not be physically adjacent.
Disadvantages:
1. The storage density is lower than the sequential storage structure (each node is composed of a data field and a pointer field, so if the same space is full, the order is more than chain storage).
2. Chain storage is slower than sequential storage when searching for nodes (the address of each node is not continuous and irregular, resulting in low efficiency of querying by index).
Advantages:
1. Flexible insertion and deletion (no need to move the node, just change the pointer in the node, but you need to locate the element first).
2. Node space will be allocated only when there are elements, and there will be no idle nodes.

Guess you like

Origin blog.csdn.net/MAKEJAVAMAN/article/details/106804688