Linked List Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phiefer3
    New Member
    • Jun 2007
    • 67

    Linked List Question

    Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs.

    First of all, I am using C++, however it's managed C++ or visual C++, or whatever microsoft calls it. I'm using MSVS2005, and working on a Windows Forms Application project from the C++ projects tab. I'm pointing this out because apparently the syntax used in these projects is similar to C#, and I want to avoid that misunderstandin g.

    Unfortunately the only class I've had on these types of projects used MSVS2003, which used a rather different set of syntax than 2005, so I've basically been piecing together what I can and teaching myself. 2005 uses the ^ character as a handle or whatever for managed objects. Up until, based on what I've seen of it, I've thought of this character (and the handles it creates) sort of like pointers (that is creating a reference to an object rather than working directly with the object).

    Anyways, the problem I've run into is that I defined a managed class (public ref class), and need to create a linked list of these objects. My first thought was instead of trying to figure out the ins and outs of a prewritten linked list, I'd make my own linked list class with specifically the functionality I want/need. Unfortunately despite the similarities, these object handles are not pointers, which ruined my plans. For one thing I found that I can't set them to NULL or 0, nor can I delete them.

    So basically I'm looking for any advice on what to do. The functionality I need is as follows: I need to know if the list is empty, I need to be able search the list to see if a specific instance of an object is in the list, I need to be able to insert objects into the list according in a sorted order (according to the < operator), and I need to be able to traverse the list from beginning to end to call the ToString() function of each object in the list (my original method was going to call the ToString of the first object and then remove the first object until the list was empty). And I'll need to be able to empty the list.

    I realize I could probably jerry-rig a sort of linked list where I don't delete nodes, and just drop references to the earlier nodes, but I'm not sure how good of an idea this would be memory-wise. I imagine as a managed type, that it might not be a bad thing, that the garbage collector may handle the deletion for me, but given that I may create as many as 615 objects at a go, I'd rather not risk it until I'm sure.

    Any help would be appreciated.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Why re-create something that already exists, look up the .NET class System::Collect ions::SortedLis t or other members of the System::Collect ions namespace.

    You can delete and set handles to NULL but you have to use the keyword nullptr so

    [code=cpp]
    System::Collect ions::SortedLis t^ MyList = gcnew System::Collect ions::SortedLis t();

    ...

    delete MyList; // Delete the object created above immediately
    [/code]

    Code:
    System::Collections::SortedList^ MyList = gcnew System::Collections::SortedList();
    
    
    ...
    
    MyList = nullptr; // Set my handle to null and mark the object 
                      // created above to be garbage collected 
                      // (assuming no-one else has a reference to it)

    Comment

    • phiefer3
      New Member
      • Jun 2007
      • 67

      #3
      Originally posted by Banfa
      Why re-create something that already exists, look up the .NET class System::Collect ions::SortedLis t or other members of the System::Collect ions namespace.

      You can delete and set handles to NULL but you have to use the keyword nullptr so

      [code=cpp]
      System::Collect ions::SortedLis t^ MyList = gcnew System::Collect ions::SortedLis t();

      ...

      delete MyList; // Delete the object created above immediately
      [/code]

      Code:
      System::Collections::SortedList^ MyList = gcnew System::Collections::SortedList();
      
      
      ...
      
      MyList = nullptr; // Set my handle to null and mark the object 
                        // created above to be garbage collected 
                        // (assuming no-one else has a reference to it)
      The main reason I was going to re-create it was because I'm not entirely familiar with browsing through the various namespaces to find things and figuring out how they work, and what I needed was relatively simple. So thanks for pointing me in the direction of where to find the type of structure I needed.

      However, in my attempt at making my own, I tried to use the delete statement on a handle, but it just gave me a compiler error that I couldn't delete it because it's not a pointer.

      I did happen across the nullptr thing in the VS2005 help index, but it noted that doing so does not mark the object for garbage collection (although that note may have just meant that if it had other references it wouldn't mark it for collection). And without the delete capability I wasn't positive if it would be a good idea to just set them to null and forget about them.

      Although, after considering how I've been treating String^ objects, I could have probably just made a makeshift linked list that didn't delete nodes and just cut them off and the garbage collector would just pick them up for me.

      I actually handled my problem by using the listbox that I was going to display the objects in as my list structure itself, it just limited my ability decide how to sort the list. But I left this question here to see what other solutions came about.

      Thanks for the help. Next time I get a chance I'm going to take a look at the System::Collect ion namespace and see what better fits my uses.

      Comment

      Working...