title: How would I clear a linked list?
body: <p>I have been trying to write a shortest path algorithm, dijkstras algorithm, finding the shortest path for the first two vertices works just fine. I run into the problem while trying to clear a linked list and a priority queue.</p>
  
  <pre><code>class llNode {
  public:
      int id;
      int source;
      int weight;
      llNode* next;
      llNode(int key, int distance, int from) {
          id=key;
          weight=distance;
          source=from;
          next = NULL;
      }
  };
  
  
  class lList {
  private:
      llNode* root;
      llNode* end;
  
      void clearAll(llNode* toClear);
  
  
  public:
      lList() {
          root = NULL;
      }
  
      void add(llNode* toAdd) {
          if ( root == NULL) {
              root = toAdd;
              end = toAdd;
              return;
          }
  
          end-&gt;next = toAdd;
          end=end-&gt;next;
      }
  
      bool isFound(int key) {
          for(llNode* ii= root; ii != NULL ; ii=ii-&gt;next) {
              if ( ii-&gt;id == key) {
                  return true;
              }
          }
  
          return false;
      }
  
      void clearAll();
  
  };
  
  void lList::clearAll() {
  clearAll(root);
  }
  
  void lList::clearAll(llNode* toClear) {
  if(toClear == NULL) {
      return;
  }
  
  clearAll(toClear-&gt;next);
  
  toClear=NULL;
  
  }
  </code></pre>
  
  <p>Along with these clear methods I tried to simply set root to NULL and I also tried traversing through the list and using the delete on each element. I am having to luck with any of these methods. Root keeps getting set to an invalid location and I get access violation errors.</p>
  
  <p>Is there something simple that I am just not seeing? How would I go about deleting every element from a linked list?</p>