title: Unable to figure out how to delete the entire Linked List through a function using classes only
body: <p>Being a beginner for C++, I was able to get to the point of making a linkedlist using classes and not templates or vectors, and reached to a point where i can ad a node to the end, delete a node from the end (by value) and print the node. However, I now want to know how to be able to clear the complete set of nodes thereby clearing the complete linkedlist, without using any specialized functions or special headers.<br>
  I went through the following resources of reference but could not set or align my understanding with them as the following sources have done using different methods:-</p>
  
  <p><a href="https://stackoverflow.com/questions/19611008/cant-figure-out-how-to-clear-a-linked-list-in-c">Can&#39;t figure out how to clear a linked list in c++?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/21802195/linked-list-delete-method">Linked List Delete Method</a>
  (yes i tried getting ideas from java even though i dont know java but just tried  too)</p>
  
  <p><a href="https://stackoverflow.com/questions/5011990/c-programming-linked-lists-delete-node-at-position-n">C programming Linked Lists delete node at position N</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10049391/how-would-i-clear-a-linked-list">How would I clear a linked list?</a> (came closest to make me understand it but some confusion)</p>
  
  <p>Book: Deitel and Deitel (surprisingly near to my thought only that they have shown using templates or vectors)</p>
  
  <p>Book: Primer (a little bit hard for me to understand)</p>
  
  <p>Any help would be highly appreciated. Please try and forgive some of my bad habbits of programming like using namespace std and etc, etc, as my aim is to get the DeleteAll function working(clearing the complete list), look at the block of code which is commented totally in the last function of the class LinkedList.
  Then look at the Case 4 where i am trying to call it in the main function.this my first time with the concept of linked list, and yes it seems very intimidating.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include "ctype.h"                  //for enabling the recognition of data types for different illegal user inputs for main program
  using namespace std;
  
  
  // Node class
  class Node
  {
      int data;
      Node* next;
  
  
  public:
  
      Node(){};
      void setData(int aData)
      {
          data = aData;
      }
  
      void setNext(Node* aNext)
      {
          next = aNext;
      }
  
      int Data()
      {
          return data;
      }
  
      Node* Next()
      {
          return next;
      }
  
  };
  
  
  // LinkedList Class
  
  class LinkedList
  {
      private:
          Node *head;
  
      public:
          List()
          {
              head = NULL;
          }
  
          // printing contents of list        
          void Print()
          {
              Node *tmp = head;
  
              // but if no Nodes then following
              if (tmp==NULL)
              {
                  cout&lt;&lt;"\n \t\tcannot find any Nodes: List EMPTY\n";
                  return;
              }
  
              //if one node is found
              else if (tmp-&gt;Next()==NULL)
              {
                  cout&lt;&lt;tmp-&gt;Data();
                  cout&lt;&lt;"--&gt;";
                  cout&lt;&lt;"NULL"&lt;&lt;endl;
              }
  
              //else more nodes then parse and print the list
              else
              {
                  do
                  {
                      cout&lt;&lt;tmp-&gt;Data();
                      cout&lt;&lt;"--&gt;";
                      tmp = tmp-&gt;Next();
                  }
                  while(tmp!=NULL);
  
                  cout&lt;&lt;"";
                  cout&lt;&lt;"X"&lt;&lt;endl;
              }
  
  
          }
  
          //Append or add a node to the linked list
          void Append(int data)
          {
              //creates a new node;
              Node *newNode = new Node();
              newNode-&gt;setData(data);
              newNode-&gt;setNext(NULL);
  
              //create a temp pointer for further linking to be facilitated
              Node *tmp = head;
  
              //but before setting next link to point last node to new node
              //we need to check if we are at the end of the list and if not then traverse to the end of node
              if(tmp !=NULL)
              {
                  while(tmp-&gt;Next() !=NULL)
                  {
                      tmp = tmp-&gt;Next();
                  }
              tmp-&gt;setNext(newNode);
              }
  
              else
              {
                  head = newNode;
              }
  
          }
  
          //Delete a node from list BY VALUE
          void Delete(int data)
          {
              //again create a temp pointer.
              Node *tmp = head;
  
  
              //again if no nodes
              if (tmp==NULL)
              {
                  cout&lt;&lt;"\n \t\tNo Nodes to delete\n";
                  return;
              }
  
              //Last node in the list which is the same as only one node left in list then following
              else if(tmp-&gt;Next()==NULL)
              {
                  delete tmp;
                  head = NULL;
              }
  
              //again parse through the nodes again to delete the data related node.
              else
              {
                  Node *prev;
                  do
                  {
                      if(tmp-&gt;Data()==data)
                      {
                          break;
                      }
                      else
                      {
                          prev = tmp;
                          tmp = tmp-&gt;Next();
                      }
                  }
                  while(tmp != NULL);
  
                  //once the data is found and located then readjust the linkage of previous with next and 
                  //delete the current node
                  prev-&gt;setNext(tmp-&gt;Next());
                  delete tmp;
              }
          }
  
  //      
  //      int Deleteall()
  //      {
  //          again create a temp pointer.
  //          Node *tmp = head;
  //          
  //          
  //          again if no nodes
  //          if (tmp==NULL)
  //          {
  //              cout&lt;&lt;"\n \t\tNo Nodes to delete\n";
  //              return 0;
  //          }
  //          
  //          Last node in the list which is the same as only one node left in list then following
  //          else if(tmp-&gt;Next()==NULL)
  //          {
  //              delete tmp;
  //              head = NULL;
  //          }
  //          
  //          again parse through the nodes again to delete the data related node.
  //          else
  //          {
  //              Node *prev;
  //              
  //              while(tmp);
  //              {
  //              
  //              once the data is found and located then readjust the linkage of previous with next and 
  //              delete the current node
  //              prev-&gt;setNext(tmp-&gt;Next());
  //              delete tmp;
  //              }
  //          } return 0;
  //      }   
  };
  
  //Now call through the main function
  
  int main()
  {
  //New List
  
  LinkedList list1;
  int usrdata, choice;
  while(choice)
  {
  cout&lt;&lt;"\n\nPlease choose an action to be performed on the LinkedList\n\ninput 1 for adding/appending a node\ninput 2 for printing the list\ninput 3 for deleting the node by value\ninput 4 to exit\n\n";
  cin&gt;&gt;choice;
  switch(choice)
  {
      case 1 :
          cout&lt;&lt;"\nEnter your desired data for adding/appending\n";
          cin&gt;&gt;usrdata;
          list1.Append(usrdata);
          list1.Print();
          continue;
  
      case 2 :
          cout&lt;&lt;"\nPrinting\n";
          list1.Print();
          break;
  
      case 3 :
          cout&lt;&lt;"\nEnter your desired data for removal/deleting\n";
          cin&gt;&gt;usrdata;
          list1.Delete(usrdata);
          list1.Print();
          break;
  
      case 4 :
          cout&lt;&lt;"\n deleting n exiting...\n";
  //      list1.Deleteall();
          list1.Print();
          goto end;
  
      default :
          cout&lt;&lt;"I think you should go home now you seem tired\n";    
  }
  }
  
  end:
  cout&lt;&lt;"\n\nThis statement was reached because either you chose to exit or entered a wrong/illegal value or operation\n\n";
  
  system("pause");        
  return 0;
  }
  </code></pre>
  
  <p>How to get the last function (completely commented lines) DeleteAll working to clear the entire list without using any other specialized headers or special functions etc.</p>