I. data structure
How to store data
Well chosen data structures can improve efficiency
Data structure 1. Logical structure
One to many relationship between father and son
One to one relationship queuing
Many to many relationship route between two places
2. Storage structure
Location relationship of data storage
To store (an array) of data one by one
Chain storage
II. Linear table
In terms of logic, each element has its unique precursor and successor
The linear table of sequential storage is the sequential table
A linear list stored in chain is a linked list
III. sequence table
Main implementation mode: array / dynamic array
Sequentially stored linetype table
Data structure - > to manage data
Operate on data --- add, delete, find and modify
Attachment:
Static array (fixed size defined)
Cannot store data when array is full
Dynamic array
Array full -- > allocate new memory (request more space to store data)
Using malloc to request memory
The test code notes are as follows:
1 #include<stdio.h> 2 struct LIST //Define structure 3 { 4 int arr[20]; //Array of data 5 int size; //How many elements can be stored in the array at most 6 int len; //How many elements have been stored in the array 7 }; 8 9 //Initialization function 10 void main(struct LIST*p) 11 { 12 //No data in the table at the beginning 13 p->len = 0; 14 p->size = 20; //Data that can be saved at most 15 } 16 17 //Add data 18 void insert(struct LIST*p,int data) 19 { 20 //Insert end directly 21 if (p->len < p->size) //Description sequence table is not full 22 { 23 p->arr[p->len] = data; //Judging according to the subscript of array len What we know is the next position of the last element 24 p->len++; 25 //Two lines are equivalent to p->arr[p->len++]=data; 26 } 27 } 28 29 //Insert in middle insert in front 30 void instert_middle(struct LIST*p, int data, int index) //index Is the subscript inserted to the subscript location 31 { 32 if (p->len < p->size) //Description sequence table is not full 33 { 34 if (index >= 0 && index < p->len) 35 { 36 //You can insert it in this position first 37 //Make a place first 38 for (int i = p->len; i>index; --i) 39 { 40 p->arr[i] = p->arr[i - 1]; //Element moves back 41 } 42 p->arr[index] = data; //Insert in this position 43 p->len++; //An element is inserted, so let it add 1 44 } 45 else 46 { 47 p->arr[p->len++] = data; //Otherwise insert the tail 48 } 49 } 50 } 51 52 //Find one by one 53 int search(struct LIST*p, int data) 54 { 55 for (int i = 0; i < p->len; ++i) 56 { 57 if (data == p->arr[i]) 58 { 59 return i; //Eureka return subscript 60 } 61 } 62 return -1; //This location does not exist 63 } 64 65 //Modifying data 66 void change(struct LIST*p,int data,int newData) 67 { 68 //Search by student's name 69 for (int i = 0; i < p->len; ++i) 70 { 71 if (data == p->arr[i]); //If you find it, modify it 72 { 73 p->arr[i] = newData; //What is the character array of student name used for comparison strcmp string.h 74 break; //If there is more than one same data, it should be modified and removed break 75 } 76 } 77 } 78 79 //Delete data delete is the overwrite cycle 80 void dele(struct LIST*p, int data) 81 { 82 for (int i = 0; i < p -> len; ++i) 83 { 84 if (data == p->arr[i]) //Find location to start deletion 85 { 86 for (int j = i; j < p->len-1; ++j) //Back to back 87 { 88 p->arr[j] = p->arr[j + 1]; 89 } 90 p->len--; 91 break; 92 } 93 } 94 } 95 96 int main() 97 { 98 #if 0 99 int x, y, z; 100 int k; 101 printf("%p,%p,%p,%p", &x, &y, &z, &k); 102 #endif 103 104 #if 1 105 /* 106 Example: 107 Information student No. + name of the stored students 108 100 students in a class + students in a class + students in a class 109 150--70 110 Character array string 111 char arr[100]="hello world";//Array size array valid elements 11 characters 112 \0 Before valid data, after invalid data. This is only limited to character array 113 In the sequence table 1. How many data are there 2. How many data can be put 114 int brr[100]={1,2,34}; 115 //You can put up to 100 pieces of data in 3 pieces 116 117 Data duplication is not considered by default 118 */ 119 struct LIST list; //Define a sequence table 120 init(&list); //Call function initialization 121 insert(&list, 2); 122 instert_middle(&list, 3, 4); //The inserted data is 3 inserted into the position with subscript 4 123 #endif 124 getchar(); 125 return 0; 126 }
2019-03-30 09:17:07