Reputation: 125
I pass in a linked list of size 10 to sort_nodes, alongside an int stating so. However when I use quicksort, my program "stops", and it does not seem to execute the second print nodes function. For some reason, my CLion compiler is giving me an "unable to disassemble function" error so I have been unsuccessful even using the debugger.
int main(void){
node_ptr *head;
int num_of_nodes = load_nodes(head); // returns the number of items loaded, in this case it is going to be 10
print_all_nodes(head);
sort_nodes(head,num_of_nodes);
print_all_nodes(head); // check that the items have been sorted correctly
}
void insert_node(node_ptr *head_ptr, node_ptr insertion){
if (*head_ptr != NULL){ // if the header has a pointer
printf("Inserting new item at head\n");
insertion->next = *head_ptr;
}
else{
printf("Head is null so assigning head to new pointer\n");
}
*head_ptr = insertion;
}
int compare_strings(const void *p1, const void *p2){
competitor *competitor1, *competitor2;
node1= (node *) p1;
node2 = (node *) c2;
return strcmp(node1->some_string,node2 ->some_string);
}
void delete_linked_list(node_ptr *head_ptr){
node_ptr current_temp_ptr = *head_ptr;
node_ptr next_temp_ptr = NULL;
while (current_temp_ptr != NULL){
next_temp_ptr = current_temp_ptr->next;
free(current_temp_ptr);
current_temp_ptr = next_temp_ptr;
}
head_ptr = NULL;
}
void sort_nodes(node_ptr *head_ptr, const int num_of_nodes){
printf("Num of nodes: %d\n",num_of_nodes); // expected 10 got 10;
node_ptr nodes[num_of_nodes]; // array used for sorting
node_ptr temp_ptr = *head_ptr;
printf("temp pointer set up\n");
for (int i=0; i<num_of_nodes; i++){
temp_ptr = temp_ptr->next; // get next (first) node
nodes[i] = temp_ptr;
}
delete_linked_list(head_ptr);
printf("Num of nodes: %d",num_of_nodes); //expected 10, got 10
qsort(&nodes,num_of_nodes, sizeof(node), compare_strings);
printf("Num of nodes= %d", num_of_nodes); // expected 10, got -2145129072
for (int i=0; i<num_of_nodes; i++){
insert_node(head_ptr,nodes[i]);
}
}
Sorry for any typos I've had to change names to make the problem more general
Upvotes: 2
Views: 129
Reputation: 153498
At least these issues
Wrong type
p1, p2
are pointers to a nodes_ptr
, not a pointer to node
. Recall that p1, p2
are pointers to the elements of the array and the array was nodes_ptr nodes[num_of_nodes]
.
int compare_strings(const void *p1, const void *p2){
// competitor *competitor1, *competitor2;
// node1= (node *) p1;
// node2 = (node *) c2;
const nodes_ptr *node1 = (const nodes_ptr *) p1;
const nodes_ptr *node2 = (const nodes_ptr *) p2;
// return strcmp(node1->some_string,node2 ->some_string);
return strcmp((*node1)->some_string, (*node2)->some_string);
}
Wrong element size and &
not needed
Rather than size to the type of an element sizeof(node)
(and use the wrong type as OP did), size to an array element sizeof nodes[0]
. Easier to code right.
nodes_ptr nodes[num_of_nodes]; // array used for sorting
....
// Drop v wrong size
// qsort(&nodes,num_of_nodes, sizeof(node), compare_strings);
qsort(nodes, num_of_nodes, sizeof nodes[0], compare_strings);
Upvotes: 2