Data structure (i)--Introductory and preparatory knowledge 1. Overview
Data structure Definition:
How do we store large and complex problems in reality in the main memory (memory) with specific data types and specific storage structures?
And on this basis for the implementation of a function (such as the curd of elements, sorting, etc.) and the corresponding operation, this corresponding operation is called the algorithm.
Data structure = storage algorithm for the relationship of the element's Store + element = operation on the datastore
Algorithm:
Algorithm is: The method and steps to solve the problem
The measurement algorithm has the following criteria:
- Time complexity (number of times the program executes, not execution time)
- Spatial complexity (the maximum memory to be used during algorithm execution)
- Difficulty level (readability)
- Robustness
2. Characteristics and status of data structure
Status:
The data structure is in the core of the software.
such as computer memory stack and heap differences, people who do not understand the data structure may think that memory is divided into two parts, called Stacks, a piece called heap, obviously this is very superficial and incorrect conclusion.
In fact, if a piece of memory is compressed stack out of the way the memory allocated, then this block of memory is called stack memory, if it is a heap sort of allocation of memory, then this block of memory is called heap memory, the most fundamental difference is its memory allocation algorithm is different.
For example, the invocation of a function is also 压栈出栈
called by the way, or the operating system in the multi-threaded operation 队列
of the concept, 队列
to ensure the sequence of multi-threaded operations,
This is also the contents of the data structure, or the principle of computer compilation inside 语法树
The concept, which is actually the data structure inside 树
,
such as software engineering, databases and the like have data structure of the shadow.
Characteristics:
Data structure cultivation is the internal strength, and can not be directly immediate to solve the real problem, but with this internal strength will be in other aspects of learning for you.
3. Preparatory knowledge (C language)
The learning data structure should have the following knowledge:
- Pointer
- Structural body
- Allocation and release of dynamic memory
- Using memory across functions
This section mainly introduces the basis of the learning data structure, and provides a little explanation of the relevant knowledge.
Pointer
The pointer is the soul of C, and the importance does not need to be spoken.
Pointer definition
Address:
Address is the number of the memory unit
The number is a non-negative integer starting from 0
Range: 0--0xFFFFFFFF (2^32-1) This refers to the x86 platform, the maximum memory address of the x64 platform is (2^64-1)
Pointer:
The pointer is the address, and the address is the pointer.
A pointer variable is a variable that holds the address of a memory cell, and the value stored internally is the corresponding address, which is the number of the memory unit (such as the memory address value: 0XFFC0).
The essence of a pointer is a non-negative integer with restricted operation
In the computer system, the CPU can directly manipulate the memory, about the CPU memory operation and control principle can be easily understood as
Address line: Determines which address unit to operate
Control line: Controls the Read and write properties of the data unit
Data lines: Transferring data between CPU and memory
Classification of pointers
- Basic types of pointers
int i = 10; // 定义一个 整形变量 i 初始值 10
int *p = i; // 定义一个 整形的指针变量 p , 变量 p 指向 i 的地址 int *p; // 这两行等价于上面一行p = &i;`1. p 存放了 i 的地址,我们就可以说“ p 指向了 i” ,但 p 和 i 是两个不同的变量,修改一方不会影响另一个的值。2. *p 等价于 i ,i 等价于 *p;两者是一块内存空间,里面的值一变具变。`
Pointers and functions
// 修改外部实参的值
void func (int * p)
{
*p = 100; Modifying the value of an external variable within a function
}
Modify the value of an external argument, the value of a level two pointer
void Func2 (int * * p)
{
*p = 100;
function to modify the value of the external variable, where the actual change is the internal address of the pointer, where the direct modification is not rigorous and unsafe, just to express the meaning
}
int main (void)
{
Modify an external argument
int i = 10;
Func (&i);
printf ("I =%d", i);
// 修改外部二级指针int *p = i; // 等价于 int *p; p = &i;func(&p);printf("i = %d",i);return 0;
}
Changing the value of an external variable (argument) of a function through a function call
`
Pointers and Arrays
' Pointers ' and ' one-dimensional arrays '
int a[5] = {1,2,3,4,5 }; a[3] == *(a + 3) // 等价于 a[3] == *(3 + a) == 3[a]; // 3[a] 这种写法只是不常用,从原理上来说是正确的 a 等价于 a[0]; // a 是数组中第一个元素,每个元素占用内存单位长度相同, // a[i] 中的 i 实际代表的是单位长度的倍数`- 数组名:<br> 一维数组名是个指针常量(它的值不可变) <br> 它存放的是该一维数组的第一个元素的地址(一维数组名指向其第一个元素) - 下标和指针的关系: (1)、 `a[i]` 等价于 `*(a + i)` (2)、假设指针变量的名字为 p, 则 `p + i` 的值为 `p + i * (p 所指向的变量所占字节数)` (3)、每个下标表示的是第 i+1 个元素,根据元素类型分配的字节长度不同(int 类型4个字节),每个字节都有对应的内存地址编号,指针变量 p 保存的是该元素首字节的地址。- 指针变量的运算: 指针变量不能相加、相乘、相除 如果两指针变量属于同一数组,则可以相减 指针变量可以加减一个整数,前提是最终结果不能超过指针最大可访问范围 // 指针变量的运算p + i 的值是 p + i*(所指向的变量所占字节数)p - i 的值是 p - i*(所指向的变量所占字节数)p++ 等价于 p + 1p-- 等价于 p - 1// 下面是一个通过函数修改数组内部元素void my_Array(int *a , int length){ for(int i = 0; i < length; i++) { *a[i]++; // 给每个元素加 1 }}int main(void){ int a[5] = {1,2,3,4,5}; my_Array(a , 5); // 调用}
`
Structural body
Why are there structures ,
In order to represent some complex data, common basic data cannot meet the requirements.
What do you mean, structural body
A struct is a composite data type that is defined by the user according to its actual needs.
// 如学生类型struct Student{ int age; char * name; // name 不同,赋值方法不同 char name2[100]; // 这个只能 strcpy(s.name2, "zhangwangdsd"); 字符串拷贝 double height;};
How to use the structure body
总结起来有两种结构体的使用方式:直接使用 && 通过指针使用struct Student ss = {12,"xiaoyou",1.73,"xiaozhang"};struct Student *pst = &ss;ss.name ; 这里直接操作结构体本身pst -> name ; 这里通过指针地址操作,更加节省空间struct Student{ // 自定义结构体int age;char * name;double height;char name2[100];};int main(void) {struct Student s = {12,"xiaoyou",1.73,"xiaozhang"};// 直接使用printf(" age = %d \n name = %s \n height = %.2f \n",s.age,s.name,s.height);s.age = 21;s.name = "xiaozhu";strcpy(s.name2, "zhangwangdsd"); // 字符串拷贝s.height = 1.70;printf(" age = %d \n name = %s \n height = %.2f \n %s \n",s.age,s.name,s.height,s.name2);// 以指针的方式使用struct Student *pst = &ss;pst -> name = "my new name";printf(" name = %s\n",pst->name);printf(" name = %s\n",(*pst).name);// pst -> name 等价于 (*pst).name ,// 而(*pst).name 又等价于 ss.name// 所以 pst -> name 等价于 ss.namereturn 0;}
Precautions
The type of struct variable is: struct Student
struct variables cannot be subtraction, but can be assigned to each other
Common structure variables and structure pointer variables as function-parameter-transfer problems
typedef struct Student{ // 结构体定义int age;char * name;char name2[100];double height;}myStudent;// 直接传递整个结构体数据,耗时 && 浪费内存空间void func(struct Student st);// 直接传递 只占用 4 byte 的指针,省时效率也高 <推荐用法>void func2(struct Student * pst);int main(void){myStudent ss = {12,"xiaoyou",1.73};func(ss);func2(&ss);return 0;}void func(struct Student st){printf("age = %d \n name = %s",st.age,st.name);}void func2(struct Student * pst){printf("age = %d \n name = %s",(*pst).age,(*pst).name);printf("age = %d \n name = %s",pst->age,pst->name);}
### 动态内存分配和释放平时直接创建数组的写法都是静态创建,创建完毕之后在整个程序的运行过程中,会固定占用对应的内存,不仅会造成内存空间浪费,还无法动态添加元素,所以局限性很大,而程序中我们为了避免这种情况,应该使用动态的方式创建和销毁数组。// 静态创建数组int a[5] = {1,2,3,4,5};
Dynamically constructing one-dimensional arrays
Dynamically constructs a one int 型
-dimensional array.
int *p = (int *) malloc (int length); 1. void * malloc (size_t __size) function, with only one formal parameter of type int representing the number of bytes required to allocate the System 2. The work of the malloc function Can be a memory space that requests the system's length bytes, if the request completes, returns the address of the first byte, and returns NULL3 if the request is unsuccessful. The malloc function can and can only return the address of the first byte, so we need to convert the meaningless first byte address (dry address) to a meaningful address, so malloc must precede (data type *) to indicate that the meaningless address is converted to the corresponding type of address instance: int *p = (int *) malloc (50); Indicates that the address of the first byte of the 50 bytes allocated by the system is converted to an address of type int, which is exactly the first address of the address converted to a group of 4, so that P points to the first four bytes ... P+i points to the i+1 four bytes, P[0],p[i] is the first, the second i+1 element. Double *p = (double *) malloc (80); Indicates that the address of the first byte of the 80 bytes allocated by the system is converted to an address of type double, exactly the first address of an address converted to 8 groups, so that P points to the first eight bytes ... P+i points to the i+1 eight bytes, P[0],p[i] is the first, the second i+1 element. 4. Free (p); Releases the memory that P points to, instead of releasing the memory code occupied by P itself as follows: void Test2 (void) {int len;printf ("Enter the length of the array you want to create dynamically:"); scanf ("%d", &len) ; int *parr = (int *) malloc (len); Dynamically created array *parr = 4; Equivalent to a[0] = 4; Here PARR is equal to the first address of the array, equal to the array name parr[2] = 5; Equivalent to a[2] = 5;printf ("parr[0] =%d \nparr[2] =%d\n", parr[0],parr[2]); free (PARR); After use, release the corresponding array space}
Using memory across functions
Local variables that are allocated inside the function are reclaimed by the system after the function call is completed and their memory disappears. However, it is often necessary to define a piece of memory in a program
We'll recycle it when we're done with it. Objects such as the OC language. So you need to save the allocated memory, you should use the dynamic allocation of memory, when used up and then manually released.
This is also a disadvantage of the C language: memory needs to be created manually and manually released, which is also the OC language in the development of IOS programs, we speak of MRC.
"Apple also found this deficiency, when the IOS 5 launched the arc"
Here is an example of using memory across functions:
// 这个例子已经非常有面向对象的味道了typedef struct Student{ // 自定义 student 结构体int age;char * name;}myStudent;myStudent * createStudent(void); // 创建 student void showStudent(myStudent *); // 输出 studentint main(void) {myStudent *p = createStudent(); // 创建 student showStudent(p); // 输出 studentreturn 0;}myStudent * createStudent(void){myStudent *p = (myStudent *)malloc(sizeof(myStudent));p->age = 20;p->name = "xiaoyou";return p;}void showStudent(myStudent *p){printf("student.age = %d \nstudent.name = %s\n",p->age,p->name);}
4. Summary
This paper mainly explains the definition and introduction of data structure.
This paper reviews the basic knowledge of C language, such as pointers, structures, and memory, which should be possessed by learning data structure.
The data structure will continue to be explained at a later start.
Record data structure--Getting started and ready knowledge