C language implementation of the non-circular doubly linked list node deletion (not the lead junction)

Source: Internet
Author: User

Non-cyclic doubly linked lists that do not take the lead are more troublesome when deleting nodes, because both prior and next two pointers are maintained. The first node and the last node should be considered separately while the number of nodes is 1. The deletion is divided into the following two categories:

(1) Delete the POS location node;

(2) Determine if x is in the linked list, and if it exists, delete it;

The code is uploaded to Https://github.com/chenyufeng1991/DeleteNodeDoubleList.

The core code is as follows:

Delete node of POS location Nodes *deleteposlist (node *pnode,int pos) {//Note the first node and the last node need to be considered separately int i = 1;    int size = Sizelist (Pnode);    Node *pmove;    Pmove = Pnode;        The list is empty if (Pnode = = NULL) {printf ("%s" function is executed, the list is empty, node deletion failed \ n ", __function__);    return pnode;        }//List only one node, delete the first node if (pos = = 1 && size = = 1) {free (pnode);        Pnode = NULL;        printf ("%s function execution, delete pos=%d location node succeeded \ n", __function__,pos);    return NULL;        }//List node greater than 1, delete first node if (pos = = 1) {Pnode = pnode->next;        Pnode->prior = NULL;        Free (pmove);        Pmove = NULL;        printf ("%s function execution, delete pos=%d location node succeeded \ n", __function__,pos);    return pnode; } while (Pmove! = NULL) {if (i = = pos && pos! = size) {Pmove->prior->next = Pmove-&gt            ; next;            Pmove->next->prior = pmove->prior;            Free (pmove);            Pmove->next = NULL;         printf ("%s function execution, delete pos=%d location node succeeded \ n", __function__,pos);   return pnode;            } if (i = = pos && pos = = size) {Pmove->prior->next = NULL;            Free (pmove);            Pmove = NULL;            printf ("%s function execution, delete pos=%d location node succeeded \ n", __function__,pos);        return pnode;        } i++;    Pmove = pmove->next;    } printf ("%s function execution, deletion of node pos=%d location failed \ n", __function__,pos); return pnode;}    Determine if x is present in the linked list, then delete node *deletevaluelist (node *pnode,int x) {node *pmove;    Pmove = Pnode;    int size = Sizelist (Pnode);        The original list is empty if (Pnode = = NULL) {printf ("%s" function is executed, the original list is empty, the node fails \ \ \ \ \ __function__);    return NULL;        }//delete the first node, and the list length is 1 if (pnode->element = = x && size = = 1) {free (pnode);        Pnode = NULL;        printf ("%s function execution, delete value%d node succeeded \ n", __function__,x);    return pnode;        }//Consider deleting the first node separately, with a list length greater than 1 if (pnode->element = = x) {Pnode = pnode->next;        Free (pnode->prior);        Pnode->prior = NULL; printf ("%s" function execution, delete value is%d node succeeded \ n ", __function__,x);    return pnode;            while (pmove! = null) {//the node to be removed 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, delete value%d node succeeded \ n", __function__,x);        return pnode; }//To remove the last node if (pmove->element = = x && pmove->next = = NULL) {pmove->prior-&            Gt;next = NULL;            Free (pmove);            Pmove = NULL;            printf ("%s function execution, delete value%d node succeeded \ n", __function__,x);        return pnode;    } Pmove = pmove->next;    } printf ("%s function execution, delete value%d node failed \ n", __function__,x); return pnode;}



C language implementation of the non-circular doubly linked list node deletion (not the lead junction)

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.