Jump to content
  • Advertisement
Sign in to follow this  
GDKnight

Clearing Linked List C#? (SOLVED)

This topic is 4626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well I have a simple question, I cant seem to find any info online so I decided to ask here. I coded my own LinkedList object in C# now I would like to implement a clear function into it which would clear each node of all its data and infact set each node to null. I am not quite sure how to do this. I could iterate through the nodes contained in it and set them to null but will this delete it? Im afraid it might set a reference to nothing leaving the data intact in memory. Or is this even a concern with C# im not quite sure on the exact details of doing this being a C++ programmer im used to just using delete. Any help would be greatly appreicated. [Edited by - GDKnight on October 24, 2005 5:32:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
Hopefully this tutorial would help. There are alot more C# tutorials in this website. I think it's very helpful to many people.

Share this post


Link to post
Share on other sites
Quote:
Original post by psae0001
I've no idea my link code won't I just type it out then:


In your post, you did not select text to display for the link as well as you missed the ending quotation mark.

Link Here - Click edit on my post to see how it's supposed to look.

Share this post


Link to post
Share on other sites
What about for a non-doubly linked list.

Mines a singly linked list I think what that one you showed in example does to remove it is some how collapse the nodes? I cant seem to get that to work for me.

Share this post


Link to post
Share on other sites
Well, once again the classical question. First of all, I perfer doubly-linked; nothing speical, I just BELIEVE it works better than singly-link. Anyway, if you are using the singly-link. Here's what I would approce.

On your list, you have a head node, 2nd node, 3rd node ... etc. What you need is to go the head node, set the 2nd node as the head node, then remove the head node.

Share this post


Link to post
Share on other sites
Quote:
Original post by GDKnight
I could iterate through the nodes contained in it and set them to null but will this delete it? Im afraid it might set a reference to nothing leaving the data intact in memory.

One way is to implement the Dispose pattern. Here's a starting point for ListNode; you should be able to construct the analogous Dispose pattern for LinkedList.


public class ListNode : IDisposable
{
public object Entry;
public ListNode NextNode;

// ...

void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

~ListNode()
{
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
if(disposing)
{
// Cleanup goes here.
Entry = null;
NextNode = null;
}
else
{
// Dispose wasn't called before the finalizer was.
DisposeNotCalled();
}

base.Dispose(disposing);
}

// Ignore improper disposing in Release mode.
[Conditional("DEBUG")]
private static DisposeNotCalled()
{
throw new InvalidOperationException("ListNode was improperly disposed.");
}
}

public class LinkedList
{
// ...

private ListNode[] _nodes;

// ...

public void Clear()
{
for (int i = 0; i < _nodes.Count; ++i)
{
// Disconnects all the nodes from the object graph, assuming there
// aren't any other references.
_nodes.NextNode = null;
_nodes = null;
}
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!