title: Deleting by Index?
body: <p>I created a linked-list program using the following source code,</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  struct Node
  {
      int data;
      Node *next;
  };
  
  struct HeadNode
  {
      int count;
      Node *headPtr;
  };
  
  class LinkedList
  {
      public:
              LinkedList();
              ~LinkedList();
  
              void addToHead( int );
              bool removeFromHead();
  
  //          bool addToTail ( int );
  //          bool removeFromTail();
  
              void addNode ( int );
              bool deleteNode ( int );
              void deleteAllNodes();
  
              bool isEmpty();
              int getNoOfNodes();
  
              void displayAllNodes();
  
      private:
          int dataCmp ( int, int );
          void displayNode ( Node* );
  
          HeadNode head;
  };
  
  LinkedList::LinkedList()
  {
      head.count = 0;
      head.headPtr = NULL;
      return;
  }
  
  LinkedList::~LinkedList()
  {
      deleteAllNodes();
      return;
  }
  
  void LinkedList::addToHead ( int newData )
  {
      Node *pNew = new Node;
      pNew -&gt; data = newData;
      pNew -&gt; next = head.headPtr;
      head.headPtr = pNew;
      head.count++;
  }
  
  bool LinkedList::removeFromHead()
  {
      bool exit;
      Node *temp;
  
      if ( head.headPtr )
      {
          temp = head.headPtr;
          head.headPtr = head.headPtr -&gt; next;
          delete temp;
          head.count--;
          exit = true;        // returns true if it's successful
      }
      else
          exit = false;       // returns false if it's not successful
      return exit;
  }
  
  /*
  bool LinkedList::addToTail( int )
  {
  
  }
  bool Linked::removeFromTail();
  {
  
  }
  */
  
  void LinkedList::addNode ( int newData )
  {
      Node *pNew = new Node,
           *pPre = NULL,
           *pCur = head.headPtr;
      pNew -&gt; data = newData;
  
      while ( pCur &amp;&amp; dataCmp( pNew -&gt; data, pCur -&gt; data ) &gt;= 0 )
          {
              pPre = pCur;
              pCur = pCur -&gt; next;
          }
  
      if ( pPre )
      {
          pNew -&gt; next = pPre -&gt; next;
          pPre -&gt; next = pNew;
          head.count++;
      }
      else
      {
          pNew -&gt; next = head.headPtr;
          head.headPtr = pNew;
          head.count++;
      }
  }
  
  bool LinkedList::deleteNode( int data )
  {
      bool exit;
      Node *pPre = NULL,
           *pCur = head.headPtr;
  
      while ( pCur &amp;&amp; dataCmp( pCur -&gt; data, data ) &lt; 0 )
      {
          pPre = pCur;
          pCur = pCur -&gt; next;
      }
  
      if ( pCur &amp;&amp; dataCmp( pCur -&gt; data, data ) == 0 )
      {
          if ( pPre )
          {
              pPre -&gt; next = pCur -&gt; next;
              delete pCur;
              head.count--;
              exit = true; // return true if successful
          }
          else
          {
              head.headPtr = pCur -&gt; next;
              delete pCur;
              head.count--;
              exit = true; // return true if successful
          }
      }
      else
          exit = false; // return false if unsuccessful
  
      return exit;
  }
  
  void LinkedList::deleteAllNodes()
  {
      Node *temp;
  
      while ( head.headPtr )
      {
          temp = head.headPtr;
          head.headPtr = head.headPtr -&gt; next;
          delete temp;
          head.count--;
      }
  
      return;
  }
  
  bool LinkedList::isEmpty()
  {
      return head.count == 0;
  }
  
  
  int LinkedList::getNoOfNodes()
  {
      return head.count;
  }
  
  void LinkedList::displayAllNodes()
  {
      Node *pCur = head.headPtr;
      int nodeCount = 1;
  
      while ( pCur )
      {
          cout &lt;&lt; "Node " &lt;&lt; nodeCount &lt;&lt; ": ";
          displayNode( pCur );
          cout &lt;&lt; endl;
  
          nodeCount++;
          pCur = pCur -&gt; next;
      }
  
      return;
  }
  
  int LinkedList::dataCmp( int value0, int value1 )
  {
      int exit = 0;
  
      if ( value0 &lt; value1 )
          exit = -1;
      else if ( value0 &gt; value1 )
          exit = 1;
  
      return exit;
  }
  
  void LinkedList::displayNode( Node *node )
  {
      cout &lt;&lt; node -&gt; data;
      return;
  }
  
  void printMenu()
  {
      cout &lt;&lt; "1. Add to head" &lt;&lt; endl;
      cout &lt;&lt; "2. Remove from head" &lt;&lt; endl;
      cout &lt;&lt; "3. Add node " &lt;&lt; endl;
      cout &lt;&lt; "4. Delete node" &lt;&lt; endl;
      cout &lt;&lt; "5. Delete all nodes" &lt;&lt; endl;
      cout &lt;&lt; "6. Is the list empty?" &lt;&lt; endl;
      cout &lt;&lt; "7. Get number of nodes" &lt;&lt; endl;
      cout &lt;&lt; "8. Display all nodes" &lt;&lt; endl;
      cout &lt;&lt; "9. Quit" &lt;&lt; endl;
  }
  
  int getChoice()
  {
      int choice;
  
      cout &lt;&lt; "Select choice: ";
      cin &gt;&gt; choice;
      cin.clear();
      cin.ignore( 200, '\n' );
      return choice;
  }
  
  int getData()
  {
      int data;
  
      cout &lt;&lt; "Enter data: ";
      cin &gt;&gt; data;
      cin.clear();
      cin.ignore( 200, '\n' );
  
      return data;
  }
  
  void processChoice( int choice, LinkedList&amp; list )
  {
      int data;
      bool opStatus;
  
      switch ( choice )
      {
          case 1: data = getData();
              list.addToHead( data );
              break;
          case 2: if ( list.removeFromHead() )
              {
                  cout &lt;&lt; "Removed node from head" &lt;&lt; endl;
              }
              else
                  cerr &lt;&lt; "List is empty" &lt;&lt; endl;
              break;
          case 3: data = getData();
              list.addNode( data );
              cout &lt;&lt; "Node " &lt;&lt; data
                   &lt;&lt; " added";
              cout &lt;&lt; endl;
              break;
          case 4: if ( !list.isEmpty() )
              {
                  data = getData();
                  if ( list.deleteNode( data ) )
                  {
                      cout &lt;&lt; "Node " &lt;&lt; data
                           &lt;&lt; " deleted";
                      cout &lt;&lt; endl;
                  }
                  else
                      cerr &lt;&lt; "Node not found" &lt;&lt; endl;
              }
              else
                  cerr &lt;&lt; "List is empty" &lt;&lt; endl;
              break;
          case 5: list.deleteAllNodes();
              cout &lt;&lt; "All nodes deleted" &lt;&lt; endl;
              break;
          case 6: cout &lt;&lt; ( list.isEmpty() ? 
                        "List is empty" : "List is not empty" );
              cout &lt;&lt; endl;
              break;
          case 7: cout &lt;&lt; "No. of nodes: "
                   &lt;&lt; list.getNoOfNodes();
              cout &lt;&lt; endl;
              break;
          case 8: list.displayAllNodes();
              break;
          default: cout &lt;&lt; "Invalid choice" &lt;&lt; endl;
      }
  
  }
  
  int main()
  {
      LinkedList list;
      int choice;
      do
      {
          printMenu();
          choice = getChoice();
  
          if ( choice != 9 )
              processChoice( choice, list );
  
      } while ( choice != 9 );
  
  
  
      return 0;
  }
  </code></pre>
  
  <p>Instead of deleting by Value, how can I modify my code so I can delete nodes by index?</p>