2

I need to merge K ordered lists in O(n*logk). I am trying to create an array of lists and I don't know how to call certain functions (like min_heapify).

void min_heapify(List *list[],int size);

void main() {
    List list[10]; // initialize all 10 list randomly in ascending order.
    min_heapify(&list,10); // This line won't compile.

}

Please help.

2 Answers 2

2

You are passing a pointer to an array, rather than passing an array of pointers expected by your function.

Your function expects an array of list pointers, because it has both an asterisk and a pair of brackets:

List *list[] // <<== This means "an array of pointers to elements of type List"

To pass this function an array of pointers, declare an array of pointers in the main, and make the call:

void main() {
    List *list[10];
    // intitialize all 10 list pointers randomly in ascending order.
    for (int i = 0 ; i != 10 ; i++) {
        list[i] = malloc(...); // Initialize list at position i
    }
    min_heapify(list,10); // Remove the ampersand
}

If you were looking to pass an array of lists, keep only the asterisk or the square brackets:

void min_heapify(List list[],int size);

or the equivalent

void min_heapify(List *list,int size);

Remove the ampersand from the call in the main to compile this properly:

void main() {
    List list[10]; // intitialize all 10 lists randomly in ascending order.
    min_heapify(list,10); // Remove the ampersand
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have the following code: typedef struct LIST{ NODE *first; NODE *last; }LIST; void main(){ int result[10*LISTE],size=10; LIST* list[LISTE]; int i=0,j=0; for(i=0;i<LISTE;i++){ int vect[MAX]; FillRandomArray(vect,size,10,20000,false,1); list[i]->first=NULL; list[i]->last=NULL; }} And I get an error when i try to modify list[i]->first. Why? How should I do this?
@StanLeyDane That's because list[] is an array of uninitialized pointers. Before you could access list[i]->... you need to allocate memory for the element - say, list[i] = malloc(sizeof(LIST)); Of course you need to free each element once your program ends.
Yes, that was the case. I managed after a lot of trial and error to make it work. It succesfully merges the lists together now. Thank's a lot for your help and keep up the good work!
@StanLeyDane You are welcome! If the problem is solved now, please consider accepting the answer by clicking the grey check mark next to it. This would tell others that your problem is solved, and earn you a brand-new badge on Stack Overflow.
0

Try allocating the array dynamically.

List *list;    
list=malloc(sizeof(List)*10);
min_heapify(&list,10);

free(list); //when you don't need list anymore

This will work because

void min_heapify(List *list[],int size);

decays to

void min_heapify(List **list,int size);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.