473,794 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linked List Question

67 New Member
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.
Jul 26 '08 #1
2 1701
Banfa
9,065 Recognized Expert Moderator Expert
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

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
phiefer3
67 New Member
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

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::Collect ion 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
15134
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
19
13576
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., head and tail are not connected. Of course, recursive function aproach to traverse the list is one way. But, depending upon the list size, it could overrun the stack pretty fast.
7
2614
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 children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
12
15101
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 should be kept for references to previous element of list. Here is my solution below: struct node {
57
4306
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
1495
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 exactly 5 nodes between these pointers. Does this make any sense or are there some more efficient way to access certain elements in a Linked-List?
12
3954
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 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
9
2846
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 * nPtr; //the next node's pointer }
11
2554
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
9671
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.