Good day. I want to clarify. There is a view code (simple sketch):
struct list{ int data; list*pNext; }; list *pHead=NULL; void addHead(int b) { list *ch=new list; ch->data=b; ch->pNext = pHead; pHead = ch; } void display (){ list * current=pHead; while(current) { cout<<current->data; current=current->pNext; } } int main() { addHead(3); display(); return 0; } I can not understand the logic in the list * current=pHead; Why * current points to the 1st element, and not NULL, because in the void addHead(int b) block, pHead is assigned NULL. It seems that everything is written as it should, but infinitely displays only the last element entered. Please help me figure it out.
Update
pHead=ch; pHead pHead=ch; pHead points to the created structure, where it was recorded in the data 3 field, did I understand correctly? In the display () function, current points to a structure, and if there are several calls, will * current point to the last structure or not?
With the assignment of NULL, I was mistaken, you are right.
Update 2
This code is cooked by me. ) Indeed, having included it in the cycle, everything worked, thanks for your attention. ) Then the last question remains, how to dissplay() in the dissplay() function, i.e. *current indicates pHead; which is declared globally and is equal to nal? Hence, *current = NULL , respectively, then comes the check of while(current){...} (if it’s not equal to currenr 0), but it’s equal. I'm confused here. The code was written with an example.