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.

    2 answers 2

    What you wrote is called a stack. The same kind of list.

    I still do not understand, but what you do not like here?

    If you want to build a list by adding elements to the tail, then use 2 pointers - head and tail (for a list of one element tail == head).

    By the way, there has just been a similar question , the truth about a doubly linked list, but in general everything is the same. Read.

    Update

    The meaning of the line:

     list * current=pHead; 

    extremely simple.

    The function defines a local variable named current and its initial value becomes the address of the first element of the list. This value is taken from the global variable pHead .


    Nothing like: "in the future can be allocated " there is no trace ...


    If the list is empty, then pHead = 0 . The last element of the list also has a null pointer. Therefore, such a while correctly iterates through all the elements of the list.

    Update 2

    @dil, take a debugger (or a pencil and a piece of paper) and look at the program step by step.


    In fact, at the beginning of main() pHead=0 . After the first call to addHead() it will be equal, say 100000 (to the result of new ). And pNext in the structure at this address will be 0.

    After the second addHead() pHead will be, for example, 100016, and pNext in this structure will be 100000 (that is, the previous value of pHead ).

    Etc.

    pHead will always point to the first in order (but last in the sense of creation time (just created)) list item (aka the stack in this implementation).

      in the void addHead (int b) block, pHead is set to NULL

      In addHead, pHead assigned to ch , actually.

      No, she is assigned a new list . pNext is an element of the structure in which the pointer to the neighboring node is stored, since This is the head, there is simply no neighboring node.

      I studied sishka a long time ago, and quite superficially, but I would recommend to write this code myself most likely, because the given example is very, uh, strange. The display will have an infinite loop, because The last line must go into the loop for the condition to change. If the cycle problem is solved, and there will be several calls, then each call will be treated the same way - look carefully at the first line.

      Update

      I studied sishka a long time ago, and quite superficially, but I would recommend to write this code myself most likely, because the given example is very, uh, strange. The display will have an infinite loop, because The last line must go into the loop for the condition to change. If the cycle problem is solved, and there will be several calls, then each call will be treated the same way - look carefully at the first line.

      Update 2

      After calling addHead() pHead no longer equal to a null pointer.

      N update

      @dil , this is your code, as you wrote it - so it will be executed) But in general, the meaning of addHead is clearly to create this very head, and the elements are added to the list in some other way. Those. if you add a million records, pHead will still point to the head, and the countdown will also start from there.