title: Basic C++ programming question
body: <p>I am in process of learning c++. I am working on creating a linkedlist data structure. One of the functions that displays the values of nodes in the structure does not work. For some reason the while loop that traverses through nodes doesn't work in the display function, hence I can't see the values in those nodes. Does anyone see what the problem is? I've been staring at the code for a while and not sure what is wrong here.
  Thanks for your help in advance.
  Header:</p>
  
  <pre><code>// linklist.h
  // class definitions
  
  
  #ifndef LINKLIST_H
  #define LINKLIST_H
  
  class linklist
   {
   private:
       // structure containing a data part and link part
       struct node
       {
           int data;
           node *link;
       }*p;
  
   public:
  
       linklist();
       void append(int num);
       void addatbeg(int num);
       void addafter(int loc, int num);
       void display();
       int count();
       void del(int num);
       ~linklist();
   };
  
  #endif
  
  .cpp file
  
  // LinkedListLecture.cpp
  // Class LinkedList implementation
  
      #include"linklist.h"
      #include&lt;iostream&gt;
  
  using namespace std;
  
  
       // initializes data member
       linklist::linklist()
       {
           p =NULL;
       }
  
       // adds a node at the end of a linked list
       void linklist::append(int num)
       {
            node *temp, *r;
           // if the list is empty, create first node
           if(p==NULL)
           {
                   temp = new node;
               temp-&gt;data = num;
               temp-&gt;link = NULL;      
           }
           else
           {
               // go to last node
               temp = p;
               while(temp-&gt;link!=NULL)
                   temp =  temp-&gt;link;
               // add node at the end
               r = new node;
               r-&gt;data=num;
               r-&gt;link=NULL;
               temp-&gt;link=r;
           }
       }
  
  
  
   // displays the contents of the linked list
   void linklist::display()
   {
       node *temp = p;
       cout&lt;&lt; endl;
       // traverse the entire linked list
       while(temp!=NULL) // DEBUG: the loop doesn't work
       {
           cout&lt;&lt;temp-&gt;data&lt;&lt;" ";
           temp = temp-&gt;link;
       }
  
       void main() 
   {
       linklist l;
  
       l.append(14);
       l.append(30);
       l.append(25);
       l.append(42);
       l.append(17);
       cout&lt;&lt;"Elements in the linked list:";
       l.display(); // this function doesn't work
       system("PAUSE");
   }
  </code></pre>