title: Print statement of all the class instance method are printing at last?
body: <p>I've defined a printList() method inside the class LinkedList which prints out all the data in the linkedlist. After printing all elements of the current list instance it prints out the statement "list printed" . Now after creating three instances of the class LinkedList and calling printList on each one by one, the elements of all three instances printed and then  "list printed" and changes line is printing three time. Can anyone please explain the reason behind this?</p>
  
  <p><strong>Class Definition:</strong></p>
  
  <pre><code> class Node{
      public:
      int data;
      Node* next;
      Node(int data){
          this-&gt;data = data;
          next = NULL;
      }
   };
  
   class LinkedList{
      public:
      Node* head;
      LinkedList(){
          head==NULL;
      }
  
      void push(int data){
          Node* temp = new Node(data);
          temp-&gt;next = head;
          head = temp;
      }
  
      void printList(){
          Node* temp = head;
          while(temp){
              cout&lt;&lt;temp-&gt;data&lt;&lt;" ";
              temp = temp-&gt;next;
          }
          cout&lt;&lt;"list printed"&lt;&lt;endl;
      }
   };
  </code></pre>
  
  <p><strong>Driver Program:</strong></p>
  
  <pre><code>LinkedList ll1;
      ll1.push(7);
      ll1.push(5);
      ll1.push(3);
      ll1.push(1);
      LinkedList ll2;
      ll1.push(8);
      ll1.push(6);
      ll1.push(4);
      ll1.push(2);
      LinkedList ll3;
      ll1.push(11);
      ll1.push(10);
      ll1.push(9);
      ll1.push(0);
      ll1.printList();
      ll2.printList();
      ll3.printList();
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>0 9 10 11 2 4 6 8 1 3 5 7 list printed
  list printed
  list printed
  </code></pre>