brief explanation:
this is a test i'm given
the compare function passed by parameter in main is the same as compare_fn
i am returning 0 from the end of one function, and when i go back into the otherone, the expression evaluates to 1, and i don't know why
CODE
int compare(void *x, void *y) {
long sub= (long) x - (long) y;
if (sub > 0)
return 1;
else
if (sub < 0)
return -1;
else return 0;
}
int main() {
Table table;
long key_i= 212, value_i= 2 * 212;
void *value;
/*
* We pass NULL as delete functions, because we will not use dynamic memory
* for key or value fields.
*/
table= create(compare, NULL, NULL);
printf("Created a table. Size is %d.\n", size(table));
printf("Inserting item with key %li and value %li\n", key_i, value_i);
insert(table, (void *) key_i, (void *) value_i);
printf("Size is %d.\n\n", size(table));
printf("Deleting the single item of the table.\n\n");
delete(table, (void *) key_i);
printf("Searching for item with key %li ...\n\t", key_i);
value= lookup(table, (void *) key_i);
if (value != NULL)
printf("Retrieved the item with key %li, its value is: %li\n",
key_i, (long) value);
else printf("Item with key %li does not exist in the table\n", key_i);
printf("Size is %d.\n", size(table));
clear(table);
free(table);
return 0;
}
Starting program: --------------------
Created a table. Size is 0.
Inserting item with key 212 and value 424
Size is 1.
Deleting the single item of the table.
Breakpoint 1, delete (table=0x502010, key=0xd4) at bst.c:197
warning: Source file is more recent than executable.
197 if (table == NULL)
(gdb) next
200 node= ((BST_node *)((long)(table->table)));
(gdb) next
201 link= &node;
(gdb) next
204 while (node != NULL) {
(gdb) next
206 if (table->compare_fn(key,(node->key)) != 0) {
(gdb) step
compare (x=0xd4, y=0xd4) at public03.c:20
20 long sub= (long) x - (long) y;
(gdb) display sub
1: sub = 0
(gdb) next
21 if (sub > 0)
1: sub = 0
(gdb) next
24 if (sub < 0)
1: sub = 0
(gdb) next
26 else return 0;
1: sub = 0
(gdb) next
27 }
1: sub = 0
(gdb) next
delete (table=0x502010, key=0xd4) at bst.c:212
212 if (node->right != NULL) {
(gdb) display table->compare_fn(key,(node->key))
2: table->compare_fn (key, node->key) = 1
(gdb)