twtw 0 Newbie Poster

i cannot delete the lastest data i input, why. it show ".exe has stoppped working"

void system1::deletenode(const string& id,const string& name)
{
   string data;               // User enters node to be deleted.
   nodetype * current = head;     // Start at the head, and checks each node.
   nodetype * garbage = current;  // garbage FOLLOWS along behind the pointer current. 

   if (head == NULL)         // Circular list is empty when head equals NULL.
   {
      cout << "\nList is empty!\n\n";
      return;
   }   
   cout << "\nDELETE an ID#: ";
   cin >> data;

   if (head == current->link)   // ** If there is ONLY one element in list. **
   {
      if ((current->ID) == data)   // And it contains the id to be destroyed.
      {   
         head = NULL;    // ** IMPORTANT to reset head to NULL if only element **
         delete garbage; // delete node.
         garbage = 0;    // To prevent a dangling pointer and possible errors.
         cout << "\nID# found--> " << data << ": DELETED!\n\n";
         return;
      }
      else
      {
         cout << "\nID#--> " << data << " is NOT in the list!\n\n";
         return;
      }
   }   
    else if ((current->ID) == data)  // Condition for deletion of first node in list.
    {      
      while (current->link != head) // Find the node that is not equal to head.
      {
         current = current->link;  // If more than one node, move to next node.
      }
      // Next node after head is assigned to current next, and then to head.
      head = current->link = head->link;   
      delete garbage;              // Delete the node.
      garbage = 0; // To prevent a dangling pointer and possible errors.
      cout << "\nID# found--> " << data << ": DELETED!\n\n";
      return;
   }

   // Call the other delete function if further search conditions are needed.
 deleteFind(data,head,current, garbage);
}


void system1::deleteFind(string data, nodetype *&  head,nodetype * current, nodetype * garbage)
{
    // Condition for deletion of all nodes EXCEPT first and last.
    while (current->link->link != head)
    {
        if (current->link->ID == data)
        {
              garbage = current->link; // current next is assigned to garbage
              current->link = current->link->link; // current points to current next next.
              delete garbage;         // Delete node.
              garbage = 0; // To prevent a dangling pointer and possible errors.
              cout << "\nID# found--> " << data << ": DELETED!\n\n";
              return;
        }
        current = current->link;    // Move to the next node, if any.        
    }

    // Condition for deletion of LAST node only.
    if ((current->link->link == head) && (current->link->ID == data))
    {
        garbage = current->link;    // Assign current next to garbage.
        delete garbage;             // delete node.
        garbage = 0; // To prevent a dangling pointer and possible errors.
        cout << "\nID# found--> " << data << ": DELETED!\n\n";
        current->link = head;       // IMPORTANT: point current to head.
        return;
    }
    cout << "\nID#--> " << data << " is NOT in the list!\n\n";
    return;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.