Think about it: head- this is a pointer to the first element in your list, and the first thing you do in AddToList():
head = new PersonRec();
What do you think will happen to the current list when overwriting headthis way?
Do not change headuntil you are ready. The main pseudo-code will be:
newnode = new PersonRec;
newnode-> next = head
head = newnode
, node . , , :
: , , - .
, , AddToList(), , , . head , isEmpty() - true. , , head = new ... / if. , .
, :
if isEmpty:
head = new PersonRec;
p = head
else:
p = head
while p->next != NULL:
p = p->next
p->next = new PersonRec;
p = p->next
p-> payload/link = whatever/null
, , , (.. , node ).
- ():
void PersonList::AddToList() {
PersonRec *p;
if(!IsEmpty()) {
p = head = new PersonRec();
} else {
p = head;
while (p->link != NULL)
p = p->link;
p->link = new PersonRec();
p = p->link;
}
cout << "Enter the person name: ";
cin.getline (p->aName, 20);
cout << "Enter the person contribution: ";
cin >> p->aBribe;
p->link = NULL;
}