After learning the data structure roughly, I didn't understand the logic at all. Later, I understood the importance of learning the data structure and planned to use a large period of free time to pick up the learning of the data structure again. I'm still standing at the door of the IT industry. I'm going to climb in step by step and kneel to appreciate the notes and experience of the boss. For beginners of data structure, it may be helpful to you. If you are lucky to get the guidance of the boss, you will be lucky.

Some basic operations of linear table (at the same time, supplement the logic and principle of the following functions):

- Construct a linear table structure ---- equivalent to the structure in C language. Extract, frame, define the type and limit the scope of the instance structure, and finally take the alias
- Initialize linear table ---- assign initial value to the defined value
- Destroy and empty the linear table ---- the delete method in C + + is directly used for destruction, which is fast, accurate and ruthless; Clearing is to directly assign the length of the linear table to 0, and the length is 0, that is, there is no data record
- Find the length of linear table ---- there are parameters of storage length in the structure. Just access it directly
- Judge whether the linear table is empty - that is, judge whether the length is not 0, 0 is empty, and non-0 is the value in the table
- Value, search, insert and delete ------ (the following code directly reflects the logic)

The code adopts C language and a little c + + syntax. It only shows the function code (with some comments), and main() won't show it any more.

#include <stdio.h> #include <stdlib. h> / / allocate memory to the table. This header file is required #define MAXSIZE 100 - maximum length of linear table /*Supplement: the following macro definitions are the predefined constants and types used in the operation algorithm Indicates the result state of the function */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 / / error #define INFEASIBLE -1 / / infeasible #define OVERFLOW -2 / / overflow typedef int Status; // Status is the type of the function, and its value is the type of the result returned by the function. It is defined here as int type (used later to return the address subscript) typedef char ElemType; // Define the type of data stored in the linear table as char

// Construct the storage structure of linear table typedef struct{ ElemType elme[MAXSIZE]; // ElemType the type elem defined above can be regarded as the base address (first address) of the linear table int length; // Storage length }SqList; //Structure name

// Construct an empty linear table - > initialize linear table} instantiate L here Status InitList_Sq(SqList &L) { L.elme = new ElemType[MAXSIZE]; // Allocate memory space to L (C + + method) if(!L.elme) exit(OVERFLOW); // Here is exception handling. Allocation succeeded. L is an empty table. Allocation failed. L is not empty. OVERFLOW storage allocation failed L.length = 0; // Assign an initial value, and the length of the empty table is 0 return OK; // The return result corresponds to the return type status - > int of the function body }// InitList_Sq

// Destroy linear table void DestroyList(SqList &L) { if(L.elme) delete L.elme; // C + + method delete }//DestroyList

// Reset L to empty table void ClearList(SqList &L) { L.length = 0; // Set length to 0 }//ClearList

// Returns the length of the linear table int GetLength(SqList L) { return (L.length); // Take the length parameter value }// GetLength

// Judge whether the linear table is empty int IsEmpty(SqList L) { if(L.length == 1) return 1; else return 0; // Take out the table length value for judgment }//IsEmpty

// Value int GetElem(SqList L,int i,ElemType &e) { if(i<1 || i>L.length) return ERROR; // Judge whether the position i is legal. Since the linear table is a finite sequence, it can only remove the existing value, that is, the value from the first element to the last element, otherwise it is invalid e = L.length[i-1]; // Assign the value of position i to e where the result is stored return OK; // Return the operation result (successful or not) }//GetElem

// lookup int LocateElem(SqList L,ElemType e) { for(i=0;i<L.length;i++) if(L.elme[i] == e) return i+1; // Judge the value corresponding to the subscript, and return the value corresponding to the subscript. i is the physical address (subscript), and i+1 is the logical address (value) return 0; }//LocateElem

// insert Status ListInsert_Sq(SqList &L,int i,ElemType e) { if(i<1 || i>L.length+1) return ERROR; // Judge legitimacy if(L.length == MAXSIZE) return OVERFLOW; // Judge whether the table is full. Since the size of the linear table cannot be changed, no other value can be inserted when it is full, otherwise it will overflow for(j=L.length-1;j>=i-1;j--) // Start with the last element and move back one by one until there is room for insertion L.elme[j+1] = L.elme[j]; // Move the first element to the next position L.elme[i-1]=e; // Spare space for e L.length++; // Length after insertion + 1 return OK; }//ListInsert_Sq

// delete Ststus ListDelete_Sq(SqList &L,int i) { if(i<1 || i>L.length) return ERROR; for(j=i;j<=L.length-1;j++) // Move forward one by one from the last element to be deleted L.elme[j-1] = L.elme[j]; // Directly squeeze out the elements to be deleted L.length--; // Length - 1 after deleting 1 element return OK; }//ListDelete_Sq

Supplement 1:

Some people may have questions. Why are some parameters & L and some are l?

& denotes reference in C + +, so & L uses reference to pass parameters. Reference parameter transfer is to directly transfer the address of the linear table for operation. You can directly modify the value of L in the linear table. L must be modified (i.e. re assignment, addition and deletion of elements) in initialization, emptying, destruction, insertion, deletion and other operations. When & L is not used in the function (i.e. L is used for parameter transfer), it means that I only need the value in the linear table L, and I do not need the linear table l to modify any value for me. What is l when calling and what is l when returning to the main function.

So why not use pointers instead of references? The pointer in C language is known to all who have touched it. It is easy to look at the pointer pointing around, which is easy to confuse logic, difficult to read, difficult to debug and high fault tolerance. Therefore, it is better and more convenient. Why not use it.

Supplement 2:

Among the above algorithms, the time complexity O(n) and the space complexity O(1)

Supplement 3:

Allocate memory with C language

SqList L;

L.elem=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE);

malloc() allocates memory and {free() frees memory

Allocate and release memory with C + +

Allocation: int *L = new int; Or int *L = new int(10)

Release: delete L;