How to pass in a linked list?

So I'm kinda struggling to pass in a linked list to a function that sorts the linked list,

how do I pass in a linked list to a function?

Do I pass in a pointer to the 'head' node?

If so, how?

many thanks :)
That depends on how you manage your linked list.

If your linked list is a class that manages itself and keeps nodes out of your reach, just pass an instance by reference.

If your linked list is just you literally managing a bunch of nodes (getting your hands dirty and avoiding Object Oriented Design at all costs), then yeah you would pass the head. Make sure you pas the head pointer by reference though, because the sort function may need to change which element is the first element.

If your linked list is a class that manages itself and keeps nodes out of your reach, just pass an instance by reference.


I've been trying this but I get the error

" IntelliSense: a reference of type "List&" (not const-qualified) cannot be initialized with a value of type "List::node *"

List is my class, node is a struct declared in the class.

Any ideas?

Don't pass a node, pass the list itself.
1
2
3
4
5
6
7
8
void MySortFunc(List &list);

//...

List my_list;
//add elements to list...
MySortFunc(list);
//show sorted list... 
Note that you shouldn't even know that the list uses nodes; from your perspective, the list takes care of itself and doesn't even let you touch its nodes.
Last edited on
hmm, I'm still getting the same error as above :/
Then you're not doing what I just said. That error is saying that you are passing a node instead of the list itself - if you are still getting the same error that means you are still trying to pass the node.

You could post your code so we could figure out what's wrong and how you can fix it ;)
Topic archived. No new replies allowed.