/*********************************************************************************
|
* Copyright: (C) 2012 Guo Wenxue <guowenxue@gmail.com>
|
* All rights reserved.
|
*
|
* Filename: list.c
|
* Description: This is the list API from linux kernel example code.
|
*
|
* Notice: Memory leak check: valgrind --leak-check=full ./list
|
*
|
* Version: 1.0.0(08/08/2012~)
|
* Author: Guo Wenxue <guowenxue@gmail.com>
|
* ChangeLog: 1, Release initial version on "08/08/2012 06:51:40 PM"
|
*
|
********************************************************************************/
|
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include "list.h"
|
|
typedef struct node_s
|
{
|
int data;
|
struct list_head list;
|
} node_t;
|
|
int main (int argc, char **argv)
|
{
|
int i;
|
struct list_head head;
|
node_t *node, *tmp;
|
|
INIT_LIST_HEAD(&head);
|
|
/* Create the link list */
|
printf("Create link list:\n");
|
for(i=0; i<10; i++)
|
{
|
node = malloc(sizeof(*node));
|
node->data = i;
|
|
printf("List add an node data : %p->%d\n", node, node->data);
|
list_add_tail(&node->list, &head);
|
}
|
|
/* Use list_for_each_entry to travel list, we can not remove the node in it */
|
printf("\nTraversing the list:\n");
|
list_for_each_entry(node, &head, list)
|
{
|
printf("List travel node data : %p->%d\n", node, node->data);
|
}
|
|
/* Use list_for_each_entry_safe to destroy the list */
|
printf("\nDestroy link list:\r\n");
|
list_for_each_entry_safe(node, tmp, &head, list)
|
{
|
printf("List remove node data : %p->%d\n", node, node->data);
|
list_del(&node->list);
|
free(node);
|
}
|
printf("\n");
|
|
return 0;
|
}
|