Delete non-cyclic double-stranded table nodes in C Language (no leading node)

Source: Internet
Author: User

Delete non-cyclic double-stranded table nodes in C Language (no leading node)

Non-cyclic double-chain tables that do not take the lead node are difficult to compare When deleting the node, because the prior and next pointers must be maintained at the same time. When processing the first node and the last node, consider the number of nodes as 1. There are two types of deletion:

(1) Delete nodes at the pos position;

(2) determine whether x is in the linked list. If x exists, delete it;

The core code is as follows:

// Delete Node * deletePosList (Node * pNode, int pos) at the pos position {// note that the first Node and the last Node int I = 1 must be considered separately; int size = sizeList (pNode); Node * pMove; pMove = pNode; // empty linked list if (pNode = NULL) {printf ("% s function execution, the linked list is empty. Deleting a node failed \ n ",__ FUNCTION _); return pNode;} // The linked list has only one node, delete the first node if (pos = 1 & size = 1) {free (pNode); pNode = NULL; printf ("% s function execution, the node at position pos = % d is successfully deleted \ n ",__ FUNCTION __, pos); return NULL ;}// if (pos) of the first node is deleted if (pos) = 1) {pNode = pNode-> next; pNode-> prior = NULL; free (pMove); pMove = NULL; printf ("% s function execution, the node at position pos = % d is successfully deleted \ n ",__ FUNCTION __, pos); return pNode;} while (pMove! = NULL) {if (I = pos & pos! = Size) {pMove-> prior-> next = pMove-> next; pMove-> next-> prior = pMove-> prior; free (pMove ); pMove-> next = NULL; printf ("% s FUNCTION execution, node deleted at position pos = % d \ n" ,__ FUNCTION __, pos); return pNode ;} if (I = pos & pos = size) {pMove-> prior-> next = NULL; free (pMove); pMove = NULL; printf ("% s FUNCTION execution, successfully deleted node at position pos = % d \ n" ,__ FUNCTION __, pos); return pNode;} I ++; pMove = pMove-> next;} printf ("% s function execution, failed to delete node at position pos = % d \ n" ,__ FU NCTION __, pos); return pNode;} // determines whether x is in the linked list. If yes, delete Node * deleteValueList (Node * pNode, int x) {Node * pMove; pMove = pNode; int size = sizeList (pNode); // The original linked list is empty. if (pNode = NULL) {printf ("% s function execution, the original linked list is empty, failed to delete node \ n ",__ FUNCTION _); return NULL ;}// the first node is deleted, and the chain table length is 1 if (pNode-> element = x & size = 1) {free (pNode); pNode = NULL; printf ("% s function execution, the deleted Value is % d. node \ n ",__ FUNCTION __, x); return pNode;} // You can separately consider deleting the first node. If (pNode-> element = x) {pNode = pNode-> next; free (pNode-> prior); pNode-> prior = NULL; printf ("% s FUNCTION execution, deletion value: % d node success \ n" ,__ FUNCTION __, x); return pNode;} while (pMove! = NULL) {// The node to be deleted is not the last if (pMove-> element = x & pMove-> next! = NULL) {pMove-> prior-> next = pMove-> next; pMove-> next-> prior = pMove-> prior; free (pMove); pMove = NULL; printf ("% s FUNCTION execution, deletion value: % d node succeeded \ n" ,__ FUNCTION __, x); return pNode ;} // The Last node to be deleted is the if (pMove-> element = x & pMove-> next = NULL) {pMove-> prior-> next = NULL; free (pMove); pMove = NULL; printf ("% s FUNCTION execution, delete value: % d node success \ n" ,__ FUNCTION __, x); return pNode ;} 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.