1, Basic concepts:

Data > data element > data item

Data element is the basic unit of data

A data item is the smallest unit of data

A data element is a collection of data elements with a structure

Data structure includes logical structure and storage structure

2, Algorithm

An algorithm must meet the following five important characteristics:

① Boundedness ② certainty ③ feasibility ④ input ⑤ output

The advantages and disadvantages of an algorithm should be evaluated from the following aspects:

① Correctness ② readability ③ robustness ④ efficiency

3, Implementation of basic operations in linear table

1. Initialization

① Dynamically allocate an array space of predefined size for sequential Table L, so that elem points to the base address of this space.

② Set the current length of the table to 0.

Status InitList_Sq(SqList &L){ //Construct an empty sequence table L.elem=new ElenType[MAXSIZE]; //Allocate space for sequential tables if(!L.elem) exit(OVERFLOW); //Storage allocation failed L.length=0; //Empty table length is 0 return OK; }

Extensions: predefined constants and types used in operation algorithms

//Function result status code #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 //Status is the type of function and its value is the result status code of the function typedef int Status; typedef char Elemtype;

2. Value (random access)

① Judge whether the specified position sequence number i value is reasonable. If it is unreasonable, ERROR is returned.

② If the value of i is reasonable, assign the ith data element to parameter e, and return the value of the ith element through e.

int GetElem(SqList L,int i,ElemType &e){ if(i<1||i>L.length)return ERROR; //Judge whether the i value is reasonable. If it is unreasonable, return ERROR e=L.elem[i-1]; //The unit i-1 stores the i-th data return OK; }

The time complexity is O(1)

3. Find (find by value)

① Find the position of the data element in the linear table L that is the same as the specified value e.

② Start from one end of the table and compare the recorded keywords with the given values one by one. If found, return the position sequence number of the element; if not found, return 0.

int LocateElem(SqList L,ElemType e){ //Find the data element with the value e in the linear table L and return its sequence number (which element is the first) for(i=0;i<L.length;i++) if(L.elem[i]==e) return i+1;//The search is successful, and the sequence number is returned return 0;//Search failed, return 0 }

The same can be achieved with the while statement:

int LocateElem(SqList L,ElemType e){ //Find the data element with the value e in the linear table L and return its sequence number (which element is the first) i=0; while(i<L.length&&L.elem[i]!=e) i++; if(L.elem[i]==e) return i+1;//The search is successful, and the sequence number is returned return 0;//Search failed, return 0 }

Average lookup length:

In order to determine the position of elements in the sequence table, the expected value of the number of data elements to be compared with the given value is called the average search length of the search algorithm when the search is successful.

The average time complexity is O(n)

4. Insert

① Judge whether the insertion position i is legal. If it is not, ERROR is returned.

② Judge whether the storage space of the sequence table is full. If it is full, ERROR is returned.

③ Move the elements from the nth to the ith position backward one position in turn, leaving the ith position (i=n+

1 without moving).

④ Put the new element to be inserted in position i.

⑤ Add 1 to the table length.

Status Listlnsert_Sq(SqList &L,int i,ElemType e){ if(i<1||i>L.length+1) return ERROR; //Illegal i value if(L.length==MAXSIZE) return ERROR; //The current storage space is full for(j=L.length-1;j>=i-1;j--) L.elem[j+1]=L.elem[j]; //Move back the element at and after the insertion position L.elem[i-1]=e; //Put the new element e in position i L.length++; //Table length increase 1 retuen OK; }

Algorithm analysis:

If it is inserted after the tail node, it does not need to move (especially fast);

If it is inserted before the first node, all elements in the table move back (n times);

To insert in various positions (n+1 possibilities):

The average time complexity of the sequential table insertion algorithm is O(n)

5. Delete

① Judge whether the deletion position i is legal. If it is not, ERROR is returned.

② Move the elements i+1 to n forward one position in turn (no need to move when i=n)

③ Table length minus 1.

Status ListDelete_Sq(SqList &L,int i){ if((i<1||i>L.length)) return ERROR; //Illegal i value for(j=i;j<=L.length-1;j++) L.elem[j-1]=L.elem[j]; //Move element forward after deleted element L.length--; //Table length minus 1 retuen OK; }

Algorithm analysis:

If the tail node is deleted, it does not need to be moved.

If the first node is deleted, all n-1 elements in the table will move forward.

To delete in various locations (n possibilities):

The average time complexity of the deletion algorithm of sequential table is O(n)