Archived

This topic is now archived and is closed to further replies.

bilsa

Searching in Linked List ?

Recommended Posts

Take a look at my last post, thats what I actually want to achieve | | | | | | | | | | | | | | | | | | | | V V V V V [edited by - bilsa on September 3, 2003 2:39:20 PM] [edited by - bilsa on September 3, 2003 2:39:48 PM] [edited by - bilsa on September 3, 2003 2:40:38 PM] [edited by - bilsa on September 3, 2003 2:40:52 PM]

Share this post


Link to post
Share on other sites
This might not be the best solution (and I''m pretty curious as to what might be better since I''m trying something similar), but, what I''ve done is make sure the objects being stored in my list have the operator== overloaded for matching the object with an identifier.

So, in the search function, all you need to do is something like:

if (node.obj == TSearchVariable) return (node.obj);


However, I''m not sure if it''s a good idea to overload the operator== for things like that. But, it seems to work.

I think it may also be possible to do something with function pointers. Passing a pointer to a function which checks if the node matches the identifier. But, I''ve never been good with function pointers so I never tried it.

And, another option I thought about was making sure all my objects were derived from a base object which had a autogenerated unique id and a (non-unique) string name (set during construction). Then, the linked list could be searched using either the id or the name. (I figured those two methods were really the only way I''d be searching through the list.) But, I didn''t think that was a very good design so I rejected it.

Hope that helps.

-HQ

Share this post


Link to post
Share on other sites
Have you tried putting more than one identifier on each node of your linked list? (i.e. each node having an int identifier and a char* identifier) That way, you don''t have to use a template for the GetObject function--simply overload it with two different functions--GetObject(int) and GetObject(char*)

The one searches the nodes by int identifier, the other searches the nodes by char* identifier. Both of them would return a pointer to the required node.

Share this post


Link to post
Share on other sites
Yes... that would work in the case with only a string and an int.

But since my plan is to create a "template" for a linked list, the one hundredeleventh linked list might just as well need to have the float variable, that is in the TData object, as a TIdentifier.

So that would mean I would need to have lots of searchfunctions

Share this post


Link to post
Share on other sites
The easiest way to do this is to make a new linked list that has a template variable that holds the template return value and togather with that value, you store the real return value, which is e.g. an integer. You''ll just have to translate the template id (of the type you''ve chosen) to an integer before searching you template linked list with that found integer.

You could also define a class that holds a list of all id''s, and initialise that class together with your linked list.
Like this:
CLinkedList MyList;
-> in MyList::Mylist(), you''ll have to initialize something like this:
CLinkedListReturnValues RetValues;

In other words: make another templatized class that holds all the ID''s of the linked list template class.

"My basic needs in life are food, love and a C++ compiler"
[Project AlterNova] [Novanet]

Share this post


Link to post
Share on other sites
Vich, I''m not sure how that will help me. But when I read the entire post I notice that I was very unclear with what I wanted to achieve... even missleading you guys.

Take a look at this, a small example of what I want to achieve:


class TestObject
{
int nNumber = 9999;
char *sFilename = "test.txt";
}

class Node
{
void *m_Identifier;
TestObject *TestObjectData;
Node *pNext;
}

void SetIdentifier(void *Identifier)
{
//Here I want to set the m_Identifier
loop(through all the nodes)
{
m_Identifier = Identifier;
}
}


//Now lets say that I would create 100 nodes. Now I want to change the Identifier in each Node so that it points
//to a variable in TestObjectData. Note that TestObjectData is the one in the current Node.

SetIdentifier(this->TestObjectData->nNumber)


//I''m afraid I was a little unclear before... what I''m out for is more like a "searchpath", Identifier.
//Now each Node is supposed to use that same searchpath. Which will be like this:

void SetIdentifier(void *Identifier)
{
//Here I want to set the m_Identifier
loop(through all the nodes)
{
m_Identifier = this->TestObjectData->nNumber;
}
}

Share this post


Link to post
Share on other sites