Created
November 8, 2016 02:58
-
-
Save snickerbockers/27bb1ef4b8c0c194a9368b776b3d85d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
/* | |
* Options: | |
* PAREN - use parenthesis around the ternary | |
* GREATER - make the root_node's key greater than the key sent to less | |
* (default is less) | |
*/ | |
/********************************************************************* | |
TEST RESULTS: | |
~/programs/test_this $ g++ -o test test.c | |
~/programs/test_this $ ./test | |
node_root->left: 0x0x128e010 | |
node_root->right: 0x0x128e070 | |
node_left: 0x0x128e010 | |
node_right: 0x0x128e030 | |
node_ins: 0x0x128e070 | |
something got set | |
~/programs/test_this $ g++ -DGREATER -o test test.c | |
~/programs/test_this $ ./test | |
node_root->left: 0x0x208c010 | |
node_root->right: 0x0x208c030 | |
node_left: 0x0x208c010 | |
node_right: 0x0x208c030 | |
node_ins: 0x0x208c070 | |
nothing got set | |
~/programs/test_this $ g++ -DPAREN -o test test.c | |
~/programs/test_this $ ./test | |
node_root->left: 0x0x2406010 | |
node_root->right: 0x0x2406070 | |
node_left: 0x0x2406010 | |
node_right: 0x0x2406030 | |
node_ins: 0x0x2406070 | |
something got set | |
~/programs/test_this $ g++ -DPAREN -DGREATER -o test test.c | |
~/programs/test_this $ ./test | |
node_root->left: 0x0xc9c070 | |
node_root->right: 0x0xc9c030 | |
node_left: 0x0xc9c010 | |
node_right: 0x0xc9c030 | |
node_ins: 0x0xc9c070 | |
something got set | |
~/programs/test_this $ gcc -o test test.c | |
test.c: In function 'test': | |
test.c:20:41: error: lvalue required as left operand of assignment | |
(p->key > key) ? p->left : p->right = ins; | |
^ | |
~/programs/test_this $ gcc -DPAREN -o test test.c | |
test.c: In function 'test': | |
test.c:18:43: error: lvalue required as left operand of assignment | |
((p->key > key) ? p->left : p->right) = ins; | |
^ | |
*************************************************************************/ | |
typedef struct node { | |
int key; | |
struct node *left, *right; | |
} Node; | |
void test(Node *p, int key, Node *ins) { | |
#ifdef PAREN | |
((p->key > key) ? p->left : p->right) = ins; | |
#else | |
(p->key > key) ? p->left : p->right = ins; | |
#endif | |
} | |
int main(int argc, char **argv) { | |
Node *node_left = (Node*)calloc(1, sizeof(Node)); | |
Node *node_right = (Node*)calloc(1, sizeof(Node)); | |
Node *node_root = (Node*)calloc(1, sizeof(Node)); | |
Node *node_ins = (Node*)calloc(1, sizeof(Node)); | |
node_root->left = node_left; | |
node_root->right = node_right; | |
#ifdef GREATER | |
test(node_root, -1, node_ins); | |
#else | |
test(node_root, 1, node_ins); | |
#endif | |
printf("node_root->left: 0x%p\n", node_root->left); | |
printf("node_root->right: 0x%p\n", node_root->right); | |
printf("node_left: 0x%p\n", node_left); | |
printf("node_right: 0x%p\n", node_right); | |
printf("node_ins: 0x%p\n", node_ins); | |
if (node_root->left == node_ins || node_root->right == node_ins) | |
printf("something got set\n"); | |
else | |
printf("nothing got set\n"); | |
free(node_left); | |
free(node_right); | |
free(node_root); | |
free(node_ins); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment