Removal on Linked List

This is a discussion on Removal on Linked List within the C++ Programming forums, part of the General Programming Boards category; im doing my delete function right now, and im only able to delete the first node in the Linked List, ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    18

    Removal on Linked List

    im doing my delete function right now, and im only able to delete the first node in the Linked List, and when i try to delete lets say the second node, i get this Exception Handling Error and the Program terminates, and i have no idea why its doing that.

    heres a link to my program again, the remove function starts on line 110, and the while loop is on line 130 i think. thanx a lot

    http://sourcepost.sytes.net/source/s...source_id=1670


    To Email me back, email me at DRFUNKENSTINE@aol.com

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    You have to delete the last node first and then work your way back towards the head.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    arent i already doing that, because on my remove function i make cur = prev->next, then i say prev->next = cur->next, and then i delete cur. isnt that right?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    when working out what to do with linked lists here's a technique I use to help: draw a diagram of what I am trying to do before I write the code. For a singly linked list I use two lines to represent a node like this:


    node name
    pointer

    and use an extended arrow to represent which node is being pointed to.

    Code:
    Say I start with this:
    
    previous
    next------------>current
                             next------------>currentNext
                                                     next
    
    with goal being to delete current node of a singly linked  list and end up with this:
    
    previous
    next------------->currentNext 
                              next   
    
    //steps
    previous->next = current->next;
    delete current;
    It's easier for me to get the reassignments correctly if I see what I am doing. For what it's worth.

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    O, sorry, you might be. I didn't look at your code. That just sounded like the answer to ur problem from how you described it. I know, I'm LAZY .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21