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 ;}