It is the most important operation to increase and revise the single-linked list. I was in the previous blog, "C Language Implementation linked list node deletion" to remove a single linked list of a node.
We're going to come here now. Insert a node in a location. The demo sample code is uploaded to Https://github.com/chenyufeng1991/InsertList.
The core code is as follows:
Node *inserttoposition (node *pnode,int pos,int x) { if (pos < 0 | | pos > sizelist (pnode)) { printf ("%s" function run, p os=%d illegal, inserting data failed \ n ", __function__,pos); return pnode; } Node *pmove; Node *pinsert; Pinsert = (node *) malloc (sizeof (node)); memset (pinsert, 0, sizeof (Node)); Pinsert->next = NULL; pinsert->element = x; Pmove = Pnode; int i = 1; Here the case of Pos=0 is considered separately if (pos = = 0) { pinsert->next = Pnode; Pnode = Pinsert; printf ("%s" function, x=%d succeeded in pos=%d insert \ n ", __function__,pos,x); return pnode; } while (pmove! = NULL) { if (i = = pos) { Pinsert->next = pmove->next; Pmove->next = Pinsert; printf ("%s" function runs.) In pos=%d insert x=%d success \ n ", __function__,pos,x); break; } i++; Pmove = pmove->next; } return pnode;}
C-language implementation of single-linked list (without the lead node) insertion of nodes