Source code analysis of VxWorks-Data Structure Article 1 (two-way linked list)

Source: Internet
Author: User

 

VxWorks uses a variety of basic data structures, such as two-way linked lists, queues, and trees. This article will introduce the implementation of these basic data structures in VxWorks.

1. Two-way linked list

Two-way linked list is the simplest data structure, and its implementation is also very simple. In addition, two-way linked list is often the basis for implementing other data structures, so this article first introduces two-way linked list. The two-way linked list is defined in the dlllib. h file, and the function is implemented in the dlllib. c file.

Before introducing a two-way linked list, it is necessary to introduce the implementation style of the two-way linked list in VxWorks:

 

List is a pointer. the pointer linked list structure contains two fields pointing to the head node and tail node of the linked list respectively:

Typedef struct
{
Dl_node * head;
Dl_node * tail;
} Dl_list;

The head and tail pointers point to the head node and tail node of the linked list respectively. The node structure contains two fields: the first node and the last node. The node structure is defined:

Typedef struct dlnode
{
Struct dlnode * next;
Struct dlnode * Previous;
} Dl_node;

1.1 create a two-way linked list

When creating a linked list, the linked list does not have any subnodes. Therefore, the pointers of the first and last nodes are null at this time. However, the linked list struct needs to be created, so you need to allocate memory for the linked list struct, the code for creating a linked list is as follows:

Dl_list * dllcreate (void)
{
Fast dl_list * plist = (dl_list *) malloc (unsigned) sizeof (dl_list ));

Dllinit (plist );

Return (plist );
}

Status dllinit
(
Fast dl_list * plist/* # define fast register */
)
{
Plist-> head = NULL;
Plist-> tail = NULL;

Return (OK );
}

At this time, the created linked list structure has only one linked list header, but there are no nodes:

1.2 Add a node

Nodes in the bidirectional linked list in VxWorks are added at the end of the linked list and implemented through the dlladd function. The dlladd function is implemented by calling the dllinsert function. The dllinsert function implements the specific addition process; the addition of linked list nodes is mainly completed through pointer migration. During the addition Process, pay attention to the storage of intermediate nodes and pay attention to the pointer migration sequence, the most important thing is to ensure that the linked list cannot be broken. If you say so much, it is better to check the Code:

Void dlladd
(
Dl_list * plist,/* pointer to list descriptor */
Dl_node * pnode/* pointer to node to be added */
)
{
Dllinsert (plist, plist-> tail, pnode );
}

 

Void dllinsert
(
Fast dl_list * plist,/* pointer to list descriptor */
Fast dl_node * pprev,/* pointer to node after which to insert */
Fast dl_node * pnode/* pointer to node to be inserted */
)
{
Fast dl_node * pnext;

If (pprev = NULL)
{/* New node is to be first in list */
Pnext = plist-> head;
Plist-> head = pnode;/* 1. Set the head pointer of the linked list struct */
}
Else
{/* Make Prev node point FWD to New */
Pnext = pprev-> next;
Pprev-> next = pnode;/* 2. Add the node to the end of the added node */

}

If (pnext = NULL)
Plist-> tail = pnode;/* 3. Set the tail pointer of the linked list structure */
Else
Pnext-> previous = pnode;/* 4. Add the node to the front of the node after the node added */

/* Set pointers in new node */

Pnode-> next = pnext;/* 5. Set the back pointer of the node */
Pnode-> previous = pprev;/* 6. Set the forward pointer of the node */
}

You can add a two-way linked list in three cases:

1.2.1 No node in the linked list

When the linked list is just created, it is similar. At this time, the head pointer and tail pointer of the linked list structure point to null. In this case, the node is added to execute statements 1, 3, 5, and 6 in the code, it can be expressed as follows:

1.2.2 The linked list contains one node.

When there is one node in the linked list, both the head pointer and tail pointer of the linked list structure point to the node, but the forward and backward pointers of the node are null, in this case, the Add node mainly executes the 2, 3, 5, and 6 statements in the Code. The specific operation can be demonstrated:

1.2.3 The number of nodes in the linked list must be greater than 1

If the number of nodes in the linked list is greater than one, you can insert nodes between the two nodes. However, the dlladd function cannot be used, this function adds nodes to the last node of the linked list each time. Therefore, the executed statements are still 2, 3, 5, and 6, which is the same as the preceding statement; to insert a node between two nodes, you can only use the dllinsert function. To insert a node in this way, you do not need to change the head pointer and tail pointer of the linked list structure, you only need to change the forward and backward pointers of the node. The executed statements are 2, 4, 5, and 6, which can be used for Demonstration:

 

 

