Static and Dynamic Linked lists

Source: Internet
Author: User

Static and Dynamic Linked lists
1. Static linked list

The member in the struct can be pointer variables of various types. When the base type of one or more members in the struct is the struct type, this struct is called "reference its own struct ". For example:

Struct link

{

Char ch;

Struct link * p;

};

P is a pointer member that can point to a struct link type variable. Therefore, a. p = & a is a valid expression, as shown in Storage Structure 1.

Figure 1 reference a struct

Example 1: A simple linked list

1 # include <stdio. h> 2 3 struct node 4 {5 int data; 6 struct node * next; 7}; 8 typedef struct node NODETYPE; 9 10 int main () 11 {12 // a is the header node, B is the intermediate node, and c is the end node 13 // h is the pointer of the base type NODETYPE, pointing to the header node 14 // p is a pointer to the base type of NODETYPE, used for the traversal Link Table 15 NODETYPE a, B, c, * h, * p; 16 17 // assign 18 a to the data in the variable. data = 10; 19 B. data = 20; 20 c. data = 30; 21 22 // connect the node to 23 h = & a; 24. next = & B; 25 B. next = & c; 26 c. next = '\ 0'; 27 28 // move p to point to a, B, and c in turn, and output 29 p = h in data; 30 while (p) 31 {32 printf ("% d \ t", p-> data); 33 p = p-> next; // p order backward shift 34} 35 printf ("\ n"); 36 return 0; 37}STRUCT_LIST

The struct type NODETYPE defined in the above program has two members: the member data is an integer type, the member next is a pointer type, and its base type is NODETYPE type.

A, B, and c are NODETYPE struct type variables, and h and p are pointer variables pointing to the NODETYPE struct type. After the program is executed, the storage structure shown in 2 is formed: the address of variable a is stored in Pointer h, and the address of variable B is stored in variable a. next ......, C. next, the member of the last variable c, is set to '\ 0' (NULL ). In this way, the same type of struct variables a, B, and c are linked together to form a so-called "Linked List". variables a, B, and c are called linked list nodes.

In this example, each node linked together (struct variables a, B, and c) is defined, and the system opens a fixed, not necessarily continuous storage unit in the memory. During program execution, it is impossible to generate new storage units, nor to think that the opened storage units will disappear. This type of linked list becomes a "static linked list ".

Figure 2 linked list Storage Structure

2. Concept of Dynamic Linked List

So far, we have used arrays to store "batch" data. Defining an array must (explicitly or implicitly) specify the number of elements, thus limiting the amount of data stored in an array. In actual applications, the number of data to be processed by a program at each run is usually unknown. If the definition of an array is small, there is not enough space to store the data, which wastes storage space.

In this case, the storage space can be reasonably used if the storage space can be opened at any time as needed during the program execution process and is released at any time when no need is required. Dynamic Storage Allocation in C language provides this possibility. The address of each dynamically allocated storage unit is not necessarily continuous, but the batch data to be processed is often a whole, and there is a sequential relationship between each data. Each node in the linked list needs at least a pointer field to store the address of the next node element in addition to the data stored in the data itself, to connect nodes through these pointers (3 ). Since each storage unit in the linked list is obtained by dynamic storage allocation, such a linked list is called "Dynamic Linked List ".

It should be emphasized that in a dynamic linked list, each node does not have its own name and can only maintain the sequential relationship between nodes by pointer. Once the pointer of a node is "disconnected", the node will no longer be available.

Figure 3 one-way linked list with header nodes

Each linked list uses a "head Pointer" variable to point to the beginning of the linked list, and the head in 3. That is to say, the address of the first node of the linked list is stored in the head. In this linked list, we set a "header node". The data field of this node does not store data (you can also set a header node as needed ). The pointer field of the last node of the linked list does not store the address. It is set to '\ 0' (NULL), marking the end of the linked list. Each node of the linked list has only one pointer field, and each pointer field stores the address of the next node. Therefore, this kind of linked list can only find successor nodes from the current node, so it is called "One-way linked list ".

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.