title: C++ : segmentation Fault
body: <p><strong>My C++ code</strong> </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  using namespace std ;
  
  struct Node {
      int data ;
      struct Node* next;
  };
  
  Node* Insert(Node *head,int data);
  
  int main(){
      struct Node* head = NULL;
      struct Node* second = NULL;
      struct Node* third = NULL;
      head = (struct Node*)malloc(sizeof(struct Node));
      second = (struct Node*)malloc(sizeof(struct Node));
      third = (struct Node*)malloc(sizeof(struct Node));
  
      head-&gt;data = 7;
      head-&gt;next = second;
      second-&gt;data = 17;
      second-&gt;next = third;
      third-&gt;data = 37;
      third-&gt;next = NULL;
  
      head = Insert(head,3);
  }
  Node* Insert(Node *head,int data)
  {
    while (true)
      {   cout &lt;&lt; "hey" ;
          if (head == NULL)
          {
              cout &lt;&lt; "hey";
              head-&gt;data = data ;
              head-&gt;next = NULL ;
              break ;
          }
          head = head-&gt;next ;
      }
  
      return head;
  }
  </code></pre>
  
  <p>I am tring to learning Linked List in C++. 
  I am passing a head and data to insert in the node using <code>Insert()</code> function that i have defined. </p>
  
  <p><strong>My output to terminal</strong> </p>
  
  <pre><code>Segmentation fault (core dumped)
  </code></pre>
  
  <p>I think i am invocating <code>Insert()</code> function in a wrong way.
  Please Help ! 
  Thanks !</p>