Clearing Linked List C#? (SOLVED)

Started by
15 comments, last by GDKnight 18 years, 5 months ago
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]
- GDKnight
Advertisement
understanding garbage collection part 1

part 2
So I should call GC.ReRegisterForFinalize(node); On each node?
- GDKnight
Hopefully this tutorial would help. There are alot more C# tutorials in this website. I think it's very helpful to many people.

<a href="http://www.codeproject.com/csharp/doubly-linkedlist.asp>
I've no idea my link code won't I just type it out then:

http://www.codeproject.com/csharp/doubly-linkedlist.asp
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.
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.
- GDKnight
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.
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;    }  }}
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}

This topic is closed to new replies.

Advertisement