1.2.4 Add a node to the head of the linked list

When there are no nodes in the linked list, adding nodes to the head of the linked list is the same as the first case. Execute the 1, 3, 5, and 6 statements. If there are nodes in the linked list, you need to change the position of the linked list header pointer and execute the following statements:

1.3 Delete A node

It is easier to delete a node than to add a node, because you only need to change the two pointers to delete a node. However, the same as adding a node, you also need to consider four scenarios to delete a node. The specific code is as follows:

Void dllremove
(
Dl_list * plist,/* pointer to list descriptor */
Dl_node * pnode/* pointer to node to be deleted */
)
{
If (pnode-> previous = NULL)
Plist-> head = pnode-> next;/* 1. Change the head pointer of the linked list */
Else
Pnode-> previous-> next = pnode-> next;/* 2. Change the backward pointer of the forward node to be deleted */

If (pnode-> next = NULL)
Plist-> tail = pnode-> previous;/* 3. Change the tail pointer of the linked list */
Else
Pnode-> next-> previous = pnode-> previous;/* 4. Change the forward pointer of the backward node to be deleted */
}

1.3.1 delete a node as the only node in the linked list

At this time, the nodes in the linked list are both the first and last nodes of the linked list. Therefore, after the node is deleted, there are no nodes in the linked list. The head and tail pointers of the linked list are both null; in this case, the statements executed are 1 and 3. The specific example is as follows:

1.3.2 If there are more than one node in the Linked List, delete the node as the first node of the linked list.

When a node is deleted as the first node of the linked list, the tail pointer of the linked list does not change, and the header Pointer Points to the next node of the node to be deleted, the forward pointer to the backward node of the node to be deleted is null, And the execution statements are 1 and 4. The specific example is as follows:

1.3.3 If there are more than one node in the Linked List, delete the node as the last node of the linked list.

When a node is deleted as the last node of the linked list, the head pointer of the linked list does not change, and the end Pointer Points to the forward node of the node to be deleted, the backward pointer of the forward node of the node to be deleted is null, And the execution statements are 2 and 3. The specific example is as follows:

1.3.4 if there are more than two node points in the Linked List, delete the node as a node in the center of the linked list.

In this case, you only need to change the backward pointer and backward pointer of the forward node of the node to be deleted, connect the forward and backward nodes of the node to be deleted. The execution statements are 2 and 4, as shown below:

1.4 obtain the first node

The dllget function is used to obtain the first node. It mainly includes two functions: deleting the first node and returning the first node; deleting the first node is similar to deleting the node, it mainly refers to the situations described above in 1.3.1 and 1.3.2. It is easier to return the first node:

Dl_node * dllget
(
Fast dl_list * plist/* pointer to list from which to get node */
)
{
Fast dl_node * pnode = plist-> head;

If (pnode! = NULL)/* Is list empty? */
{
Plist-> head = pnode-> next;/* make next node be 1st */

If (pnode-> next = NULL)/* Is there any next node? */
Plist-> tail = NULL;/* No-list is empty */
Else
Pnode-> next-> previous = NULL;/* yes-make it 1st node */
}

Return (pnode );
}

1.5 obtain the number of nodes

You can use the dllcount function to obtain the number of nodes and use the dll_next macro to implement the current operation. The dll_next is defined as follows:

# Define dll_next (pnode )/
(/
(Dl_node *) (pnode)-> next )/
)
The dllcount function is implemented as follows:

Int dllcount
(
Dl_list * plist/* pointer to list descriptor */
)
{
Fast dl_node * pnode = dll_first (plist );
Fast int COUNT = 0;

While (pnode! = NULL)
{
Count ++;
Pnode = dll_next (pnode );
}

Return (count );
}

1.6 traverse nodes

The function of traversing a node is similar to that of returning the number of nodes.

1.7 delete a linked list

Deleting a linked list corresponds to creating a linked list, mainly releasing memory:

Status dlldelete
(
Dl_list * plist/* pointer to list head to be initialized */
)
{
Free (char *) plist);/* free list */
Return OK;
}

The function content is very simple, but I have some objection to this. I always feel that I should first judge whether there are nodes in the linked list, if a node is deleted and the node memory is released, the linked list can be deleted only when there are no nodes in the linked list. Otherwise, some nodes may become wild nodes and cannot be accessed, but it cannot be released. However, there should be its own reason for writing like this in VxWorks. After all, I only read a small part of the code. After all, I should be able to answer this question.

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.