title: Implementing a double linked list in C++, pointers and odd values
body: <p>I'm pretty rusty in C++ and I'm trying to implement a double linked list but I am having some reading violations and having some odd values being given.</p>
  <pre><code>    #include &lt;iostream&gt;
  
  struct Node
      {
        int val;
        Node* next;
        Node* prev;
  };
  
  class linkedList
  {
    public:
      linkedList(); //constructor
      ~linkedList(); //destructor
      void push_back(int x);
      void addtofront(int x);
      //void deleteNode(int x);
      bool isempty();
      void firstelem();
      void prnt_tail();
      /*void insert_after(int x, int y);
      void insert_before(int x, int y);*/
    private:
      
      Node* head;
      Node* next;
      Node* prev;
  
  
  };
  
  
  linkedList::linkedList(){};
  
  
  linkedList::~linkedList(){};
  
  
  void linkedList::push_back(int x)
  {
      linkedList* list = this;
      Node temp;
      temp.val=x;
      temp.next=NULL;
      temp.prev=NULL;
      if (!head)
    {
      linkedList* list = new linkedList();
      list-&gt;head;
      head = new Node;
      head-&gt;val = x;
      head-&gt;next = NULL;
      head-&gt;prev = NULL;
    }
    else
    {
      Node* temp1;
      temp1=head;
      while (temp1-&gt;next!=NULL)
      {
          temp1 = temp1-&gt;next;
      }
  
      temp.next= NULL; 
      temp.prev=temp1;
      temp.val = x;
  
    }
  };
  
  void linkedList::addtofront(int x)
  {
      linkedList* list = this;
      Node temp;
      temp.val=x;
      temp.next=NULL;
      temp.prev=NULL;
      if (!head)
    {
      linkedList* list = new linkedList();
      list-&gt;head;
      head = new Node;
      head-&gt;val = x;
      head-&gt;next = NULL;
      head-&gt;prev = NULL;
    }
      else
      {
          list-&gt;head-&gt;prev=&amp;temp;
          temp.next=head;
          head=&amp;temp;
      }
  };
  
  //void linkedList::deleteNode(int x)
  //{
  //  if(head)
  //  {
  //      linkedList *ptr = head;
  //      while(ptr-&gt;node.val != x)
  //      {
  //          ptr = ptr-&gt;node.next;
  //      }
  //      (ptr-&gt;node.next)-&gt;prev=ptr-&gt;node.prev;
  //      ptr-&gt;node.prev=ptr-&gt;node.next;
  //      delete ptr;
  //  }
  //  else
  //      std::cout&lt;&lt;&quot;empty list&quot;;
  //}
  
  
  bool linkedList::isempty()
  {
      if(head)
          return false;
      else return true;
  };
  
  
  void linkedList::firstelem()
  {
      std::cout&lt;&lt;head-&gt;val;
  };
  
  
  void linkedList::prnt_tail()
  {
      if(head)
      {
          Node *temp;
          temp=head;
          temp=head-&gt;next;
          std::cout&lt;&lt;temp;
          while(temp-&gt;next!=NULL)
          {
              std::cout&lt;&lt;temp-&gt;val&lt;&lt;&quot; &quot;;
          }
          std::cout&lt;&lt;temp-&gt;val;
      }
      else
      {
          std::cout&lt;&lt;&quot;empty list&quot;;
      }
      
  };
  
  
  //linkedList::insert_after(int x, int y)
  //{
  //
  //}
  //
  //linkedList::insert_before(int x, int y)
  //{
  //
  //}
  </code></pre>
  <p>and my main</p>
  <pre><code>#include &quot;linkedlist2.h&quot;
  #include &lt;stdlib.h&gt;
  #include &lt;iostream&gt;
  
  
  
  int main()
  {
      linkedList example;
      if(example.isempty())
          std::cout&lt;&lt;&quot;this list is empty &quot;&lt;&lt;&quot;\n&quot;;
      else
          std::cout&lt;&lt;&quot;this list is not empty&quot;&lt;&lt;&quot;\n&quot;;
      for (int i = 1; i&lt;=20; i++)
      {
          example.push_back(i);
          //example.prnt_tail();
      }
      example.addtofront(25);
      example.firstelem();
      std::cout&lt;&lt;&quot;\n&quot;;
      example.addtofront(28);
      example.firstelem();
      std::cout&lt;&lt;&quot;\n&quot;;
      if(example.isempty())
          std::cout&lt;&lt;&quot;this list is empty &quot;&lt;&lt;&quot;\n&quot;;
      else
          std::cout&lt;&lt;&quot;this list is not empty&quot;&lt;&lt;&quot;\n&quot;;
      
      //example.push_back(26);
      //std::cout&lt;&lt;example.head-&gt;next-&gt;val;
      example.firstelem();
      std::cout&lt;&lt;&quot;\n&quot;;
      example.prnt_tail();
      std::cout&lt;&lt;&quot;\n&quot;;
      system(&quot;pause&quot;);
  }
  </code></pre>
  <p>when I run main I get</p>
  <blockquote>
  <p>this list is empty</p>
  <p>-858993460</p>
  <p>-858993460</p>
  <p>this list is not empty</p>
  <p>-858993460</p>
  <p>CCCCCCCC</p>
  </blockquote>
  <p>I also get the error</p>
  <blockquote>
  <p>Access violation reading location 0xCCCCCCD0.</p>
  </blockquote>
  <p>and the next statement to be executed is the while loop in &quot;void linkedList::prnt_tail()&quot;</p>
  <p>I'm fairly sure my problem is in my pointers and all that. Like I said, I'm really rusty so any help you can give would be greatly appreciated, even in things not directly related to my problems.</p>