liked list method to delete all elements with a given value

-- removed since resolved
Last edited on
In all likeliness, it wasn't the last 12 but the middle of the three last ones that was skipped. Keep in mind that when you delete an element, the 'current location' changes and you shouldn't ->next, because you'll skip an element. (e.g.: If you delete element 3, element 4 becomes 3, but you're checking 4 next. The old 4/new 3 is never checked.)

A neat trick for deleting items in all list types: go backwards. Current becomes next, but you've already checked that anyway, so you don't miss one if you ->prev. (e.g. If you delete 3, 4 becomes 3. You've already checked old4/new3 and you're checking 2 next, which is still 2.)
but when I delete an item, the previous pointer will now point to NULL as it's next element is gone and I'll loose the rest of my list from that point right? so I need to change that to point to the next item in the list? atleast that's how I've understood it :/
Last edited on
basicly the code looks like this:

1
2
3
4
5
6
// assuming I have a baseObject *foundPtr
// assuming I kept track of the one before it baseObject *previousPtr;
// this all assums the baseObject has a baseObject *nextObject; element for the link in the list.

    previousPtr->nextObject = foundPtr->nextObject;
    delete foundPtr;


This process shouldn't matter what the foundPtr is. If you are at the bottom of the list, nextObject should be null. Thus you would set the new tail to null. If you are at the top of the list, The head of the list needs to be set to the nextObject of the head item, then you delete the head. Anything else would adjust the pointer as it would. The head is the only special case you need to make sure you dealing with in a delete of the element, since most implementations we have a Head maintained and a working pointer for some point in the list. If you just used the working pointer, the head pointer becomes invalid and the list is lost instantly, since you use the head to orientate to a common point for all searches.
Last edited on
removed since resolved
Last edited on
closed account (D80DSL3A)
Nevermind...Got it.
Last edited on
nevermind, sorted it with an else if -.-
Last edited on
Topic archived. No new replies allowed.