can anyone help me to solve this problem ?
i cannot excute this program.

-----------------------------------------------------------

#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:
    void linklist_1();
    void linklist_2();
    void insFirstNode (string name, int id, string prog, double cgpa);
    void printAllList();

};

void linklist::linklist_1()
{
    head = NULL;
}

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

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;
}


void linklist::insFirstNode(string name, int id,string prog, double cgpa)
{
    node * newNode;
    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
    {
        newNode->link = head -> link;
        head ->link = newNode;
    }
}


int main()
{
    linklist L;
    L.insFirstNode("John",687,"BM",3.0);
    L.insFirstNode("MAX",187,"gM",3.0);
    L.printAllList();

    return 0;
    system("pause");
}

The "head" is not null. So, the check in line 76 fails.

call
L.linklist_1();
You are setting the head as null there.

PS:
you can the above in the constructor. Also, next time, mention what problem you face. This time it is a runtime exception due to illegal memory access.

Try initializing head to something, say to NULL by calling linklist_1() 1st. Or better, initialize head to NULL in the constructor (which you don't have)...

//class
public:
    linklist(){head=NULL;}
    void linklist_1(); //no need;
    void linklist_2();
    void insFirstNode (string name, int id, string prog, double cgpa);
    void printAllList();
//class stuff

and you call it simply as you tried to call 1st:

int main()
{
    linklist L;
    L.insFirstNode("John",687,"BM",3.0);
    L.insFirstNode("MAX",187,"gM",3.0);
    L.printAllList();

    return 0;
}

You program crashes because your head is uninitialized.

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.