can anyone show me how to convert this linkList statement into Array ?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

public:

    void insFirstNode (string name, int id, string prog, double cgpa);

};

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)               //insert first node
        head = newNode;
    else
    {
        p = head;

        while ((p->link !=NULL))        // link to the next node
            p = p->link;

        p->link = newNode;              //insert to the next node
    }
}

First and foremost, I think insFirstNode() is poorly named. It implies that the node will be prepended to the list in all cases, but in reality what it does is append.

As far as converting the list to an array, it's simplicity itself. But a few things will need to change for the code to make sense. You can't really call the class linkedlist anymore. Since it's only a collection of students, why not student_collection? From there just keep an index for the most recently appended item, and increment it when you add a new student:

struct student {
    string name;
    int id;
    string prog;
    double cgpa;
};

class student_collection {
private:
    student _students[50];
    int last;
public:
    student_collection(): last(0) {}

    void add(string name, int id, string prog, double cgpa);

    //...
};

void student_collection::add(string name, int id,string prog, double cgpa)
{
    if (last == sizeof _students / sizeof *_students) {
        throw "Oh noes!";
    }

    _students[last].name = name;
    _students[last].id = id;
    _students[last].prog = prog;
    _students[last].cgpa = cgpa;

    ++last;
}

Of course now you have to concern yourself over reaching the end of the array, and the size of the array on the stack becoming a problem for a large number of students. Ideally you'd dump arrays entirely in favor of std::vector, which handles dynamic allocation of memory on the heap and will adjust its size accordingly.

commented: thanks +1

how about this statement

if (last == sizeof _students / sizeof *_students) {

change to

if (last == students ) {

also can ?

If you define students as an integer that matches the size of the array, sure. But within the given code, it'll fail with a syntax error because students is undefined.

is there any method to replace this statement ????

 if (last == sizeof _students / sizeof *_students) {
throw "Oh noes!";
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.