Insert nodes in a single-chain table using C language (Leading node)
In my previous blog "insert a single-link table (no-leading node) node in C Language", I described in detail how to insert a node in a single-link table with no leading node. However, in practical applications, linked lists with leading nodes are more commonly used and more convenient. Today, we will use the single-chain table of the leading node for node insertion.
The core code is as follows:
Node * InsertNode (Node * pNode, int pos, int x) {int I = 0; Node * pMove; Node * pInsert; pInsert = (Node *) malloc (sizeof (Node); memset (pInsert, 0, sizeof (Node); pInsert-> next = NULL; pInsert-> element = x; pMove = pNode; // The first node while (pMove! = NULL) {if (I = pos) {pInsert-> next = pMove-> next; pMove-> next = pInsert; printf ("% s function execution, value = % d inserted at position pos = % d success \ n ",__ FUNCTION __, pos, x); return pNode;} I ++; pMove = pMove-> next;} printf ("% s FUNCTION execution, failed to insert node \ n" ,__ FUNCTION _); return pNode ;}