Linux内核---60.linux内核链表list的使用

来源:互联网 发布:学白话软件 编辑:程序博客网 时间:2024/05/02 12:26
1.链表的基本操作
   初始化链表-->插入链表-->遍历链表-->删除链表中的某一项
下面两个代码一个不用entry,一个用entry,还是用entry的比较简单明了
2.代码->testlist1.c
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>

  4. #define LIST_LEN 10
  5. typedef struct __num_node_{
  6.     int num;
  7.     struct list_head list;
  8. }num_node;

  9. num_node head;
  10. static int __init hello_init(void)
  11. {
  12.     int i;
  13.     -->不用entry时,既需要定义struct list_head也需要定义num_node
  14.     struct list_head* pos;
  15.     struct list_head* n;
  16.     num_node* listnode;
  17.     num_node* p;
  18.     //1.初始化链表
  19.     INIT_LIST_HEAD(&head.list);             -->初始化双向链表
  20.     //2.向链表中添加项
  21.     printk(KERN_ALERT "add node 1-10:\n");
  22.     for(i=0; i<LIST_LEN; i++)
  23.     {
  24.         listnode = (num_node*)kmalloc(sizeof(num_node), GFP_KERNEL);
  25.         listnode->num = i;
  26.         list_add_tail(&listnode->list, &head.list);  -->只是把list插入到链表中,而数据本身不会插入到链表中
  27.         printk(KERN_ALERT "add node=%d\n", i);
  28.     }
  29.     printk(KERN_ALERT "\n");
  30.     //3.遍历链表
  31.     printk(KERN_ALERT "node traversal 1111:\n");
  32.     list_for_each(pos, &head.list)
  33.     {
  34.         p = list_entry(pos, num_node, list);
  35.         printk(KERN_ALERT "node data=%d\n", p->num);
  36.     }
  37.     printk(KERN_ALERT "\n");
  38.     //4.删除链表中的某一项
  39.     printk(KERN_ALERT "node delete 3:\n");
  40.     list_for_each_safe(pos, n, &head.list)
  41.     {
  42.         p = list_entry(pos, num_node, list);
  43.         if(== p->num)
  44.         {
  45.             printk(KERN_ALERT "find node -> 3\n");
  46.             list_del(&p->list);
  47.             kfree(p);
  48.         }
  49.         //printk(KERN_ALERT "node data=%d\n", p->num);
  50.     }
  51.     printk(KERN_ALERT "\n");
  52.     //5.再次遍历链表
  53.     printk(KERN_ALERT "node traversal 2222:\n");
  54.     list_for_each(pos, &head.list)
  55.     {
  56.         p = list_entry(pos, num_node, list);
  57.         printk(KERN_ALERT "node data=%d\n", p->num);
  58.     }
  59.     printk(KERN_ALERT "\n");
  60.     
  61.     return 0;
  62. }

  63. static void __exit hello_exit(void)
  64. {
  65.     printk(KERN_ALERT "goodbye my first dirver\n");
  66.     return ;
  67. }

  68. module_init(hello_init);
  69. module_exit(hello_exit);
  70. MODULE_LICENSE("GPL");
  71. MODULE_AUTHOR("wangcong");


3. 代码->testlist2.c
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/slab.h>

  4. #define LIST_LEN 10
  5. typedef struct __num_node_{
  6.     int num;
  7.     struct list_head list;
  8. }num_node;

  9. num_node head;
  10. static int __init hello_init(void)
  11. {
  12.     int i;
  13.     num_node* listnode;
  14.     num_node* p;      -->这个地方就不需要struct list_head
  15.     num_node* n;      -->这个地方就不需要struct list_head
  16.     //1.初始化链表
  17.     INIT_LIST_HEAD(&head.list);
  18.     //2.向链表中添加项
  19.     printk(KERN_ALERT "add node 1-10:\n");
  20.     for(i=0; i<LIST_LEN; i++)
  21.     {
  22.         listnode = (num_node*)kmalloc(sizeof(num_node), GFP_KERNEL);
  23.         listnode->num = i;
  24.         list_add_tail(&listnode->list, &head.list);
  25.         printk(KERN_ALERT "add node=%d\n", i);
  26.     }
  27.     printk(KERN_ALERT "\n");
  28.     //3.遍历链表
  29.     printk(KERN_ALERT "node traversal 1111:\n");
  30.     list_for_each_entry(p, &head.list, list)
  31.     {
  32.         printk(KERN_ALERT "node data=%d\n", p->num);
  33.     }
  34.     printk(KERN_ALERT "\n");
  35.     //4.删除链表中的某一项
  36.     printk(KERN_ALERT "node delete 4:\n");
  37.     list_for_each_entry_safe(p, n, &head.list, list)
  38.     {
  39.         if(== p->num)
  40.         {
  41.             printk(KERN_ALERT "find node -> 4\n");
  42.             list_del(&p->list);
  43.             kfree(p);
  44.         }
  45.         //printk(KERN_ALERT "node data=%d\n", p->num);
  46.     }
  47.     printk(KERN_ALERT "\n");
  48.     //5.再次遍历链表
  49.     printk(KERN_ALERT "node traversal 2222:\n");
  50.     list_for_each_entry(p, &head.list, list)
  51.     {
  52.         printk(KERN_ALERT "node data=%d\n", p->num);
  53.     }
  54.     printk(KERN_ALERT "\n");
  55.     
  56.     return 0;
  57. }

  58. static void __exit hello_exit(void)
  59. {
  60.     printk(KERN_ALERT "goodbye my first dirver\n");
  61.     return ;
  62. }

  63. module_init(hello_init);
  64. module_exit(hello_exit);
  65. MODULE_LICENSE("GPL");
  66. MODULE_AUTHOR("wangcong");
注:判断链表为空
 printk(KERN_ALERT "list_empty=%d\n", list_empty(&head.list));
为空时会打印1, 不为空时会打印0

4.代码打包
testlist.rar(下载后改名为testlist.tar.gz)
0 0
原创粉丝点击