We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,344 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Problem linklist delete head node

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
Contributors
2
Replies
29 Minutes
Discussion Span
4 Months Ago
Last Updated
4
Views
Question
Answered
on93
Junior Poster in Training
54 posts since Jul 2012
Reputation Points: -7
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 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.

deceptikon
Challenge Accepted
Administrator
3,456 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 57

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

on93
Junior Poster in Training
54 posts since Jul 2012
Reputation Points: -7
Solved Threads: 0
Skill Endorsements: 0
Infraction Points: 2
Question Answered as of 4 Months Ago by deceptikon

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0834 seconds using 2.74MB