Can anyone tell me about this program how the delete tail work ?

.....................................................................

void linklist::deleteSearchList(int id)
{
    node * cur;
    cur = head;

    if (cur == NULL) 
        return;         // no node in list

    if (cur->link == NULL){
        delete cur;
        head = NULL;
    }else if (id == head ->st.id){
                cur = head;
                head = head->link;
                delete cur;
        }else {
        node * p;
        p = head;
        do{
            if( cur->st.id==id )
                break;
            p = cur;
            cur = cur -> link;

        }
        while (cur !=NULL);

        p->link = cur->link;
        delete cur;
    }
}

Didn't you write it? Why would you need someone to explain to you how your own code works?

Your while is kinda wrong:

while (cur !=NULL);

Your code chrashes the program:

            do{
                if( cur->st.id==id )
                    break;
                p = cur;
                cur = cur -> link;
            }
            while (cur !=NULL);
            //after it finishes the while
            p->link = cur->link; //link is undefined if cur = NULL. NULL doesn't have a "link"...
            delete cur; //deletes NULL, but wait: it will crash at the instruction above.

it should be:

while (cur != NULL){
    if( cur->st.id==id )
    break;
    p = cur;
    cur = cur -> link;  
}
//after it finishes the while
p->link = NULL;
delete cur;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.