Kernel linked list in Linux

Source: Internet
Author: User

List of the general to be initialized, insert, delete, display, release linked list, look for the nodes of these operations, the following I have a brief introduction to these operations, because my ability is not enough, there may be some things to understand the depth, resulting in a certain error, please Bo friends pointed out.

A, the Linux kernel linked list of several main functions (below is the kernel of the source code to give you to analyze it)

1) Initialize:

#define Init_list_head (PTR) do {\
(PTR)->next = (PTR); (PTR)->prev = (PTR); \
} while (0)//ptr is a struct list_head, which includes two pointers next and Prev, which can already be seen as a two-way circular linked list of the kernel list

2) Trailing insert:

static inline void List_add_tail (struct list_head *new, struct list_head *head)
{
__list_add (New, Head->prev, head);
}//Trailing insert, the passed parameter is two pointers in the new node and two pointers in the head node

3) Head insertion function

static inline void List_add (struct list_head *new, struct list_head *head)
{
__list_add (New, head, Head->next);
}//Header Insert function, the passed parameter is two pointers in the new node and two pointers in the head node

4) Delete node function

static inline void List_del (struct list_head *entry)//Incoming pointer field in the node to be deleted
{
__list_del (Entry->prev, entry->next);//Two parameters are the previous node and the latter node, respectively, of the deleted node
Entry->next = (void *) 0;//the node is empty after it is deleted
Entry->prev = (void *) 0;
}

5) Display function (if you want to print out the information in the linked list, you have to write the printing function, such as printf, because this is actually a traversal function, no display function)

#define LIST_FOR_EACH_ENTRY (POS, head, member) \
for (pos = list_entry (head)->next, typeof (*pos), member); \
&pos->member! = (head); \
pos = List_entry (Pos->member.next, typeof (*pos), member))

/* This function is used to traverse the linked list
POS is a node pointer,
Head is the address of two pointers in the head node
Member as a pointer field in each node
*/

6) Delete Linked list

#define LIST_FOR_EACH_SAFE (POS, n, head) \
for (pos = (head)->next, n = pos->next; pos! = (head); \
pos = N, n = pos->next)

Both the POS and n are list_head pointers, and the n pointers are used to keep the linked list from breaking when deleted.

7) Find the node (which is also the traversal function in the kernel)

#define LIST_FOR_EACH_ENTRY (POS, head, member) \
for (pos = list_entry (head)->next, typeof (*pos), member); \
&pos->member! = (head); \
pos = List_entry (Pos->member.next, typeof (*pos), member))

B. The following section of the code to show you how to use the specific method

#include "kernel.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct LIST_NODE
{
int data;
The pointer domain of the struct List_head list;//node is encapsulated within the struct list_head structure.
This structure includes the struct List_head *next,*prev
}*node,node1;


Node Init_head (node Head)//Initialize empty list
{
Head = (node) malloc (sizeof (NODE1));//Allocate space for nodes
if (head = = NULL)
{
Perror ("Head");
return NULL;
}
Init_list_head (& (Head->list));//#define INIT_LIST_HEAD (PTR) do {\
(PTR)->next = (PTR); (PTR)->prev = (PTR); \
} while (0)//Call the initialization function in the kernel, the passed parameter is
The two pointers in the node, that is, two pointers in the struct list_head struct body
return head;
}

Node Insert_tail (node Head,int data)//trailing Insert function
{
Node new = (node) malloc (sizeof (NODE1));//Allocate space for new nodes
if (new = NULL)//Determine if the allocation space is successful
{
Perror ("NEW:");
return NULL;
}
New->data = data;
List_add_tail (& (New->list),& (head->list));//Call the function from the tail in the kernel and pass in the parameter as two pointers in the new node
and two pointers in the head node
return 0;
}

Head_insert_node (node Head,int data)//Head Insert function
{
Node new;//creating a new node
New = (node) malloc (sizeof (NODE1));//Allocate space for the node
if (new = NULL)//Determine if the allocation space is successful
{
Perror ("NEW:");
return 0;
}
New->data = data;
List_add (& (New->list),& (head->list));//The function that is inserted from the beginning of the kernel, two pointers to the two pointers and head nodes of the new node for the passed arguments
return 0;
}

Node Search_node (node Head,int data)//Search for nodes function
{
Node p = NULL;
List_for_each_entry (p,& (head->list), list)//traversal function in the kernel
{
if (P->data = = data)//p is the node that needs to be found
{
printf ("Found the data:%d\n", p->data);
Goto OK;
}
}

Puts ("Not found the data!");
return NULL;

Ok:
return p;
}

int Show_node (node tmp)
{
if (tmp = = NULL)
{
Puts ("TMP is null!");
return-1;
}
printf ("The data is%d\n", tmp->data);
return 0;
}

int Delete_node (node head,int data)
{
Node p = NULL;
List_for_each_entry (p,& (head->list), list)
{
if (P->data = = data)
{
printf ("Found the data which you want to delete!\n");
Goto F;
}
}

F:
List_del (& (P->list));
Free (p);
return 0;
}

int show_list (node head)
{
Node p = NULL;
List_for_each_entry (p,& (head->list), list)
{
printf ("data:%d\n", p->data);
}
return 0;
}


int delete_list (node head)//delete list function
{
Node p,q;
List_for_each_entry_safe (p,q,& (head->list), list)//This is the security delete function in the kernel
{
List_del (& (P->list));
Free (p);
}

List_del (& (Head->list));
Free (head);
return 0;
}
int main (int argc,char **argv)
{
Node head;
node tmp;
Head = Init_head (head);//Initialize empty list function
Insert_tail (head,45);//Insert function from end
Insert_tail (head,55);
Insert_tail (head,65);

Head_insert_node (head,75);//Insert function from the beginning
Show_list (head); Show linked list functions

TMP = Search_node (head,55);//Find node function
Show_node (head);
Delete_node (head,55);
Show_list (head);
Delete_list (head);//Delete list function
return 0;
}

Kernel linked list in Linux

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.