I was in a previous blog "C Language implementation of single-linked list (not the lead node) insertion of nodes" specifically, how to do in a non-lead node in a single linked list of the insertion of nodes. But in practical applications, the list of leading nodes is more frequently used. More convenient. Today we're going to use a single-linked list of lead nodes for node insertion. The demo sample code is uploaded to Https://github.com/chenyufeng1991/InsertList_HeadNode.
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; There is no need to consider the first node separately while (pmove! = NULL) { if (i = = pos) { Pinsert->next = pmove->next; Pmove->next = Pinsert; printf ("%s function runs, insert value=%d successfully in pos=%d location \ n", __function__,pos,x); return pnode; } i++; Pmove = pmove->next; } printf ("%s" function runs.) Failed to insert node \ n ", __function__); return pnode;}
C language implementation of single-linked table node insertion (lead node)