Need help with C++ linked lists, returning a pointer

austin316

Diamond Member
Dec 1, 2001
3,572
0
0
Ok, I need to know the code for a function that returns a head pointer. This program is done using template functions. So far, what I have is a struct for the node (includes data and a link), a class that has private member variables for the headptr, (which is what I want to return). I now need a non-member function that takes in two nodes and compares them. the only problem is I can't find how to make a function that will return the headptr node. any help will be greatly appreciated.
 

GigaCluster

Golden Member
Aug 12, 2001
1,762
0
0
This thread belongs in the Software Programming forum.

From what I understand, you want one of two things: return a node pointer OR compare two node pointers and return a boolean value TRUE or FALSE is they're the same.

Given that the node pointer is a custom type definition, like so:
typedef node* nodePtr;

TO RETURN A NODE POINTER:

nodePtr my_fun(nodePtr first) // parameters are optional; for example, to traverse a list, you need a pointer to the first link
{
nodePtr temp;
<traverse the list or whatnot>
return temp;
}

TO COMPARE TWO NODE POINTERS AND RETURN A BOOLEAN VALUE

bool my_fun(nodePtr x, nodePtr y)
{
if (x == y)
return 1; // they match
return 0; // they're different
}


Hope this helps.
 

worth

Platinum Member
Feb 4, 2001
2,369
0
0
Why are you using a class together with a struct? What is the point? Try explaining your program better :p
 

austin316

Diamond Member
Dec 1, 2001
3,572
0
0
thanks for the post, but I'm still confused. how do I make a typedef for a templated function? the name of my stuct is node, my template is template <class Item> and my class is name Bag.

i think it should be something like this, typedef node<Item>* nodeptr;

but that doesn't work.
 

austin316

Diamond Member
Dec 1, 2001
3,572
0
0
my template is called class Item. didn't come out before. why I use both is because I feel its easier to use a struct instead of a class for nodes.
 

shikhan

Senior member
Mar 15, 2001
834
0
71


<< thanks for the post, but I'm still confused. how do I make a typedef for a templated function? the name of my stuct is node, my template is template <CLASS Item>and my class is name Bag. i think it should be something like this, typedef node<ITEM>* nodeptr; but that doesn't work. >>



I agree with what someone said before... structs and a class? Uck...
I'm also not seeing why you need a typedef in here....

Give us what exactly your looking for in your program... If its a simple templated linked list and your looking to see how to make a perticular function, tell us what function and what your code looks like now. Then we can try to help :-D


PS. GigaCluster gave a general idea of what some function do although he left out a few things [ie, the nodes must be able to be compared and so have the assignment operator overloaded and such]

 

GigaCluster

Golden Member
Aug 12, 2001
1,762
0
0


<< shoot. shoulda went to class >>


Yes, that might've helped.

A linked list can be easily implemented with a single struct. No classes and no templates are required.

Example:


struct node; //declaring a node so that the typedef statement below wouldn't give a compile error
typedef node* nodePtr; //defining a new data type -- node pointer.
struct node {
int number; //actual data goes here
nodePtr next; //a pointer that uses our custom data type to point to the next link in the list
};