473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 misunderstanding.

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.
Jul 26 '08 #1
2 1665
Banfa
9,065 Expert Mod 8TB
Why re-create something that already exists, look up the .NET class System::Collections::SortedList or other members of the System::Collections namespace.

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

Expand|Select|Wrap|Line Numbers
  1. System::Collections::SortedList^ MyList = gcnew System::Collections::SortedList();
  2.  
  3. ...
  4.  
  5. delete MyList; // Delete the object created above immediately
  6.  
Expand|Select|Wrap|Line Numbers
  1. System::Collections::SortedList^ MyList = gcnew System::Collections::SortedList();
  2.  
  3.  
  4. ...
  5.  
  6. MyList = nullptr; // Set my handle to null and mark the object 
  7.                   // created above to be garbage collected 
  8.                   // (assuming no-one else has a reference to it)
  9.  
Jul 27 '08 #2
Why re-create something that already exists, look up the .NET class System::Collections::SortedList or other members of the System::Collections namespace.

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

Expand|Select|Wrap|Line Numbers
  1. System::Collections::SortedList^ MyList = gcnew System::Collections::SortedList();
  2.  
  3. ...
  4.  
  5. delete MyList; // Delete the object created above immediately
  6.  
Expand|Select|Wrap|Line Numbers
  1. System::Collections::SortedList^ MyList = gcnew System::Collections::SortedList();
  2.  
  3.  
  4. ...
  5.  
  6. MyList = nullptr; // Set my handle to null and mark the object 
  7.                   // created above to be garbage collected 
  8.                   // (assuming no-one else has a reference to it)
  9.  
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::Collection namespace and see what better fits my uses.
Jul 27 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
by: Paminu | last post by:
I have a Linked-List and would like to create pointers to elements in this list. e.g I would like two pointers that point to each of their elements in the Linked-List. But there should always be...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.