I'm doing some C++ drills to learn the language.
Currently, I'm working on linear data structures, and I'm asked to implement sorting in a linked list without an extra array or structure.
Right now, I'd like to use callbacks in the iterate()
function, to be flexible to call a print()
function to check the values of a findMin
to find the minimum value that will determine the base number for sorting.
I read this pretty useful guide about callbacks: Callback functions in C++, but I can't reference the callback inside of my recursion call inside the iterate()
function.
It would be nice to get some help to solve the issue that the comment in the code shows, but if you can let me know which concept I need to review/study, that would be even more helpful for building callback knowledge foundations.
/* Implement sorting in a dynamic linked list without using an additional array or data structure.*/
#include <iostream>
class List{
typedef struct Node{
int data;
Node* next = nullptr;
}node;
public:
List(int data);
~List();
void add(int data);
void iterate(Node * node, void(*func)());
private:
int min = 0;
Node* root;
Node* it;
};
List::List(int data){
root->data = data;
it = root;
}
List::~List(){}
void List::iterate(Node * node, void(*func)()){
func();//prints or finds min
if(node->next == nullptr)
return;
iterate(node->next,&func()); //--compiler says it can't get an address of a rvalue type void
}
int main(){
List list(8);
list.add(9);
list.add(7);
list.add(4);
list.add(3);
return 0;
}