Error in a Link List
<p>I wrote the following code for a Link list to create a Book its serial no. and search it. I am using linked list in it.
<br>
When I add my first entry , it is added successfully, but when I add second entry it shows segmentation fault. I am not able to figure out why. Please help.<br>Thanks in advance.<br>Code:<br></p>

<pre><code>#include&lt;iostream&gt;
#include&lt;string&gt;
#include&lt;fstream&gt;
#include&lt;cstdlib&gt;
using namespace std;

struct book
{
  int accno;
  string name;
  book* next;
};



int main()

{
  bool flag=false;
  int x,m;
  string s;
  book* front=NULL;
  book* n;
  do
    {

      cout&lt;&lt;"\nPlease select the following:\n1.Create and append\n2.Search\n3.Exit";
      cin&gt;&gt;m;
      switch(m)
    {
    case 1:
      n=new book();
      cout&lt;&lt;"\nEnter the book name: ";
      cin&gt;&gt;s;
      cout&lt;&lt;"\nEnter the acc no.: ";
      cin&gt;&gt;x;

      if(front==NULL)
        {
          front=n;
        }
      else
        {   n=front;

          while(n-&gt;next!=NULL)
        {
          n=n-&gt;next;
        }
          n=n-&gt;next;
        }

      n-&gt;accno=x;
      n-&gt;name=s;
      break;


    case 2:
      cout&lt;&lt;"Enter the roll no.";
      int y;
      cin&gt;&gt;y;
      if(front==NULL){cout&lt;&lt;"Doesnot exist\n"; break;
      }

      else
        {
          n=front;
          while(n-&gt;accno!=y &amp;&amp; n-&gt;next!=NULL)
        {
          n-&gt;next=n;
        }
          cout&lt;&lt;"Book name is:"&lt;&lt;n-&gt;name;
          cout&lt;&lt;"\nAccno is: "&lt;&lt;n-&gt;accno;

        }
      break;


    case 3: flag=true;
      break;


    }
    }

  while(flag==false);

  return 0;
}
</code></pre>
