Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2101007
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: LINUX

2016-11-18 17:21:33

1. 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "mylist.h"
  4. struct student{
  5.     int age;
  6.     struct list_head list;
  7. };

  8. int my_init_list(struct student* head)
  9. {
  10.     INIT_LIST_HEAD(&head->list);
  11.     if(list_empty(&head->list))
  12.         printf("list_empty\n");
  13. }

  14. int my_add_list(struct student* head)
  15. {
  16.     int i;
  17.     struct student *p;
  18.     for(i=0; i<5; i++)
  19.     {
  20.         p = (struct student*)malloc(sizeof(struct student));
  21.         p->age = i*10;
  22.         list_add(&p->list, &head->list);
  23.     }
  24.     return 0;
  25. }

  26. int my_traver_list(struct student* head)
  27. {
  28.     struct list_head *plist;
  29.     struct student *p;

  30.     printf("<--------\n");
  31.     list_for_each(plist, &head->list)
  32.     {
  33.         p = list_entry(plist, struct student, list);
  34.         printf("p->age=%d\n", p->age);
  35.     }
  36.     printf("-------->\n");
  37.     return 0;
  38. }

  39. int my_del_node(struct student* head)
  40. {
  41.     struct list_head *plist;
  42.     struct student *p;

  43.     list_for_each(plist, &head->list)
  44.     {
  45.         p = list_entry(plist, struct student, list);
  46.     // printf("p->age=%d\n", p->age);
  47.         if(p->age==20)
  48.             break;
  49.     }
  50.     list_del(plist);
  51.     return 0;
  52. }

  53. int main ( int argc, char *argv[] )
  54. {
  55.     struct student s_head;
  56.     my_init_list(&s_head);         //init list
  57.     my_add_list(&s_head);          //加入5个node
  58.     my_traver_list(&s_head);       //遍历list

  59.     my_del_node(&s_head);          //删除age=20的node
  60.     my_traver_list(&s_head);       //再次遍历list
  61.     return EXIT_SUCCESS;
  62. }
2.运行结果
  1. cong@msi:/work/os/test/list$ ./mylist
  2. list_empty               //init之后判断empty是空的
  3. <--------
  4. p->age=40
  5. p->age=30
  6. p->age=20
  7. p->age=10
  8. p->age=0
  9. -------->                 //加入5个结点后遍历的打印
  10. <--------
  11. p->age=40
  12. p->age=30
  13. p->age=10
  14. p->age=0                  //删除age=20的结点后遍历的打印
  15. -------->
3. 代码打包
   mylist.rar (下载后改名为mylist.tar.gz)
阅读(1182) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~