List linked list: Delete a node list_del from the linked list

Source: Internet
Author: User

Delete a node from the linked list using the list_del interface. Pay attention when using list_del.

The list_del implementation is as follows:

Static inline void list_del (struct list_head * entry)
{
_ List_del (entry-> prev, entry-> next );
Entry-> next = list_python1;
Entry-> prev = list_python2;
}

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


In fact, it is to point the prev of the node to next, next to prev, and cross the current node

Then, the next and prev values of the current node are assigned to a meaningless value.

Therefore, if you want to continue the loop, you need to temporarily store the prev node of the current node,

Then execute list_del, and then restore the prev node to the current node, that is, to process it in a loop:


/* Del a node from list */list_for_each (tmp, & list_use_head) {struct list_head * tmp1; pstListTmp = list_entry (tmp, struct stListUse, list); printf ("index: % d, name: % s, pointer: % s \ n ", pstListTmp-> index, pstListTmp-> name, pstListTmp-> pointer ); if (5 = pstListTmp-> index) {printf ("del node 5 from list! \ N "); tmp1 = tmp-> prev; list_del (tmp); tmp = tmp1 ;}}

The complete test code is as follows:

[Email protected]:/mnt/shared/kernelbox/list # cat listuse. c # include "list. h "struct stListUse {char name [32]; char * pointer; int index; struct list_head list ;};# define STR_LEN_IN_NODE 32LIST_HEAD (list_use_head); struct stListUse * pstListNode; int main (int argc, char * argv []) {int I; char nametmp [STR_LEN_IN_NODE]; struct stListUse * pstListTmp; struct list_head * tmp; printf ("enter listuse. c/main () \ n "); For (I = 0; I <10; I ++) {pstListNode = (struct stListUse *) malloc (sizeof (struct stListUse); memset (pstListNode, 0, sizeof (struct stListUse);/* init node's member: index */pstListNode-> index = I;/* init node's member: name */memset (nametmp, 0, STR_LEN_IN_NODE); sprintf (nametmp, "name % d", I); strcpy (pstListNode-> name, nametmp);/* init node's member: pointer */pstListNode-> pointer = (char *) mallo C (optional * sizeof (char); memset (pstListNode-> pointer, 0, sizeof (STR_LEN_IN_NODE * sizeof (char); memset (nametmp, 0, STR_LEN_IN_NODE ); sprintf (nametmp, "pointer % d", I); strcpy (pstListNode-> pointer, nametmp ); /* add node I to list list_use_head */# if 0 list_add (& pstListNode-> list, & list_use_head); # endif list_add_tail (& pstListNode-> list, & list_use_head );} /* print list */pstListTmp = (s Truct stListUse *) malloc (sizeof (struct stListUse )); printf ("************************************* * ******* \ n "); list_for_each (tmp, & list_use_head) {pstListTmp = list_entry (tmp, struct stListUse, list); printf ("index: % d, name: % s, pointer: % s \ n ", pstListTmp-> index, pstListTmp-> name, pstListTmp-> pointer);} printf (" list_usr_head: % p \ n ", list_use_head ); printf ("tmp: % p \ n", tmp); printf ("***************** * *************************** \ N "); /* del a node from list */list_for_each (tmp, & list_use_head) {struct list_head * tmp1; pstListTmp = list_entry (tmp, struct stListUse, list); printf ("index: % d, name: % s, pointer: % s \ n ", pstListTmp-> index, pstListTmp-> name, pstListTmp-> pointer ); if (5 = pstListTmp-> index) {printf ("del node 5 from list! \ N "); tmp1 = tmp-> prev; list_del (tmp); tmp = tmp1 ;}} printf (" list_usr_head: % p \ n ", list_use_head ); printf ("tmp: % p \ n", tmp ); /* print list */printf ("******************************* * ************* \ n "); list_for_each (tmp, & list_use_head) {pstListTmp = list_entry (tmp, struct stListUse, list); printf ("index: % d, name: % s, pointer: % s \ n ", pstListTmp-> index, pstListTmp-> name, pstListTmp-> pointer);} printf (" list_usr_head: % p \ n ", list_use_head ); printf ("tmp: % p \ n", tmp ); printf ("************************************* * ******* \ n "); return 0 ;}


List linked list: Delete a node list_del from the linked list

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.