2, Linear table
2.1 definition of linear table
Linear list: a finite sequence of zero or more data elements (with the same data type)
- Sequence. There is order between elements. When there are multiple elements, the first one has no precursor, the last one has no successor, and the others have only one precursor and successor.
- Finite, finite number of elements. Infinite sequences exist only in concepts.
if the linear table is recorded as(
a
1
a_1
a1,
a
2
a_2
a2,...
a
i
a_i
ai,...
a
n
a_n
an), then in the table
a
i
−
1
a_{i-1}
ai − 1 − ahead
a
i
a_i
ai,
a
i
a_i
ai ahead
a
i
+
1
a_{i+1}
ai+1, scale
a
i
−
1
a_{i-1}
ai − 1 − yes
a
i
a_i
The direct precursor element of ai,
a
i
+
1
a_{i+1}
ai+1 = yes
a
i
a_i
The direct successor element of ai. When i=1,2,... n-1, there is and only one direct successor, and when i=2,3,... n, there is and only one direct precursor.
the number of elements n (n ≥ 0) of a linear table is defined as the length of the linear table. When n=0, it is called an empty table. In a non empty table, each element has a certain position,
a
i
a_i
ai , is the i-th element, which is called the data element
a
i
a_i
The bit order of ai in a linear table.
Examples: constellation, point roster
2.2 abstract data type of linear table
Operations of linear table: create and initialize, reset to an empty table, get data elements according to bit order, find out whether an element exists, get the length of linear table, insert data and delete data.
The abstract data types of linear tables are as follows:
ADT Linear table(List) Data The data object set of the linear table is{a1, a2, ......, an},The type of each element is DataType. Where, except the first element a1 In addition, each element has and has only one direct precursor element, except the last element an In addition, each element has and has only one direct successor element. The relationship between data elements is one-to-one. Operation InitList(*L): Initialization operation to create an empty linear table L. ListEmpty(L): If the linear table is empty, return true,Otherwise return false. ClearList(*L): Empty the linear table. GetElem(L, i, *e): Add linear table L Section in i A location element value is returned to e. LocateElem(L, e): In linear table L Find and given values in e Equal elements. If the search is successful, the sequence number of the element in the table indicates success; Otherwise, a return of 0 indicates failure ListInsert(*L,i,e): In linear table L Section in i Insert new element at e ListDelete(*L,i,*e): Delete linear table L Section in i A location element and e Returns its value ListLength(L): Return linear table L Number of elements EndADT
the above operations are the most basic, and complex operations can be realized by a combination of basic operations.
for example, to realize the union of linear sets a and B. Loop each element in set B to judge whether the current element exists in A. if it does not exist, insert it into a.
/* Insert all data elements in the linear table Lb but not in La into la */ void unionL(List *La, List Lb) { int La_len, Lb_len, i; ElemType e; /* Declare the same data element e as La and Lb */ La_len = ListLength(La); /* Find the length of linear table */ Lb_len = ListLength(Lb); for (i = 1; i <= Lb_len; i++) { GetElem(Lb, i, *e); /* Take the ith data element in Lb and assign it to e */ if (!LocateElem(La, e)) /* La The same data element as e does not exist in */ ListInsert(*La, ++La_len, e); /* insert */ } } //The basic operations ListLength, GetElem, LocateElem and ListInsert are used
2.3 sequential storage structure of linear table
2.3.1 definition of sequential storage
The sequential storage structure of linear table refers to the sequential storage of data elements of linear table with a section of storage units with continuous addresses.
2.3.2 sequential storage mode
Each data element of the linear table has the same type, so the sequential storage structure can be realized by using the one-dimensional array of C language, that is, the first data element is stored in the position where the index of the array is 0, and then the adjacent elements of the linear table are stored in the adjacent position in the array.
Sequential storage structure code of linear table:
#define MAXSIZE 20 / / initial allocation of storage space typedef int ElemType; /* ElemType The type depends on the actual situation. It is assumed to be int */ typedef struct { ElemType data[MAXSIZE]; /* The array stores data elements. The maximum value is MAXSIZE */ int length; /* Current length of linear table */ } SqList;
Three properties of sequential storage structure
- Starting position of storage space: the position of array data
- Maximum storage capacity of linear table: maxsize
- Current length of linear table: length
2.3.3 difference between data length and linear table length
The length of the array is the length of the storage space where the linear table is stored. This amount is generally unchanged after storage allocation.
The length of a linear table is the number of data elements in the linear table. This amount changes with linear table insertion and deletion operations.
Length of linear table ≤ length of array
2.3.4 address calculation method
The linear table count starts from 1 and the array subscript starts from 0. Therefore, the ith element of the linear table is stored at the position where the array subscript is i-1.
LOC indicates the acquisition address, c indicates the size of an element, and the ith data element
a
i
a_i
The address of ai can be
a
1
a_1
a1. Calculated.
LOC(
a
i
a_i
ai+1)=LOC(
a
i
a_i
ai)+c
LOC(
a
i
a_i
ai)=LOC(
a
1
a_1
a1)+(i−1)∗c
The data stored or retrieved from each linear table position is the same time for the computer, that is, a constant, so its access time performance is O(1). The storage structure with this characteristic is called random storage structure.
2.4 insertion and deletion of sequential storage structure
2.4.1 get element operation
Implement the GetElenm operation, that is, return the value of the ith position element in the linear table L.
#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; /* Status Is the type of function whose value is the result status code of the function, such as OK */ /* Initial condition: sequential linear table l already exists, 1 ≤ i ≤ ListLength(L) */ /* Operation result: use e to return the value of the ith data element in L */ Status GetElem(SqList L, int i, ElemType *e) { if (L.length == 0 || i < 1 || i > L.length) return ERROR; *e = L.data[i - 1]; return OK; }
2.4.2 insertion operation
ListInsert(*L,i,e), insert a new element E at the ith position in the linear table L.
Idea of insertion algorithm:
- If the insertion position is unreasonable, an exception is thrown
- If the length of the linear table is greater than or equal to the length of the array, an exception is thrown or the capacity is increased dynamically
- Traverse forward from the last element to the i-th position, and move them back one position respectively
- Fill in the element to be inserted at position i
- Table length plus 1
Implementation code
/* Initial condition: sequential linear table l already exists, 1 ≤ i ≤ ListLength(L), */ /* Operation result: insert a new data element e before the ith position in L, and add 1 to the length of L */ Status ListInsert(SqList *L, int i, ElemType e) { int k; if (L->length == MAXSIZE) /* The sequential linear table is full */ return ERROR; if (i < 1 || i >L->length + 1) /* When i is out of range */ return ERROR; if (i <= L->length) /* If the inserted data position is not at the end of the table */ { for (k = L->length - 1; k >= i - 1; k--) //Move the data element back one bit after the position to be inserted L->data[k + 1] = L->data[k]; } L->data[i - 1] = e; /* Insert new element */ L->length++; return OK; }
2.4.3 deleting
Delete algorithm idea:
- If the deletion position is unreasonable, an exception is thrown;
- Remove and delete elements;
- Traverse from the deleted element position to the last element position, and move them forward one position respectively;
- Table length minus 1
Implementation code:
/* Initial condition: sequential linear table l already exists, 1 ≤ i ≤ ListLength(L) */ /* Operation result: delete the ith data element of L and return its value with e. the length of L is reduced by 1 */ Status ListDelete(SqList *L, int i, ElemType *e) { int k; if (L->length == 0) /* The linear table is empty */ return ERROR; if (i < 1 || i > L->length) /* The deletion location is incorrect */ return ERROR; *e = L->data[i - 1]; if (i < L->length) /* If the deletion is not the last position */ { for (k = i; k < L->length; k++) /* Move the deletion position and subsequent elements forward */ L->data[k - 1] = L->data[k]; } L->length--; return OK; }
Insertion and deletion time complexity: best case O(1), worst case O(n).
The sequential storage structure of linear table. When saving and reading data, the time complexity is O(1), and when inserting or deleting, the time complexity is O(n). Therefore, it is suitable for applications where the number of elements does not change much, but more access data.