fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct node{
  5. int value;
  6. struct node *left;
  7. struct node *right;
  8. } Node;
  9.  
  10. Node *
  11. newNode(int n){
  12. Node* obj;
  13. obj = (Node *)malloc(sizeof(Node));
  14. obj->value = n;
  15. obj->left = NULL;
  16. obj->right = NULL;
  17. return obj;
  18. }
  19.  
  20.  
  21. Node *
  22. addNode(Node* obj, int n){
  23. Node *iobj;
  24.  
  25. if(obj == NULL){
  26. return newNode(n);
  27. }
  28. iobj = obj;
  29.  
  30. while(1){
  31. if(iobj->value == n){
  32. break;
  33. }else if(iobj->value < n){
  34. if(iobj->right == NULL){
  35. iobj->right = /* (10) */;
  36. break;
  37. }else{
  38. iobj = iobj->right;
  39. }
  40. }else{
  41. if( iobj->left == NULL){
  42. iobj->left = newNode(n);
  43. break;
  44. }else{
  45. iobj = iobj->left;
  46. }
  47. }
  48. }
  49. return obj;
  50. }
  51.  
  52.  
  53. Node *
  54. deleteThisNode(Node *obj){
  55. Node *tmpobj;
  56. Node *iobj;
  57.  
  58. if(obj == NULL) return NULL;
  59. if(obj->left == NULL){
  60. tmpobj = obj->right;
  61. free(obj);
  62. }else if(obj->right == NULL){
  63. tmpobj = obj->left;
  64. free(obj);
  65. }else{
  66.  
  67. tmpobj = obj->left;
  68.  
  69. iobj = tmpobj;
  70. while(iobj->right != NULL){
  71. iobj = iobj->right;
  72. }
  73. iobj->right = /* (11) */;
  74. free(obj);
  75. }
  76. return tmpobj;
  77. }
  78.  
  79.  
  80. Node *
  81. deleteNode(Node *obj, int n){
  82. Node * pobj;
  83.  
  84. if(obj->value == n){
  85. obj = deleteThisNode(obj);
  86. return obj;
  87. }else{
  88.  
  89. pobj = obj;
  90. while(1){
  91. if(pobj->value < n){
  92. if(pobj->right == NULL){
  93. break;
  94. }else{
  95. if(pobj->right->value==n){
  96. pobj->right = /* (12) */;
  97. break;
  98. }else{
  99. pobj = pobj->right;
  100. }
  101. }
  102. }else{
  103.  
  104. if(pobj->left == NULL){
  105. break;
  106. }else{
  107. if(pobj->left->value == n){
  108. pobj->left = deleteThisNode(pobj->left);
  109. break;
  110. }else{
  111. pobj = /* (13) */;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118.  
  119.  
  120. void
  121. printNodes(Node *obj){
  122. if(obj != NULL){
  123. if(obj->left != NULL){
  124. printNodes(obj->left);
  125. }
  126.  
  127. printf("%d\t", obj->value);
  128.  
  129. if(obj->right != NULL){
  130. printNodes(obj->right);
  131. }
  132. }
  133. }
  134.  
  135. int
  136. main(int argc, char *argv[]){
  137. Node *topnode = NULL;
  138.  
  139.  
  140. topnode = addNode(topnode, 5);
  141. topnode = addNode(topnode, 7);
  142. topnode = addNode(topnode, 1);
  143. topnode = addNode(topnode, -1);
  144. topnode = addNode(topnode, 4);
  145. topnode = addNode(topnode, 10);
  146.  
  147. printNodes(topnode);
  148. printf("\n");
  149.  
  150. topnode = deleteNode(topnode, 5);
  151.  
  152. printNodes(topnode);
  153. printf("\n");
  154. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘addNode’:
prog.c:35: error: expected expression before ‘;’ token
prog.c: In function ‘deleteThisNode’:
prog.c:73: error: expected expression before ‘;’ token
prog.c: In function ‘deleteNode’:
prog.c:96: error: expected expression before ‘;’ token
prog.c:111: error: expected expression before ‘;’ token
stdout
Standard output is empty