Delete a specific node in link-list

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 803 Views - Last Post: 11 July 2013 - 12:17 AM Rate Topic: -----

#16 1helloworld1  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 11-May 13

Re: Delete a specific node in link-list

Posted 09 July 2013 - 11:13 AM

Quote

+ del 0x00425018 {Name="Henry" Position=0x00425038 "manger" Salary=1000.0000000000000 ...} node *
+ del->Name "Henry" std::basic_string<char,std::char_traits<char>,std::allocator<char> >
+ travel->next 0x00000000 {Name={...} Position=0x00000020 <Bad Ptr> Salary=??? ...} node *
+ del 0x00425018 {Name=<Bad Ptr> Position=0x00425038 "铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪铪b,gy " Salary=-2.6569842580370804e+303 ...} node *

I using debugger to check the variable i eneter. When i enter the 2nd node Name is "Henry" then this few sentence was highlighted with red colour.Why?

This post has been edited by 1helloworld1: 09 July 2013 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#17 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

Re: Delete a specific node in link-list

Posted 11 July 2013 - 12:17 AM

Upon re-reading your posts, I'm thinking now that you may have wanted to delete ALL the nodes that contained the string "Henry" ... (and NOT just the first Node that contained Henry) ... if any Nodes existed containing "Henry"?

If that is the case ... then coding something like this would do it:

void delete_Henry( Node*& head )
{
    const std::string Henry = "Henry";

    if( head )
    {
        // firstly handle THIS case ...
        // value is in 2nd or greater nodes (if they exist)
        Node* prev = head;
        Node* cur = head->next;
        while( cur ) // cur may be NULL at first ... if at end
        {
            // check if value is in 2nd or greater nodes (if they exist)
            // if so ... delete each of those nodes ...
            
            if( cur->name != Henry )
            {
                prev = cur;
                cur = cur->next;
            }
            else // delete this node containing Henry
            {
                prev->next = cur->next; // skip over this cur
                delete cur;
                cur = prev->next; // update to new (next) cur
            }
        }
        
        // now check if value exists in first node
        if( head->name == Henry )
        {
            Node* del = head;
            head = head->next;
            delete del;
        }
    }
    else
        std::cout << "List was already empty." << std::endl;
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2