kernel-mini-basic-list.md 5.0 KB
Newer Older
A
Annie_wang 已提交
1
# Doubly Linked List
D
duangavin123 已提交
2

D
duangavin123 已提交
3

A
Annie_wang 已提交
4
## Basic Concepts
D
duangavin123 已提交
5

A
Annie_wang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
A doubly linked list (DLL) is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains a pointer to the previous node and a pointer to the next node in the sequence of nodes. The pointer head is unique.

A DLL allows access from a list node to its next node and also the previous node on the list. This data structure facilitates data search, especially traversal of a large amount of data.  The symmetry of the DLL also makes operations, such as insertion and deletion, easy. However, pay attention to the pointer direction when performing operations.


## **Function Description**

The table below describes APIs available for the DLL. For more details about the APIs, see the API reference.

| **Category**                | **Description**                                                |
| ------------------------ | ------------------------------------------------------------ |
| Initializing and deleting a DLL        | **LOS_ListInit**: initializes a DLL node as a DLL.<br>**LOS_DL_LIST_HEAD**: Defines a DLL node and initializes the node as a DLL.<br>**LOS_ListDelInit**: deletes a DLL.|
| Adding a node                | **LOS_ListAdd**: adds a node to the head of a DLL.<br>**LOS_ListTailInsert**: inserts a node to the tail of a DLL.|
| Deleting a node                | **LOS_ListDelete**: deletes a node from this DLL.<br>**LOS_ListDelInit**: deletes a node from this DLL and uses this node to initialize the DLL.|
| Checking a DLL    | **LOS_ListEmpty**: checks whether a DLL is empty.                           |
| Obtaining structure information          | **LOS_DL_LIST_ENTRY**: obtains the address of the structure that contains the DLL. The first input parameter of the API indicates a node in the list, the second input parameter indicates the name of the structure to be obtained, and the third input parameter indicates the name of the DLL in the structure.<br>**LOS_OFF_SET_OF**: obtains the offset of a member in the specified structure relative to the start address of the structure.|
| Traversing a DLL            | **LOS_DL_LIST_FOR_EACH**: traverses a DLL.<br>**LOS_DL_LIST_FOR_EACH_SAFE**: traverses the DLL and stores the subsequent nodes of the current node for security verification.|
| Traversing the structure that contains a DLL| - **LOS_DL_LIST_FOR_EACH_ENTRY**: traverses a DLL and obtains the address of the structure that contains the linked list node.<br>**LOS_DL_LIST_FOR_EACH_ENTRY_SAFE**: traverses a DLL, obtains the address of the structure that contains the linked list node, and stores the address of the structure that contains the subsequent node of the current node.|

D
duangavin123 已提交
25

A
Annie_wang 已提交
26
## How to Develop
D
duangavin123 已提交
27

A
Annie_wang 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40
The typical development process of the DLL is as follows:

1. Call **LOS_ListInit** or **LOS_DL_LIST_HEAD** to initialize a DLL.

2. Call **LOS_ListAdd** to add a node into the DLL.

3. Call **LOS_ListTailInsert** to insert a node into the tail of the DLL.

4. Call **LOS_ListDelete** to delete the specified node.

5. Call **LOS_ListEmpty** to check whether the DLL is empty.

6. Call **LOS_ListDelInit** to delete the specified node and initialize the DLL based on the node.
D
duangavin123 已提交
41 42


A
Annie_wang 已提交
43 44 45 46 47 48 49
> **NOTE**
> 
> - Pay attention to the operations operations of the front and back pointer of the node.
> 
> - The DLL APIs are underlying interfaces and do not check whether the input parameters are empty. You must ensure that the input parameters are valid.
> 
> - If the memory of a linked list node is dynamically allocated, release the memory when deleting the node.
D
duangavin123 已提交
50 51


A
Annie_wang 已提交
52 53 54 55
## Development Example


### Example Description
D
duangavin123 已提交
56 57 58

This example implements the following:

A
Annie_wang 已提交
59 60 61 62 63 64 65 66
1. Initialize the DLL.

2. Add nodes.

3. Delete nodes.

4. Check the operation result.

D
duangavin123 已提交
67

A
Annie_wang 已提交
68
### Sample Code
D
duangavin123 已提交
69 70 71

The sample code is as follows:

A
Annie_wang 已提交
72 73 74
The sample code is compiled and verified in **./kernel/liteos_m/testsuites/src/osTest.c**. Call **ExampleList** in **TestTaskEntry**.


D
duangavin123 已提交
75 76 77 78
```
#include "stdio.h"
#include "los_list.h"

A
Annie_wang 已提交
79
STATIC UINT32 ExampleList(VOID)
D
duangavin123 已提交
80 81 82 83 84
{
    LOS_DL_LIST listHead = {NULL,NULL};
    LOS_DL_LIST listNode1 = {NULL,NULL};
    LOS_DL_LIST listNode2 = {NULL,NULL};

A
Annie_wang 已提交
85
    /* Initialize a DLL. */
D
duangavin123 已提交
86 87 88
    printf("Initial head\n");
    LOS_ListInit(&listHead);

A
Annie_wang 已提交
89
    /* Add node 1 and node 2 and verify their relationship. */
D
duangavin123 已提交
90 91 92 93 94 95 96 97 98 99
    LOS_ListAdd(&listHead, &listNode1);
    if (listNode1.pstNext == &listHead && listNode1.pstPrev == &listHead) {
        printf("Add listNode1 success\n");
    }

    LOS_ListTailInsert(&listHead, &listNode2);
    if (listNode2.pstNext == &listHead && listNode2.pstPrev == &listNode1) {
        printf("Tail insert listNode2 success\n");
    }

A
Annie_wang 已提交
100
    /* Delete the two nodes. */
D
duangavin123 已提交
101 102 103
    LOS_ListDelete(&listNode1);
    LOS_ListDelete(&listNode2);

A
Annie_wang 已提交
104
    /* Check whether the DLL is empty. */
D
duangavin123 已提交
105 106 107 108 109 110 111 112
    if (LOS_ListEmpty(&listHead)) {
        printf("Delete success\n");
    }

    return LOS_OK;
}
```

A
Annie_wang 已提交
113

A
Annie_wang 已提交
114
### Verification
D
duangavin123 已提交
115 116 117

The development is successful if the return result is as follows:

A
Annie_wang 已提交
118

D
duangavin123 已提交
119 120 121 122 123 124
```
Initial head 
Add listNode1 success 
Tail insert listNode2 success
Delete success 
```