Linked List in linux

Source: Internet
Author: User
// Include/linux/list. h
Struct list_head {
Struct list_head * next, * prev;
};

# Define LIST_HEAD_INIT (name) {& (name), & (name )}
# Define LIST_HEAD (name) struct list_head name = LIST_HEAD_INIT (name)

# Define INIT_LIST_HEAD (ptr) do {\
(Ptr)-> next = (ptr); (ptr)-> prev = (ptr );\
} While (0)

Static inline void _ list_add (struct list_head * newnode,
Struct list_head * prev,
Struct list_head * next)
{
Next-> prev = newnode;
Newnode-> next = next;
Newnode-> prev = prev;
Prev-> next = newnode;
}

// Add a node
Static inline void list_add (struct list_head * newnode, struct list_head * head)
{
_ List_add (newnode, head, head-> next );
}

Static inline void list_add_tail (struct list_head * newnode, struct list_head * head)
{
_ List_add (newnode, head-> prev, head );
}

Static inline int list_empty (struct list_head * head)
{
Return head-> next = head;
}

Static inline void _ list_del (struct list_head * prev,
Struct list_head * next)
{
Next-> prev = prev;
Prev-> next = next;
}
// Delete a node
Static inline void list_del (struct list_head * entry)
{
_ List_del (entry-> prev, entry-> next );
Entry-> next = entry-> prev = 0;
}

# Define list_for_each_safe (pos, n, head )\
For (pos = (head)-> next, n = pos-> next; pos! = (Head );\
Pos = n, n = pos-> next)

# Define list_for_each (pos, head )\
For (pos = (head)-> next; pos! = (Head );\
Pos = pos-> next)

# Define list_entry (ptr, type, member )\
(Type *) (char *) (ptr)-(unsigned long) (& (type *) 0)-> member )))

/*******************************
** The pointer ptr points to the member in the struct type;
** Returns the starting address of the struct type through the pointer ptr.
Type

| ---------- |
|
|
| ---------- |
Ptr --> | member -- |
| ---------- |
|
|
| ---------- |
********************************/

// Test_list.c

# Include <stdio. h>
# Include <stdlib. h>

// # Include "ilist. h"

Struct my_list {
Struct list_head list;
Char value [10];
};

Int main (int argc, char ** argv ){

Struct my_list * tmp;
Struct list_head * pos, * q;
Unsigned int I;

Struct my_list mylist;
INIT_LIST_HEAD (& mylist. list);/* initialize the linked list header */

/* Add elements to mylist */
For (I = 3; I! = 0; -- I ){
Tmp = (struct my_list *) malloc (sizeof (struct my_list ));

/* Or INIT_LIST_HEAD (& tmp-> list );*/
Printf ("enter value :");
Scanf ("% s", tmp-> value );


List_add (& (tmp-> list), & (mylist. list ));
/* You can also use list_add_tail () to add elements at the end of the table */
}
Printf ("\ n ");

Printf ("traversing the list using list_for_each () \ n ");
List_for_each (pos, & mylist. list ){

/* Here pos-> next points to the next node, and pos-> prev refers to the forward node. The node here is
Struct my_list type. However, we need to access the node itself, instead of the list field in the node,
The macro list_entry () is for this purpose. */

Tmp = list_entry (pos, struct my_list, list );
Printf ("% s", tmp-> value );
}
Printf ("\ n ");

Printf ("deleting the list using list_for_each_safe () \ n ");
List_for_each_safe (pos, q, & mylist. list ){
Tmp = list_entry (pos, struct my_list, list );
Printf ("% s", tmp-> value );
List_del (pos );
Free (tmp );
}
}

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.