1.11M Members

Problem linklist delete head node

 
0
 

can anyone help me to fix this problem ?

i can delete the midder and the tail of the node , the only problem is i cannot delete the head node when i have more than one node.

#include<iostream>
#include<string>

using namespace std;

class linklist
{
private:
    struct node{
        struct student{
            string name;
            int id;
            string prog;
            double cgpa;
        }st;
        node *link;
    }*head;

public:
    linklist();
    void linklist_2();
    void insFirstNode (string name, int id, string prog, double cgpa);
    void printAllList();
    void deleteSearchList(int id);

};

linklist::linklist(){
    head = NULL;
}
void linklist::linklist_2()
{
    node *p;
    if (head == NULL)
        return ;
    while (head !=NULL)
    {
        p = head -> link ;
        delete head;
        head = p;
    }
}


void linklist::insFirstNode(string name, int id,string prog, double cgpa)
{
    node * newNode,*p;
    newNode = new node;
    newNode -> st.name = name;
    newNode -> st.id = id;
    newNode -> st.prog = prog;
    newNode -> st.cgpa = cgpa;
    newNode -> link = NULL;

    if (head == NULL)
        head = newNode;
    else
    {
        p = head;

        while ((p->link !=NULL))
            p = p->link;
        p->link = newNode;
    }
}

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

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

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

        }
        while (tmp !=NULL);

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




void linklist :: printAllList()
{
    node * cur;
    cur = head;
    cout<<" { ";

    while (cur !=NULL)
    {
        cout<<cur ->st.name<<" ";
        cout<<cur ->st.id<<" ";
        cout<<cur ->st.prog<<" ";
        cout<<cur ->st.cgpa<<" ";

        cur = cur->link;
        if (cur != NULL)
            cout<<", ";
    }
    cout<<" } "<<endl;
}



int main()
{
    char ans;
    string name1,prog1;
    int id1;
    double cgpa1;

    linklist L;

    do{
    cout<<"A.) Insert new student"<<endl
        <<"B.) Delete student info"<<endl
        <<"C.) Print out ALL student info" <<endl

    cout<<"Pls choice one option : ";
    cin>>ans;

    switch (ans)
    {
    case 'a':
    case 'A':

        cout<<"student name : "<<endl;
        cin>>name1;
        cout<<"ID: "<<endl;
        cin>>id1;

        cout<<"prog : "<<endl;
        cin>>prog1;

        cout<<"CGPA: "<<endl;
        cin>>cgpa1;

        L.insFirstNode (name1,id1,prog1,cgpa1);
        L.printAllList();

        break;

    case 'b':
    case 'B':
        cout <<"pls enter Student ID tat wan delete form node:";
        cin >> id1;

        L.deleteSearchList(id1);

        break;

    case 'c':
    case 'C':
        L.printAllList();

        break;


    case 'f':
    case 'F':
        break;
    default :
        cout<<"enter again"<<endl;

    }


    }while (toupper(ans) !='F');

        cout<<"exit the loop"<<endl;




    system("pause");
    return 0;

}
 
2
 

In lines 75-79, you unconditionally delete the head if it's the only node in the list. What if id != head->st.id in that case? As for successfully deleting the first node when it matches the id, consider adding another clause between lines 79 and 80 that handles a true deletion of a matching head:

else if (id == head->st.id)
{
    tmp = head;
    head = head->link;
    delete tmp;
}

This will reseat the head to the 2nd node in the list, and then release memory for the old head.

 
0
 

oic ....
tat's why i cannot delete head node .
thanks ya .

Question Answered as of 1 Year Ago by deceptikon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article