### Python data structure and algorithm (2.1) — basic concept of linear table

## 0. Learning objectives

Linear table is the most widely used data structure. As its name shows, it is a typical linear structure. This section mainly introduces the concepts and basic operations of linear tables.

Through the study of this section, you should master the following contents:

- Definition of linear table
- Logical structure of linear table

## 1. Definition of linear table

Linear list is the most basic and commonly used data structure

L

=

(

a

1

,

a

2

,

…

,

a

i

,

a

i

+

1

,

…

,

a

n

)

L=( a_1, a_2, …, a_i, a_{i+1}, …, a_n)

L=(a1,a2,…,ai,ai+1,…,an)

Among them

The logical structure of non empty linear table can be represented by the following figure:

As shown in the above figure, it is not difficult to find that the logical structure of non empty linear table has the following characteristics:

- The data elements in the linear table are ordered according to the front and rear positions, i.e. the second row
- First data element in linear table
- The types of data elements in the linear table are the same, and the value of table length is a finite number, with a minimum of 0.

Linear table is very common in the real world. For example, 26 English alphabets – (a, B, C,…, z) can be regarded as a linear table, in which each letter is a data element; The evolution process of plant evolution can also be expressed in the form of linear table – (algae, naked ferns, ferns, gymnosperms, angiosperms); In a complex linear table, a data element can be composed of several data items. A student basic information table can be regarded as a linear table, in which all relevant information of each student constitutes a data element (also known as record). Each data element contains data items such as student number, name, gender, age, class, contact information, etc.

As can be seen from the above examples, although the specific contents of the data elements of the linear table are different in different situations in the real world, they all reflect the logical structure characteristic that the relative position between the data elements is linear.

## 2. Operation of linear table

The operation of data structure is defined at the logical structure level, and the specific implementation of operation is based on the storage structure. Therefore, as a part of the logical structure, the basic operations of the linear table defined below give the function of these operations as “what to do”, and the “how to do” depends on the selected storage structure.

The main operations of the sequence table include:

- Initialize linear table: construct an empty linear table
- Count: returns the number of elements in the list
- Get element by sequence number: read the element at the specified position in the linear table
- Find by value: finds the specified data element in the list
- Insert: inserts an element into the list
- Delete element: deletes and returns the specified location element from the list

## 3. Definition of abstract data type linear table

To sum up, the definition of abstract data type linear table is as follows:

ADT List:

data object:

data relationship:

basic operation:

1. __ itit__ (): initialize linear table

set linear table l as an empty table

2. __ len__ (): get and return the number of elements in the linear table n

if the linear table is empty, the integer 0 is returned

3. __ getitem__ (i) : read the ith data element in linear table L

where 1 ≤ I ≤ len (L)

4. Locate (x): find the data element with the value of X in the linear table L

if the search is successful, the sequence number of the first element with the value of X is returned; Otherwise, a special value (for example, – 1) is returned, indicating that the search failed

5. Insert (I, x): insert a new element with value x at position I of linear table L

where 1 ≤ I ≤ len (L) + 1, the table length increases by 1 after insertion; The serial numbers of data elements with original serial numbers of I, I + 1,…, n become I + 1, I + 2,…, N + 1

6. __ delitem(i)__: Delete the data element with sequence number I in linear table L

where 1 ≤ I ≤ len (L), the length of the table after deletion will be reduced by 1; Elements with original sequence numbers I + 1, I + 2,…, n become sequence numbers I, I + 1,…, n-1

The above only gives the most basic operations defined on the logical structure of linear table. In practical application, more complex operations can be constructed with the help of these basic operations.