-
#include <stdio.h>
-
#include <stdlib.h>
-
#include "mylist.h"
-
struct student{
-
int age;
-
struct list_head list;
-
};
-
-
int my_init_list(struct student* head)
-
{
-
INIT_LIST_HEAD(&head->list);
-
if(list_empty(&head->list))
-
printf("list_empty\n");
-
}
-
-
int my_add_list(struct student* head)
-
{
-
int i;
-
struct student *p;
-
for(i=0; i<5; i++)
-
{
-
p = (struct student*)malloc(sizeof(struct student));
-
p->age = i*10;
-
list_add(&p->list, &head->list);
-
}
-
return 0;
-
}
-
-
int my_traver_list(struct student* head)
-
{
-
struct list_head *plist;
-
struct student *p;
-
-
printf("<--------\n");
-
list_for_each(plist, &head->list)
-
{
-
p = list_entry(plist, struct student, list);
-
printf("p->age=%d\n", p->age);
-
}
-
printf("-------->\n");
-
return 0;
-
}
-
-
int my_del_node(struct student* head)
-
{
-
struct list_head *plist;
-
struct student *p;
-
-
list_for_each(plist, &head->list)
-
{
-
p = list_entry(plist, struct student, list);
-
// printf("p->age=%d\n", p->age);
-
if(p->age==20)
-
break;
-
}
-
list_del(plist);
-
return 0;
-
}
-
-
int main ( int argc, char *argv[] )
-
{
-
struct student s_head;
-
my_init_list(&s_head); //init list
-
my_add_list(&s_head); //加入5个node
-
my_traver_list(&s_head); //遍历list
-
-
my_del_node(&s_head); //删除age=20的node
-
my_traver_list(&s_head); //再次遍历list
-
return EXIT_SUCCESS;
- }
-
cong@msi:/work/os/test/list$ ./mylist
-
list_empty //init之后判断empty是空的
-
<--------
-
p->age=40
-
p->age=30
-
p->age=20
-
p->age=10
-
p->age=0
-
--------> //加入5个结点后遍历的打印
-
<--------
-
p->age=40
-
p->age=30
-
p->age=10
-
p->age=0 //删除age=20的结点后遍历的打印
- -------->
