The C language realizes the deletion of the Single Link table node (does not take the lead node)

Source: Internet
Author: User

It is the most basic operation to make the deletion of the single linked list node, this blog will be implemented to delete the node. Other operations can refer to the "C language to achieve the basic operation of the list" this blog. There are two types of deletes for a node:

(1) Delete a node of the I location;

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

The instance code has been uploaded to Https://github.com/chenyufeng1991/DeleteLinkedList.

The core code is as follows:

Deletes node *deleteposelement (nodes *pnode,int POS) of POS location {//requires a header node to maintain node *phead;
    Node *pmove;
    int i = 1;
        if (pos <= 0 | | | pos > sizelist (pnode)) {printf ("%s function execution, enter POS value illegal, delete node failed \ n", __function__);
    return NULL;
    } phead = Pnode;
    Pmove = Pnode;
        Consider deleting the first node individually if (pos = 1) {Pmove = pmove->next;
        Pnode = Pmove;

        Free (phead);
        printf ("%s function execution, delete Pos=1 location element succeeded \ \", __function__);
    return pnode;
        while (Pmove!= NULL) {if (i = = pos-1) {break;
        } i++;
    Pmove = pmove->next;
    Free (pmove->next);

    Pmove->next = pmove->next->next;

    printf ("%s function execution, delete pos=%d location element succeeded \ \", __function__,pos);
return pnode; //Determine if the X value is in the list, and if it exists delete the node node *deletexelement (node *pnode,int x) {//One of the last two pointers, Pmovepre is Pmove's previous node *pmovepre
    ;

    Node *pmove; if (Pnode = = NULL) {printf ("%s function execution, linked list is empty, delete x=%d failed \ \", __functiON__,X);
    return NULL;
    } pmovepre = Pnode;

    Pmove = pmovepre->next;
        Consider the first node if (pmovepre->element = = x) {pnode = Pmove separately;
        Free (PMOVEPRE);
    return pnode; while (Pmove!= NULL) {if (pmove->element = x) {//Find the previous node of the node Pmovepre->ne
            XT = pmove->next;
            Free (pmove);
        Break
        }//sync forward pmove = pmove->next;
    Pmovepre = pmovepre->next;
        } if (Pmove = NULL) {printf ("%s function execution, no x=%d, delete data failed \ n", __function__,x);
    return pnode;
    printf ("%s function execution, delete x=%d succeeded \ \", __function__,x);
return pnode;

 }


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.