title: Using a public class function to initialize a nodeType<Type> pointer C++
body: <p>I posted a similar question about this, but I'm asking again, because I was wrong in my attempt. So it's a completely different question now.<br/><br/>
  This is part of a school project about a linked list of Payroll objects. The feedback from my Professor stated that I need to traverse the linked list from <strong> inside</strong> the InList function. I understand what he's saying, but not how to do it.<br/><br/>
  In the InList function, I pass a linked list and an int, create the Payroll object and assign the int as the employee number variable. Then using the nodeType current pointer, traverse the list and print the record if found. <br/><br/> </p>
  
  <pre><code>    void InList(const orderedLinkedList&lt;Payroll&gt;&amp; P, int employee_number)
      {
      int listCount = P.length();
      Payroll object;
      object.setEmployeeNumber(employee_number);
      nodeType&lt;Payroll&gt; *current; //how to initialize this?
      if (P.isEmptyList())
          cout &lt;&lt; "Sorry, there is nothing in the list yet." &lt;&lt; endl;
      else
      {
          while (current != NULL &amp;&amp; listCount &gt; 0)
          {
              if (current-&gt;info.getEmployeeNumber() == employee_number)
              {
                  current-&gt;info.printPayroll();
              }
              listCount--;
              current = current-&gt;link;
          } //end while
      } //end else
      }
  </code></pre>
  
  <p>And here are all the functions for the linked list. Please note, that this header file does not have any member variables, and is derived from a virtual base class that has the first and last pointers. Also note, that first and last are protected in the base class.</p>
  
  <pre><code>template &lt;class Type&gt;
  bool orderedLinkedList&lt;Type&gt;::search(const Type&amp; searchItem) const
  {
      bool found = false;
      nodeType&lt;Type&gt; *current; //pointer to traverse the list
  
      current = first;  //start the search at the first node
  
      while (current != NULL &amp;&amp; !found)
          if (current-&gt;info &gt;= searchItem)
              found = true;
          else
              current = current-&gt;link;
  
      if (found)
          found = (current-&gt;info == searchItem); //test for equality
  
      return found;
  }//end search
  
  template &lt;class Type&gt;
  void orderedLinkedList&lt;Type&gt;::insert(const Type&amp; newItem)
  {
      nodeType&lt;Type&gt; *current; //pointer to traverse the list
      nodeType&lt;Type&gt; *trailCurrent = NULL;//pointer just before current
      nodeType&lt;Type&gt; *newNode;  //pointer to create a node
  
      bool  found;
  
      newNode = new nodeType&lt;Type&gt;; //create the node
      newNode-&gt;info = newItem;   //store newItem in the node
      newNode-&gt;link = NULL;      //set the link field of the node
                                 //to NULL
  
      if (first == NULL)  //Case 1
      {
          first = newNode;
          last = newNode;
          count++;
      }
      else
      {
          current = first;
          found = false;
  
          while (current != NULL &amp;&amp; !found) //search the list
              if (current-&gt;info &gt;= newItem)
                  found = true;
              else
              {
                  trailCurrent = current;
                  current = current-&gt;link;
              }
  
          if (current == first)      //Case 2
          {
              newNode-&gt;link = first;
              first = newNode;
              count++;
          }
          else                       //Case 3
          {
              trailCurrent-&gt;link = newNode;
              newNode-&gt;link = current;
  
              if (current == NULL)
                  last = newNode;
  
              count++;
          }
      }//end else
  }//end insert
  
  template&lt;class Type&gt;
  void orderedLinkedList&lt;Type&gt;::insertFirst(const Type&amp; newItem)
  {
      insert(newItem);
  }//end insertFirst
  
  template&lt;class Type&gt;
  void orderedLinkedList&lt;Type&gt;::insertLast(const Type&amp; newItem)
  {
      insert(newItem);
  }//end insertLast
  
  template&lt;class Type&gt;
  void orderedLinkedList&lt;Type&gt;::deleteNode(const Type&amp; deleteItem)
  {
      nodeType&lt;Type&gt; *current; //pointer to traverse the list
      nodeType&lt;Type&gt; *trailCurrent = NULL; //pointer just before current
      bool found;
  
      if (first == NULL) //Case 1
          cout &lt;&lt; "Cannot delete from an empty list." &lt;&lt; endl;
      else
      {
          current = first;
          found = false;
  
          while (current != NULL &amp;&amp; !found)  //search the list
              if (current-&gt;info &gt;= deleteItem)
                  found = true;
              else
              {
                  trailCurrent = current;
                  current = current-&gt;link;
              }
  
          if (current == NULL)   //Case 4
              cout &lt;&lt; "The item to be deleted is not in the "
              &lt;&lt; "list." &lt;&lt; endl;
          else
              if (current-&gt;info == deleteItem) //the item to be 
                                               //deleted is in the list
              {
                  if (first == current)       //Case 2
                  {
                      first = first-&gt;link;
  
                      if (first == NULL)
                          last = NULL;
  
                      delete current;
                  }
                  else                         //Case 3
                  {
                      trailCurrent-&gt;link = current-&gt;link;
  
                      if (current == last)
                          last = trailCurrent;
  
                      delete current;
                  }
                  count--;
              }
              else                            //Case 4
                  cout &lt;&lt; "The item to be deleted is not in the "
                  &lt;&lt; "list." &lt;&lt; endl;
      }
  }//end deleteNode
  </code></pre>
  
  <p>And here is the virtual base class:</p>
  
  <pre><code>#pragma once
  #ifndef H_LinkedListType
  #define H_LinkedListType
  #include &lt;iostream&gt;
  #include &lt;cassert&gt;
  using namespace std;
  //Definition of the node
  template &lt;class Type&gt;
  struct nodeType
  {
      Type info;
      nodeType&lt;Type&gt; *link;
  };
  template &lt;class Type&gt;
  class linkedListIterator
  {
  public:
      linkedListIterator();
      linkedListIterator(nodeType&lt;Type&gt; *ptr);
      Type operator*();
      linkedListIterator&lt;Type&gt; operator++();
      bool operator==(const linkedListIterator&lt;Type&gt;&amp; right) const;
      bool operator!=(const linkedListIterator&lt;Type&gt;&amp; right) const;
  private:
      nodeType&lt;Type&gt; *current; //pointer to point to the current 
  };
  template &lt;class Type&gt;
  linkedListIterator&lt;Type&gt;::linkedListIterator()
  {
      current = NULL;
  }
  template &lt;class Type&gt;
  linkedListIterator&lt;Type&gt;::
  linkedListIterator(nodeType&lt;Type&gt; *ptr)
  {
      current = ptr;
  }
  template &lt;class Type&gt;
  Type linkedListIterator&lt;Type&gt;::operator*()
  {
      return current-&gt;info;
  }
  template &lt;class Type&gt;
  linkedListIterator&lt;Type&gt; linkedListIterator&lt;Type&gt;::operator++()
  {
      current = current-&gt;link;
      return *this;
  }
  template &lt;class Type&gt;
  bool linkedListIterator&lt;Type&gt;::operator==
  (const linkedListIterator&lt;Type&gt;&amp; right) const
  {
      return (current == right.current);
  }
  template &lt;class Type&gt;
  bool linkedListIterator&lt;Type&gt;::operator!=
  (const linkedListIterator&lt;Type&gt;&amp; right) const
  {
      return (current != right.current);
  }
  //*****************  class linkedListType   ****************
  template &lt;class Type&gt;
  class linkedListType
  {
  public:
      const linkedListType&lt;Type&gt;&amp; operator=
          (const linkedListType&lt;Type&gt;&amp;);
      void initializeList();
      bool isEmptyList() const;
      void print() const;
      int length() const;
      void destroyList();
      Type front() const;
      Type back() const;
      virtual bool search(const Type&amp; searchItem) const = 0;
      virtual void insertFirst(const Type&amp; newItem) = 0;
      virtual void insertLast(const Type&amp; newItem) = 0;
      virtual void deleteNode(const Type&amp; deleteItem) = 0;
      linkedListIterator&lt;Type&gt; begin();
      linkedListIterator&lt;Type&gt; end();
      linkedListType();
      linkedListType(const linkedListType&lt;Type&gt;&amp; otherList);
      ~linkedListType();
  protected:
      int count;   //variable to store the number of elements in the list
      nodeType&lt;Type&gt; *first; //pointer to the first node of the list
      nodeType&lt;Type&gt; *last;  //pointer to the last node of the list
  private:
      void copyList(const linkedListType&lt;Type&gt;&amp; otherList);
  };
  template &lt;class Type&gt;
  bool linkedListType&lt;Type&gt;::isEmptyList() const
  {
      return(first == NULL);
  }
  template &lt;class Type&gt;
  linkedListType&lt;Type&gt;::linkedListType() //default constructor
  {
      first = NULL;
      last = NULL;
      count = 0;
  }
  template &lt;class Type&gt;
  void linkedListType&lt;Type&gt;::destroyList()
  {
      nodeType&lt;Type&gt; *temp;   //pointer to deallocate the memory
                              //occupied by the node
      while (first != NULL)   //while there are nodes in the list
      {
          temp = first;        //set temp to the current node
          first = first-&gt;link; //advance first to the next node
          delete temp;   //deallocate the memory occupied by temp
      }
      last = NULL; //initialize last to NULL; first has already
                   //been set to NULL by the while loop
      count = 0;
  }
  template &lt;class Type&gt;
  void linkedListType&lt;Type&gt;::initializeList()
  {
      destroyList(); //if the list has any nodes, delete them
  }
  template &lt;class Type&gt;
  void linkedListType&lt;Type&gt;::print() const
  {
      nodeType&lt;Type&gt; *current; //pointer to traverse the list
  
      current = first;    //set current so that it points to 
                          //the first node
      while (current != NULL) //while more data to print
      {
          cout &lt;&lt; current-&gt;info &lt;&lt; " ";
          current = current-&gt;link;
      }
  }//end print
  template &lt;class Type&gt;
  int linkedListType&lt;Type&gt;::length() const
  {
      return count;
  }  //end length
  template &lt;class Type&gt;
  Type linkedListType&lt;Type&gt;::front() const
  {
      assert(first != NULL);
  
      return first-&gt;info; //return the info of the first node 
  }//end front
  template &lt;class Type&gt;
  Type linkedListType&lt;Type&gt;::back() const
  {
      assert(last != NULL);
  
      return last-&gt;info; //return the info of the last node   
  }//end back
  template &lt;class Type&gt;
  linkedListIterator&lt;Type&gt; linkedListType&lt;Type&gt;::begin()
  {
      linkedListIterator&lt;Type&gt; temp(first);
  
      return temp;
  }
  template &lt;class Type&gt;
  linkedListIterator&lt;Type&gt; linkedListType&lt;Type&gt;::end()
  {
      linkedListIterator&lt;Type&gt; temp(NULL);
  
      return temp;
  }
  template &lt;class Type&gt;
  void linkedListType&lt;Type&gt;::copyList
  (const linkedListType&lt;Type&gt;&amp; otherList)
  {
      nodeType&lt;Type&gt; *newNode; //pointer to create a node
      nodeType&lt;Type&gt; *current; //pointer to traverse the list
      if (first != NULL) //if the list is nonempty, make it empty
          destroyList();
      if (otherList.first == NULL) //otherList is empty
      {
          first = NULL;
          last = NULL;
          count = 0;
      }
      else
      {
          current = otherList.first; //current points to the list to be copied
          count = otherList.count;
          //copy the first node
          first = new nodeType&lt;Type&gt;;  //create the node
          first-&gt;info = current-&gt;info; //copy the info
          first-&gt;link = NULL;        //set the link field of the node to NULL
          last = first;              //make last point to the first node
          current = current-&gt;link;     //make current point to
          while (current != NULL)
          {
              newNode = new nodeType&lt;Type&gt;;  //create a node
              newNode-&gt;info = current-&gt;info; //copy the info
              newNode-&gt;link = NULL;       //set the link of newNode to NULL
              last-&gt;link = newNode;  //attach newNode after last
              last = newNode;        //make last point tothe actual last node
              current = current-&gt;link;   //make current point to the next node
          }//end while
      }//end else
  }//end copyList
  template &lt;class Type&gt;
  linkedListType&lt;Type&gt;::~linkedListType() //destructor
  {
      destroyList();
  }//end destructor
  template &lt;class Type&gt;
  linkedListType&lt;Type&gt;::linkedListType
  (const linkedListType&lt;Type&gt;&amp; otherList)
  {
      first = NULL;
      copyList(otherList);
  }//end copy constructor
   //overload the assignment operator
  template &lt;class Type&gt;
  const linkedListType&lt;Type&gt;&amp; linkedListType&lt;Type&gt;::operator=
  (const linkedListType&lt;Type&gt;&amp; otherList)
  {
      if (this != &amp;otherList) //avoid self-copy
      {
          copyList(otherList);
      }//end else
  
      return *this;
  }
  #endif
  </code></pre>
  
  <p>He stated that I can access all the public functions from the main program, and use the function that returns the first pointer to the list, to initialize the current pointer. How do I do that?<br/><br/>
  Thanks!</p>