Delete a single-chain table node in C Language (lead node)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.