Linked list of pointers C++ -


i have list, have link it.
here program ( deleted code inside functions, make program more easy 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 student {  }  void brisi(student* pointer,int x) // delete information 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 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 place in array." << endl;             cin >> mesto;             dodajanje( mesto, s );           }         else if (ali == 2)         {             cout << "delete student on place ?" << endl;             cin >> brisanje;             brisi(s,brisanje);         }         else         {             break;         }     }     delete[] s;        return 0; } 

can explain me how link list, because code in tutorials came across similar this:

node* temp = node(); 

but in program code is:

student* s = new student[student]; 

and i'm lost;

note: have create dynamically linked list.

node* temp = node();

this creates single node instance. though should instead:

node* temp = new node; 

student* s = new student[student];

this creates array of student number of student instances. defeats purpose of linked list , won't able add/remove student instances array efficiently. but, sake of argument, lets need array. can "link" student instances this:

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

if need linked list need more instead:

student *studentlist = null;  student *laststudent = null; (int = 0; < student; ++i) {     student* s = new student;     s->next = null;     if (laststudent) laststudent->next = s;     if (!studentlist) studentlist = s;     laststudent = s; }  // use studentlist needed...  student *s = studentlist; while (s) {     student *next = s->next;     delete s;     s = next; } 

after fixing that, consider using stl std::list class instead, or std::forward_list in c++11.

that being said, need rethink code design. linked list grows , shrinks dynamically, there no point in asking user number of students front, or pre-allocating list garbage has cleaned before can used. change loop run forever (or @ least until user says stop). on each iteration, ask user do. if add, add new student list @ time. if delete, ask user student delete, find student, unlink it, , delete it. if display, ask user student display, find student, , display it. , on.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -