I'm beginner. So, I have trouble to understand linked lists and how pointers behave in creating first item or last and so on. Soon as struct of new_type (student) is created (with pointer to itself), and object of that struct, there are three pointers. "first", "new" and "temp". Are they always null pointers (including "link" pointer)? I mean soon as they are declared, I understand that later they have to change adress and/or value. If (!ptr) command says they are. If that's the case always I could better understand principle of further code. In all tutorials and lectures stand that pointer has to be declared as null pointer to become one. Tnx.
#include <iostream>
using namespace std;
typedef struct student {
char* first_name;
char* last_name;
char* smth;
student* link; // is this null pointer??
}student;
student* first, *new; //
student* temp; // are these null pointers?
// here is whole thing... pointers "translated"
#include <iostream>
#pragma GCC diagnostic ignored "-Wwrite-strings"
using namespace std;
typedef struct student {
char* name;
char* last_name;
char* rnmb;
student* next;
} student;
student *poc, *s;
student *temp;
void make_new (char name[10], char last_name[10], char rnmb[5]){
s = new student;
s->name = name;
s->last_name = last_name;
s->rnmb = rnmb;
s->next = NULL;
}
void add_at_b (char name[10], char last_name[10], char rnmb[5]) {
make_new (name, last_name, rnmb);
s->next = poc;
poc = s;
}
void add_at_end (char name[10], char last_name[10], char rnmb[5]) {
make_new (name, last_name, rnmb);
if (!poc) {
poc = s;
} else {
temp = poc;
while (temp->next) temp = temp->next;
temp->next = s;
}
}
void stu_del (char name[10]) {
temp = poc;
while (temp->next) {
if (temp->next->name == name) {
delete temp->next;
temp->next = temp->next->next;
}
temp = temp->next;
}
}
void stu_del_all () {
student *cpy;
temp = poc;
while (temp) {
cpy=temp;
temp=temp->next;
delete cpy;
}
poc = NULL;
}
int main()
{
add_at_b("John", "Doe", "4323");
add_at_end("John jr.", "DoeII", "4323");
add_at_b("Ma", "Mar", "4323");
stu_del("John");
//stu_del_all ();
if (poc == NULL) cout << "List is empty" << endl;
return 0;
}