Bidirectional linked list operation (Unified linked list Interface)

Source: Internet
Author: User

This is a document issued during company training, not original

This is a list of two-way linked list header files. h

I understand. Almost all linked list operations are OK, which is quite easy to use.

# Ifndef _ list_h _
# DEFINE _ list_h _

/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("_ XXX") are useful when
* Manipulating whole lists rather than single entries,
* Sometimes we already know the next/Prev entries and we can
* Generate better code by using them directly rather
* Using the generic single-entry routines.
*/

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)

/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* The Prev/next entries already!
*/
Static inline void _ list_add (struct list_head * New, struct list_head * Prev, struct list_head * Next)
{
Next-> Prev = new;
New-> next = next;
New-> Prev = Prev;
Prev-> next = new;
}

/**
* List_add-Add a new entry
* @ New: New entry to be added
* @ Head: List head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
Static inline void list_add (struct list_head * New, struct list_head * head)
{
_ List_add (new, head, head-> next );
}

/**
* List_add_tail-Add a new entry
* @ New: New entry to be added
* @ Head: List head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
Static inline void list_add_tail (struct list_head * New, struct list_head * head)
{
_ List_add (new, head-> Prev, head );
}

/*
* Delete A List entry by making the prev/next entries
* Point to each other.
*
* This is only for internal list manipulation where we know
* The Prev/next entries already!
*/
Static inline void _ list_del (struct list_head * Prev, struct list_head * Next)
{
Next-> Prev = Prev;
Prev-> next = next;
}

/**
* List_del-deletes entry from list.
* @ Entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
Static inline void list_del (struct list_head * entry)
{
_ List_del (Entry-> Prev, entry-> next );
}

/**
* List_del_init-deletes entry from list and reinitialize it.
* @ Entry: the element to delete from the list.
*/
Static inline void list_del_init (struct list_head * entry)
{
_ List_del (Entry-> Prev, entry-> next );
Init_list_head (entry );
}

/**
* List_empty-Tests whether a list is empty
* @ Head: the list to test.
*/
Static inline int list_empty (struct list_head * head)
{
Return head-> next = head;
}

/**
* List_splice-join two lists
* @ List: the new list to add.
* @ Head: the place to add it in the first list.
*/
Static inline void list_splice (struct list_head * List, struct list_head * head)
{
Struct list_head * First = List-> next;

If (first! = List ){
Struct list_head * Last = List-> Prev;
Struct list_head * at = head-> next;

First-> Prev = head;
Head-> next = first;

Last-> next =;
At-> Prev = last;
}
}

/**
* List_entry-get the struct for this entry
* @ PTR: The & struct list_head pointer.
* @ Type: the type of the struct this is embedded in.
* @ Member: the name of the list_struct within the struct.
*/
# Define list_entry (PTR, type, member )/
(Type *) (char *) (PTR)-(unsigned long) (& (type *) 0)-> member )))

/**
* List_for_each-iterate over a list
* @ Pos: The & struct list_head to use as a loop counter.
* @ Head: The head for your list.
*/
# Define list_for_each (Pos, head )/
For (Pos = (head)-> next; pos! = (Head );/
Pos = pos-> next)

/**
* List_for_each_safe-iterate over a list safe against removal of List entry
* @ Pos: The & struct list_head to use as a loop counter.
* @ N: Another & struct list_head to use as temporary storage
* @ Head: The head for your list.
*/
# Define list_for_each_safe (Pos, N, head )/
For (Pos = (head)-> next, n = pos-> next; pos! = (Head );/
Pos = n, n = pos-> next)

/**
* List_for_each_entry-iterate over list of given type
* @ Pos: The type * to use as a loop counter.
* @ Head: The head for your list.
* @ Member: the name of the list_struct within the struct.
*/
# Define list_for_each_entry (Pos, Head, Member, type )/
For (Pos = list_entry (head)-> next, type, member );/
& Pos-> member! = (Head );/
Pos = list_entry (POS-> member. Next, type, member ))

/**
* List_for_each_entry_safe-a secure usage for deleting the list_for_each_entry node of the Time Series
* @ Pos: The type * to use as a loop counter.
* @ N: Another & struct list_head to use as temporary storage
* @ Head: The head for your list.
* @ Member: the name of the list_struct within the struct.
*/
# Define list_for_each_entry_safe (Pos, N, Head, Member, type )/
For (Pos = list_entry (head)-> next, type, member), n = pos-> member. Next ;/
& Pos-> member! = (Head );/
Pos = list_entry (n, type, member), n = pos-> member. Next)

# Endif

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.