Lin table C language implementation (including dynamic memory allocation)

xiaoxiao2021-03-06  117

Lin table C language implementation (including dynamic memory allocation)

Dynamic memory allocation of C language implementation of the upper list

First, why use dynamic memory allocation but when we don't learn the list, if you want to store the number of similar types or data of the same structure, always use an array. For example, we have to store a score of a class student, always define a FLOAT type (there is 0.5 points) arrays:

Float score [30];

However, when using arrays, there is always a problem that plasmides us: How big should arrays?

In many cases, you can't determine how much arrays to use, such as in the previous example, you may not know the number of students of the class, then you have to define a large group enough. In this way, your program applies for a fixed size of memory space when it is running. Even if you know the number of students in the class, if there is an increase or decrease in a certain number of special reasons, you must re-modify the program to expand the storage range of the array. This memory allocation method for assigning fixed sizes is called static memory allocation. However, this memory allocation has a relatively serious defect, especially when handling certain problems: In most cases, a large amount of memory space is wasted, in a few cases, when your defined array is not large enough, it may cause The cycosity is wrong, and even serious consequences. So do you have any other way to solve such an outer body? Yes, that is, dynamic memory allocation. The so-called dynamic memory allocation refers to a method of dynamically assigning or recovering a distribution memory in the process of execution. Dynamic memory allocation is a static memory allocation method such as an array. It is necessary to pre-allocate storage, but by the system according to the need to instantly allocate, and the size of the assignment is the size of the program requirements. From the above, static memory allocation can be known that the dynamic memory allocation is characterized by the dynamic memory allocation: 1, there is no need to allocate storage space in advance; 2, the assigned space can be expanded or reduced according to the needs of the program. Second, how to achieve dynamic memory allocation and its management To implement the data according to the needs of the program, the following functions must be used to use the following functions 1, the prototype of the malloc function Malloc function is:

Void * malloc (unsigned int size)

Its role is to allocate a continuous space of Size in the dynamic storage area of ​​the memory. Its parameters are an unsigned shaping, and the return value is a pointer to the start address of the assigned continuous storage domain. Another point to note that a NULL pointer will be returned when the function fails to successfully assign a storage space (such as memory). Therefore, when calling the function, it should detect whether the return value is null and performs the corresponding operation.

The following example is a dynamically allocated program:

#include "malloc.h"

#include "stdlib.h"

Main (void)

{

/ * Count is a counter, Array is an integer pointer, or it is also understood to point to the first address of an integer array * /

INT country;

Int * array;

Array = malloc (10 * sizeof (int));

IF (array == null)

{

Printf ("Out of Memory! \ n");

Exit (1);

}

/ * Assignment to array * /

For (count = 0; count <10; count )

{

Array [count] = count;

}

/ * Print array element * /

For (count = 0; count <10; count )

{

Printf ("% 2D \ n", array [count]);

}

}

10 integer storage areas are dynamically allocated in the above example, and then assign and print. In the case of IF ((int *) malloc (10 * sizeof (int)) == NULL) statement can be divided into the following steps: 1) Assign 10 integer continuous storage and return a pointing Entrepreneur 2) Assigning this integer address to array 3) Detection whether the return value is NULL 2, the Free function is always limited due to the memory area, and cannot be distributed unlimited, and a program will save as much as possible Resources, so when the allocated memory area is not used, it is to release it so that other variables or programs are used. At this time we have to use the Free function. Its function prototype is: void free (void * p)

The role is to release the memory area pointed to by the pointer P. The parameter P must be the pointer returned when the Malloc function or a calloc function (another dynamically assigned storage area is function). Delivering other values ​​to free functions It is likely to cause crash or other catastrophic consequences. Note: The value of the pointer is important here, not the pointer used to apply for dynamic memory itself. example:

INT * P1, * P2; P1 = malloc (10 * sizeof (int)); p2 = p1; ... free (p2) / * or free (p2) * /

Malloc returns a value to P1 and assigns the value of P1 to P2, so P1, P2 can be used as the parameters of the free function. The Malloc function is assigned to the storage area. The Free function is to release the memory area that is no longer. So the two functions can be dynamically allocated and simple to manage the memory area.

Single-link table implementation of the C language implementation of the middle list

First, the establishment of single-strand table has the basis of dynamic memory allocation, and it is not difficult to implement the linked list. The so-called linked list is a data structure of a set of any set of storage units. The linked list is divided into single-link tables, two-way linked lists, and loop lay lists. Let's talk about a single-link table. The so-called single-link table is that the data contact is arranged in one direction. A single-stranded table node, its structural type is divided into two parts: 1, data field: to store itself data 2, chain domain, or pointer domain: to store the next node address or point to its direct successive pointer . example:

Typedef struct node

