Click here to Skip to main content
15,883,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I need some help to improve this bit of code to remove a node from a linked list at a specific location.

/// <summary>
/// Removes a node from a specific location
/// </summary>
/// <param name="nodePosition"></param>
/// <returns></returns>
public bool RemoveAt(int nodePosition)
{
    //The choosen node is out of bounds
    if (nodePosition > listSize-1 || nodePosition < 0)
    {
        return false;
    }

    //first node was choosen
    if (nodePosition == 0)
    {
        if (listSize == 1)
        {
            first = null;
            listSize = 0;
            return true;
        }
        else
        {
            first = first.Next;
        }
    }

    Node node = first;

    //runs through the list
    for (int i = 0; i < nodePosition; i++)
    {
        //if it is at the beginning or at the end
        if (i == listSize - 1)
        {
            node.Next = null;
            break;
        }

        if (i == nodePosition - 1)
        {
            node.Next = node.Next.Next;
            break;

        }
        else
        {
            node = node.Next;
        }

    }

    listSize--;
    return true;
}
Posted
Comments
Maciej Los 26-Sep-13 7:10am    
What kind of issue do you have? Where are you stuck?
Paulo Augusto Kunzel 26-Sep-13 7:13am    
Hello Maciej,
It's simply a matter of performance. I don't think this is the best way to implement it, but so far I couldn't do it better..

1 solution

That's a very odd way to do it. I wouldn't concern yourself with list size at all.

I'd recommend a two stage approach, first identifying the node you want to remove, the removing it. A good way to keep things consistent is to have an empty dummy node at the beginning of the list, purely to provide the link to the next item (the first item or null) if there isn't one. This way you can write a generalised method which doesn't have to behave differently for the first item.

Node current = dummy; // empty node at front

while (current.Next != null && index--)
{
    current = current.Next;
}

this will stop at the item before the one you want to remove, so to get it you just need current.Next which will be the item or null if you've gone outside the structure.

Then (if current.Next != null):

current.Next = current.Next.Next; // item removed!


It's best when building alogithms to steer clear of special blocks for edge cases (ie. first item, last item etc.)
 
Share this answer
 
v2
Comments
Paulo Augusto Kunzel 26-Sep-13 8:38am    
The idea of the dummy is a great one.. thx.
Wouldn't it cause a problem doing

current.Next = current.Next.Next;

if the item is the last one from the list?
Rob Philpott 26-Sep-13 8:47am    
I don't think so. Current is a bad name as it is actually the one before current. 'Previous' would be better.

So, when current (new definition) is the last, it next is null. Previous is the one before, so you set Previous.Next = Current.Next = null. If that makes sense!
Paulo Augusto Kunzel 26-Sep-13 9:28am    
It's perfect. I used the name current to indicate the current node which it would be referencing.
Thanks for the quick reply
Rob Philpott 26-Sep-13 10:49am    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900