Using the C language to insert elements to any position in a bidirectional non-circular linked list (the end node of the lead node)

Source: Internet
Author: User

Using the C language to insert elements to any position in a bidirectional non-circular linked list (the end node of the lead node)

For a two-way linked list, it is recommended that you use the tail node of the leading node to handle the problem easily. In "insert nodes that implement bidirectional non-circular linked list (do not take the lead node) in C Language", I have inserted nodes that do not take the lead node in detail. This time, we will insert elements at any position when using the head node and the end node. Upload the code to https://github.com/chenyufeng1991/insertnodedoublelinkedlist_headnode.

The core code is as follows:

 

// Insert a Node // The insertion locations are 0, 1, 2, .....int InsertNodeList (Node * pHead, Node * pTail, int pos, int x) {int I = 0; node * pMove; Node * pInsert; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node )); pInsert-> prior = NULL; pInsert-> next = NULL; pInsert-> element = x; pMove = pHead; while (pMove! = PTail) {if (I = pos) {// note the link sequence pMove-> next-> prior = pInsert; pInsert-> next = pMove-> next; pMove-> next = pInsert; pInsert-> prior = pMove; printf ("% s function execution, insert x = % d node at position pos = % d successfully \ n ", __function __, pos, x); return 1;} I ++; pMove = pMove-> next;} printf ("% s FUNCTION execution, failed to insert element \ n ", __function _); return 0 ;}


 

 

Related Article

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.