Data structure sequence table dynamic allocation

Data structure dynamic allocation

 

Realize some basic functions: find data (by value and bitwise), delete data (usually bitwise), insert elements (bitwise), output the length of the table, and print the table.

1>Define the structure

Try to be as readable as possible when defining data

How to use the reference symbol & : If the execution result of the table data needs to be brought back after the function is processed, the reference symbol must be used

2>Initialization table

A dynamic linked list is different from a static linked list. After a static linked list is defined, the system will automatically allocate memory space for it. The function malloc is often used to allocate memory space in a dynamic linked list ( free function is used to release space ). Both have a common header file #include<stdlib.h>

Malloc function analysis: the full name of malloc is memory allocation, which is called dynamic memory allocation in Chinese . It is used to apply for a continuous memory block area of ​​a specified size to return the address of the allocated memory area with void * type. When the specific memory location is not known , think To bind the real memory space , you need to dynamically allocate memory, and the allocated size is the size required by the program. The memory space allocated by the system is: initial linked list data quantity * quantity type L.data=(int *)malloc(sizeof(int)*Initsize)---data type must correspond to the same, malloc is a forced conversion

At the same time, the pointer data points to the first address of the storage address , and moves backward according to the space occupied by the element type.

 

3>Enter some data into the table

4>Expand the capacity of the table

Because malloc opens up a continuous space for the table , when I reallocate the space, I also re-allocate a continuous address to the table. So we need to transfer data to it and increase the capacity of the table after the transfer.

Setting the pointer p is convenient for data migration

 5> Find data (bitwise)

6>Delete element

To delete elements in a continuous space is to move all elements forward after deleting the element position

Can judge whether the deleted location is legal

7>Insert data (bitwise)

For determining the insertion position and then deleting the data from the same main sheet length and sheet capacity and the insertion position of the start determination

8>main() function content

 

Guess you like

Origin blog.csdn.net/qq_46861651/article/details/112731491