title: One-way linked list in C++
body: <p>I started working on algorithms and data structures. I decided to have a try with one-way linked list. It contains functions like inserting element after chosen element, deleting element with given value and returning an amount of deleted items, function returning current content of list and class to write and read on/from files. There's a small problem with fuction which has to add new element. The debugger returns a following error: </p>
  
  <p>First-chance exception at 0x00e71ea2 in AiSD.exe: 0xC0000005: Access violation reading location 0x00000000.</p>
  
  <p>Unhandled exception at 0x00e71ea2 in AiSD.exe: 0xC0000005: Access violation reading location 0x00000000.</p>
  
  <p>Looks like a problem with pointer but I have no idea how to solve it. It's not my speciality right now... Yet! So I asked you to help me. There is a code:
  <i></p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;string&gt;
  using namespace std;
  
  struct Node
  {
      Node *next;
      int key;
  };
  
  class List
  {
  public:
      Node *head;
      unsigned int counter;
  
      List()
      {
          head = NULL;
          counter = 0;
      }
  
      unsigned size(Node *p)
      {
          unsigned int c=0;
  
          while(p)
          {
              c++;
              p = p-&gt;next;
          }
          return c;
      }
  
  
      void Addafter(Node *e, int v)
      {
      Node *p = new Node;
      p-&gt;next = e-&gt;next; //there's a problem
      p-&gt;key = v;
      e-&gt;next = p;
      }   
  
      int Delete(int v)
      {
          Node *p = new Node;
          int count = 0;
          while(p-&gt;next != NULL)
          {
              if(p-&gt;key = v)
              {
                  delete p;
                  count++;
                  return count;
              }
          }
      }
      void Write()
      {
          string data;
          fstream file;
          file.open("D:\\plik2.txt", ios::out);
          Node *p = head;
          while(p-&gt;next != NULL)  
          {
              p = p-&gt;next;
              int wartosc = p-&gt;key;
              file&lt;&lt;wartosc;
          }
          file.close();
      }
  
      void Read()
      {
          string data;
          fstream file;
          file.open("D:\\plik.txt", ios::in);
          Node *p = head;
          for(int i=0; i&lt;counter; i++)
          {
              getline(file, data);
              int result = atoi(data.c_str());
              p-&gt;key = result;
              p = p-&gt;next;
          }
          file.close();
      }
  
  
  };
  
  int main()
  {
      List a;
      a.Read();
      a.Addafter(a.head, 3);
      a.Delete(5);
      a.size(a.head);
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <p></i></p>