{

Char Name [20];

Struct Node * LINK;

STUD;

This defines the structure of a single-link table, where char name [20] is a character type array used to store the name, pointer * link is a pointer to store its direct successive. After defining the structure of the linked list, as long as the appropriate data is stored in the data field when the program is running, if there is a subsequent node, the chain domain points to its direct successor, if not, set to NULL. Let's take a full program that establishes a single-stranded list with a header (without explanation, the following finger shown below.

#include

#include

#include / * The header file containing the dynamic memory allocation function * /

#define n 10 / * n is the number of people * /

Typedef struct node

{

Char Name [20];

Struct Node * LINK;

STUD;

STUD * CREAT (INT N) / * Create a function of single-strand table, the meticulum N is the number of people * /

{

Stud * p, * h, * s; / * * h save the pointer of the header node, * p Points the previous node of the current node, * S point to the current node * /

INT i; / * counter * /

IF ((h = (stud *) Malloc (SIZEOF (STUD))) == NULL) / * Assign space and detects * / {

Printf ("cannot allocate memory space!");

exit (0);

}

H-> Name [0] = '\ 0'; / * Put the data domain of the header node * /

H-> link = null; / * Put the chain domain of the head head * /

P = h; / * p pointing to the header node * /

For (i = 0; i

{

IF ((s = (stud *) Malloc (SIZEOF (STUD))) == NULL) / * Assign new storage space and detect * /

{

Printf ("cannot allocate memory space!");

exit (0);

}

P-> Link = S; / * assigns the address of the S to the nodes pointed to the chain domain, which connects the nodes pointed to by P and S. * /

Printf ("Please enter the name of the first% D personal name", i 1);

Scanf ("% s", S-> Name); / * Store your name * /

S-> Link = NULL;

P = S;

}

Return (H);

}

Main ()

{

INT Number; / * Variables for saving people * /

Stud * head; / * head is a pointer to the header address of the single-chain table * /

Number = n;

HEAD = CREAT (NUMBER); / * Associate the new single-link table header address to Head * /

}

This will write a single-link table that can build N personal names. Write a dynamic memory allocation should pay attention to, please try to detect if the allocation is successful.

Cyclic list and two-way linked list of C language implementation of the next linked list

First, the circulating chain list is the same as the single-strand table, is a chain storage structure, which is different, the pointer to the last node of the loop chain table is the first node or table head knotted to the loop chain. Point, thereby constituting an annular chain. The calculation of the circulatory chain table is basically the same as the operation of the single-strand list. There are several points below: 1. When establishing a loop chain list, the pointer of its last node must point to the header node, not the NULL like a single-link table. This situation is also used to insert a new node after the last node. 2. When it is determined whether or not to the end of the tail, it is determined whether the value of the node chain domain is a header node. When the chain domain value is equal to the head pointer, the description has arrived at the end. The chain domain value is NULL as the single-link table. Second, the bidirectional linked list is actually a single-link form. When we operate on a single-link table, sometimes you have to operate the direct pre-drive of a node, and you must start from the header. This is limited by the structure of a single-link table node. Since only one chain domain stores direct successive node addresses each node, you can define a chain domain that stores direct successive node addresses, and a chain domain that stores directly-to-the-point address. Double-strand domain node structure? This is a two-way linked list. In the two-way linked list, in addition to the data domain, there are two chain domains, one stored direct successive node address, generally called the right chain domain; a storage direct forward discharge point address, generally called the left chain domain . The two-way linked list node type in the C language can be defined as:

Typedef struct node

{

INT data; / * Data Domain * /

Struct node * llink, * rlink; / * chain domain, * llink is the left chain domain pointer, * RLINK is the right chain point pointer * /

} JD;

Of course, a two-way linked list can be constructed into a two-way circular list. The two-way linked list is the same as one-way linked list: find, insert, and delete. The basic operation of the two-way linked list: 1. When we want to find a certain node of a specific value in a two-way circular loop table with a header, we also compare each node from the head head node. The value of the data field is, if it is the particular value, return to the pointer to the node, otherwise continue to check until the end. The following example is a program for applying the two-way loop chain list. #include

#include

#define n 10

Typedef struct node

{

Char Name [20];

Struct Node * LLINK, * RLINK;

STUD;

Stud * Creat (INT N)

{

Stud * p, * h, * s;

INT I;

IF ((h = (stud *) malloc (sizeof)) == NULL)

{

Printf ("cannot allocate memory space!");

exit (0);

}

H-> Name [0] = '\ 0';

H-> llink = NULL;

H-> rlink = NULL;

P = H;

For (i = 0; i

{

IF ((s = (stud *) Malloc (SIZEOF (STUD))) == NULL)

{

Printf ("cannot allocate memory space!");

exit (0);

}

P-> rlink = s;

Printf ("Please enter the name of the first% D personal name", i 1);

Scanf ("% s", S-> Name);

S-> llink = P;

S-> rlink = NULL;

P = S;

}

H-> llink = s;

P-> rlink = H;

Return (H);

}

Stud * Search (stud * h, char * x)

{

STUD * P;

CHAR * Y;

P = H-> rlink;

While (p! = h)

{

Y = p-> name;

IF (strcmp (y, x) == 0)

Return (P);

ELSE P = P-> rlink;

}

Printf ("Not finding this data!");

}

Void Print (stud * h)

{

Int n;

STUD * P;

P = H-> rlink;

Printf ("data information is: \ n");

While (p! = h)

{

Printf ("% s", & * (p-> Name));

P = P-> rlink;

}

Printf ("\ n");

}

Main ()

{

Int number;

Char studname [20];

Stud * Head, * searchPoint;

Number = n;

CLRSCR ();

HEAD = CREAT (NUMBER);

Print (HEAD);

Printf ("Please enter the name of the person you want to find:");

Scanf ("% s", studname);

SearchPoint = Search (Head, StudName);

Printf ("The name of the person you want to find is:% s", * & searchPoint-> name);

转载请注明原文地址:https://www.9cbs.com/read-102669.html

New Post(0)