Delete a single-chain table node in C Language (lead node)
In my previous blog "delete single-link table nodes in C Language (do not take the lead node)", I described how to delete a node from a single-link table with no lead node, in this blog, I changed it to a single-chain table with the leading node.
There are two types of deletion:
(1) Delete the pos node at a certain position;
(2) Determine whether the x value is in the linked list. If yes, delete the node;
The core code is as follows:
// Delete Node * DeletePosNode (Node * pNode, int pos) {int I = 1; Node * pMove; Node * pMovePre; pMovePre = pNode; pMove = pNode-> next; while (pMove! = NULL) {if (I = pos) {pMovePre-> next = pMove-> next; free (pMove); pMove = NULL; printf ("% s function execution, node deleted at position pos = % d \ n ",__ FUNCTION __, pos); return pNode;} I ++; pMovePre = pMovePre-> next; pMove = pMove-> next;} printf ("% s FUNCTION execution, failed to delete node at position pos = % d \ n" ,__ FUNCTION __, pos); return pNode ;} // determine whether the x value is in the linked list. If yes, delete the Node * DeleteValueNode (Node * pNode, int x) {Node * pMovePre; Node * pMove; pMovePre = pNode; pMove = pNode-> n Ext; while (pMove! = NULL) {if (pMove-> element = x) {pMovePre-> next = pMove-> next; free (pMove); pMove = NULL; printf ("% s FUNCTION execution, delete value = % d node succeeded \ n" ,__ FUNCTION __, x); return pNode;} pMovePre = pMovePre-> next; pMove = pMove-> next;} printf ("% s FUNCTION execution, delete value = % d node failed \ n" ,__ FUNCTION __, x); return pNode ;}