[Solved] While loop is executing code I don’t understand


// L is a data structure also known as a linked list. It has:
struct L
{
  int d; // a value
  L *p;  // a pointer to the next element
}

// it creates the first element of the linked list, and assigns it a value of 3
L* l = new L;
l->d = 3;

// then makes it point to a new element, with value 5
l->p = new L;
l->p->d = 5;

// the new element's pointer to the next element is then set to be 
// the first item in the list, hence creating a circular list of 2 elements
// .->| 3 |-->| 5 | -.
// '-----------------'
l->p->p = l;

// then it loops through the list, printing the value of the current element 
// and increasing it to 1 every time, and stopping when the value of the current 
// element is 7 or more
while(l->d < 7) {
    cout << l->d++ << ' '; // prints the value of the current element and increases it
    l = l->p; // moves to the next node
}

Here’s a very good article that can help you understand what a linked list is and how to properly use it: https://www.learn-c.org/en/Linked_lists

I hope this was clear enough!

1

solved While loop is executing code I don’t understand