yack yack - 3 months ago 16
C++ Question

Linked list of pointers C++

I have a list, but now I have to link it.

Here is my program ( I deleted code inside functions, to make my program more easy to read ).

#include <iostream>

using namespace std;

struct Student
{
char ime[16];
char priimek[16];
char vpisna[10];
char ocenaRV[10];
char ocenaDN[10];
char ocenaKV[10];
char ocenaVI[10];
Student *next;
};

void clean(Student* pointer,int x) // Delete random data
{

}

void dodajanje(int x,Student* s) // Add information about student
{

}

void brisi(Student* pointer,int x) // Delete information about student
{

}

int main()
{
int student,mesto, brisanje, ali = 0;
cout << "Number of students?." << endl;
cin >> student;

Student* s = new Student[student];

clean(s,student);

cout << endl;
cout << "Add student to i place in array." << endl;
cin >> mesto;
dodajanje( mesto, s );

for(int i=0;i<(student*2);i++)
{
cout << "add student = 1, delete student = 2, cout information = 3"<<endl;
cin>>ali;
if (ali == 1)
{
cout << endl;
cout << "Add student to i place in array." << endl;
cin >> mesto;
dodajanje( mesto, s );
}
else if (ali == 2)
{
cout << "delete student on i place ?" << endl;
cin >> brisanje;
brisi(s,brisanje);
}
else
{
break;
}
}
delete[] s;

return 0;
}


Can someone explain me how to link my list, because code in all tutorials I came across was similar to this:

Node* temp = Node();


But in my program my code is:

Student* s = new Student[student];


And now I'm lost;

Note: I have to create dynamically linked list.

Answer

Node* temp = Node();

This creates a single Node instance. Though it should be this instead:

Node* temp = new Node;

Student* s = new Student[student];

This creates an array of student number of Student instances. This defeats the purpose of a linked list , as you won't be able to add/remove Student instances from the array efficiently. But, just for the sake of argument, lets say you really need an array. You can "link" the Student instances together like this:

for (int i = 0; i < (student-1); i++)
    s[i].next = &s[i+1];
s[student-1].next = NULL;

If you actually need a linked list then you need something more like this instead:

Student *studentList = NULL;

Student *lastStudent = NULL;
for (int i = 0; i < student; ++i)
{
    Student* s = new Student;
    s->next = NULL;
    if (lastStudent) lastStudent->next = s;
    if (!studentList) studentList = s;
    lastStudent = s;
}

// use studentList as needed...

Student *s = studentList;
while (s)
{
    Student *next = s->next;
    delete s;
    s = next;
}

After fixing that, consider using the STL std::list class instead, or even std::forward_list in C++11.

That being said, you also need to rethink your code design. A linked list grows and shrinks dynamically, so there is no point in asking the user for the number of students up front, or pre-allocating the list with garbage that has to be cleaned before it can be used. Change your loop to run forever (or at least until the user says to stop). On each iteration, ask the user what to do. If Add, add a new Student to the list at that time. If Delete, ask the user which student to delete, find that Student, unlink it, and delete it. If Display, ask the user which student to display, find that Student, and display it. And so on.

Comments