Linked list program facing segmentation fault
<p>I was writing a C++ program to implement a linked list. On compilation it's not giving any error but in the output windows it goes blank and program ended with</p>

<blockquote>
  <p>list1.exe has 
  encountered a problem and needs to close.</p>
</blockquote>

<p>Debugger response: Program received signal SIGSEGV, Segmentation fault.</p>

<p>Maybe it's because of memory leakage, but I'm not able to figure out the exact bug and how can we fix that. Please what's wrong in the prog and what should be fixed?</p>

<p>Below is the code</p>

<pre><code>  //Program to implement linked list

  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;

  using namespace std;

  class Node
  {
      int data;
      Node * next;

   public:
      Node (){}
      int getdata(){return data ;}
      void setdata(int a){data=a;}
      void setnext(Node* c){next=c;}
      Node* getnext(){return next;}
  };

  class linkedlist
  {
      Node* head;

  public:
      linkedlist(){head=NULL;}
      void print ();
      void push_back(int data);
  };

  void linkedlist::push_back(int data)
  {
      Node* newnode= new Node();
      if(newnode!=NULL)
      {
          newnode-&gt;setdata(data);
          newnode-&gt;setnext(NULL);
      }
      Node* ptr= head;

      if(ptr==NULL) 
          {head=newnode;}
      while ((ptr-&gt;getnext())!=NULL)
      {
          ptr=ptr-&gt;getnext();
      }
      ptr-&gt;setnext(newnode);
  }

  void linkedlist::print()
  {
      Node* ptr=head;
      if(ptr==NULL)
          {cout&lt;&lt;"null"; return;}

      while(ptr!=NULL)
      {
          cout&lt;&lt;(ptr-&gt;getdata())&lt;&lt;" ";
          ptr=ptr-&gt;getnext();
      }
  }

  int main()
  {
     linkedlist list;
      list.push_back(30);
      list.push_back(35);
      list.print();
      return 0;
  }
</code></pre>
