Linear table of DS, ds linear table

Linear table definition

A linear table is the most common and simple data structure. A linear table is a finite sequence of n data elements. The specific meaning of each data element varies under different circumstances. It can be a number or symbol, a book, or even more complex information.

In a slightly complex linear table, a data element can be composed of several data items. In this case, data elements are often referred to as records, and linear tables containing a large number of records are also referred to as files.

For example, it is a file: the situation of each student in the table is a record, which consists of five data items: name, student ID, gender, age, and health.

Linear table Representation

Elements in the same linear table must have the same attributes, that is, they belong to the same data object, and there is a sequence relationship between adjacent data elements. If the linear table is marked as (a1 ,..., ai-1, ai, ai + 1 ,..., an), then the table ai-1 ahead of ai, ai ahead of ai + 1, said ai-1 is the direct precursor element of ai, ai + 1 is the direct successor element of ai. When I =,..., n-1, ai has only one direct successor. When I =,..., n, ai has only one direct precursor.

The number of elements in a linear table is n (n> = 0) and is defined as the length of the linear table. If n is 0, it is called an empty table. Each data element in a non-empty table has a definite position. For example, a1 is the first data element, an is the last data element, and ai is the I data element, I is the order of the data element ai in a linear table.

A linear table is a flexible data structure. Its length can be increased or shortened as needed. That is, the data elements of a linear table can be accessed, inserted, or deleted.

Abstract linear tables of the Data Type can also be complicated. Two or more linear tables can be merged into a linear table. A linear table can be split into two or more linear tables; copy a linear table again.

Basic operations on Linear tables:

<Span style = "font-size: 18px;"> // basic linear table operation InitList (& L) // operation result: Create an empty table LDestroyList (& L) // initial condition: the linear table L already exists. Operation Result: destroy the linear table LClearList (& L). // initial condition: the linear table L already exists. Operation Result: reset L to empty table ListEmpty (L) // initial condition: Linear table L already exists operation result: Determine whether L is empty table ListLength (L) // initial condition: linear table L already exists operation result: returns the number of data elements in L. GetElem (L, I, & e) // initial condition: Linear table L already exists, 1 <= I <= ListLength (L) // Operation Result: use e to return the value of the I-th Data Element in L. LocateElem (L, e, compare ()) // initial condition: the linear table L already exists, and compare () is the data element determining function // Operation Result: determines whether to return the number of the first compare () in L that satisfies the relationship with e The order of data elements. ListInsert (& L, I, e) // initial condition: the linear table L already exists. 1 <= I <= ListLength (L) + 1 // operation result: insert the new data element e before the I position in L, and add the length of L to 1 ListDelete (& L, I, & e) // initial condition: the linear table L already exists and is not empty. 1 <= I <= ListLength (L) // Operation Result: Delete the I-th Data Element in L and return its value with e, L length minus 1 ListTraverse (L, visit () // initial condition: the linear table L already exists // operation result: Call visit () the function traverses all data elements in a linear table and outputs The result. </span>

Example 1: an algorithm that combines data elements of La AND Lb into Lb and does not contain repeated data elements is described as follows:

<Span style = "font-size: 18px;"> void union (List & La, list Lb) {La_len = ListLength (La ); // calculate the length of the two linear tables Lb_len = ListLength (Lb); for (int I = 1; I <= Lb_len; I ++) {GetElem (Lb, I, e ); // retrieve the I-th Data Element in Lb and assign it to eif (! LocateElem (La, e, equal) {ListInsert (La, ++ La_len, e ); // If Lb does not contain the same data element as e, insert it. }}</span>

Example 2: Merge La AND Lb into a new linear table Lc, and the sequence of the data elements in Lc is still non-decreasing:

<Span style = "font-size: 18px;"> void MergeLIst (List La, List Lb, List & Lc) {InitList (Lc ); // construct an empty table int I = j = 1; int k = 0; La_len = ListLength (La); // obtain the length of the two linear tables Lb_len = ListLength (Lb ); while (I <= La_len) & (j <= Lb_len) {int ai, bj; GetElem (La, I, ai); GetElem (Lb, j, bj); if (ai <= bj) {ListInsert (Lc, ++ k, ai); ++ I;} else {ListInsert (Lc, ++ k, bj ); ++ j ;}while (I <= La_len) {GetElem (La, I ++, ai); ListInsert (Lc, ++ k, ai );} while (j <= Lb_len) {GetElem (Lb, j ++, bj); ListInsert (Lc, ++ k, bj) ;}}</span>