Linked list of data structure foundation (1)

Linear table is a very common data structure, which is divided into sequential table and linked list. The sequence table can be simply understood as the concept of "array" introduced earlier. Next, we will explain the linked list.
When an array is defined in the normal way, the computer will take a continuous address from memory to store an array of a given length; the linked list is composed of several nodes (each node represents an element) The storage locations in memory are usually discontinuous. In addition, the two nodes of the linked list generally use a pointer to point from one node to another. Therefore, the nodes of the linked list are generally composed of two parts, namely the data field and the pointer field:

struct node{
	typename data ;//数据域
	node* next; / /指针域
}
//补充一点小知识:很多同学老说这个“*”号到底放在哪呢,
//其实放哪都行,放在typename后面挨着也行,放在变量名前面挨着也行。

The data field stores the data to be stored by the node, and the pointer field points to the address of the next node, which will produce a chain structure starting from a node and linked by the pointer, namely a linked list. Depending on whether the linked list has a head node, the linked list can be divided into a linked list with a leading node and a linked list without a leading node. The head node is generally called head, and its data field data does not store any content, and the pointer field next points to the node with content in the first data field (generally called it the first node). In most cases, the head node is used.
Insert picture description here
Insert picture description here

Use malloc function or new operator to allocate memory space for linked list nodes

Above, we already know how to define the node type of the linked list. Next, we will temporarily allocate the corresponding size of memory space to the new node every time we need to use the new node. The C language uses the malloc function, and C ++ uses the new operator.

malloc function

The malloc function is a function used to apply for dynamic memory under the stdlib.h header file in the C language, and its return type is a pointer of the same variable type. The basic usage is as follows:

typename* p = (typename*)malloc(sizeof(typename));
//以下是申请一个int型变量和一个node型结构体变量为例:
int *p = (int*)malloc(sizeof(int));
node *p = (node*)malloc(sizeof(node));

The logic of this writing is: take the size of the memory space to be applied (ie sizeof (int)) as the parameter of the malloc function, so that the malloc function will apply to the memory for a sizeof (int) space, and return to point to this space Pointer. But this pointer is an undefined pointer void * at this time, so it needs to be cast to an int pointer, so (node ) is added before malloc . In this way, an int pointer is obtained on the right side of the equal sign , and this pointer is assigned to the node type pointer variable p by assigning the equal sign, and a node type memory space is successfully applied, that is, a node type structure variable, And access it through the pointer p, if the application fails, it will return a null pointer NULL.
Generally speaking, just applying for a linked list node will not fail. Failure usually occurs when using malloc to apply for a larger dynamic array. In
Insert picture description here
this case, malloc will return a null pointer NULL and assign it to p.
Insert picture description here

Memory leak problem

Memory leak means that the memory space created by using malloc and new is not released after being used, resulting in it always occupying the memory space before the end of the program. This is very easy to cause the memory consumption in some large programs to be too fast so that there is no memory Assignable. After using the space created by malloc and new, it must be released.
(1) The free function The
free function corresponds to the malloc function, also under stdlib.h. Just fill in the pointer variable (assuming p) of the memory space to be released in the free parameter:

free(p);

The free function mainly achieves two effects: release the memory space pointed to by the pointer variable p: point the pointer variable p to the empty address NULL. It can be seen that after the execution of the free function, the pointer variable p itself has not disappeared, just let him point to the empty address NULL, but the memory he originally pointed to was indeed released.
Insert picture description here

Published 19 original articles · praised 2 · visits 735

Guess you like

Origin blog.csdn.net/zan1763921822/article/details/105599239