Guest User

Untitled

a guest
Jan 19th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. else if(strcmp(cmd, "DELETE")==0){
  2. fscanf(ifp, "%s", &firstTemp);
  3. fscanf(ifp, "%s", &lastTemp);
  4.  
  5. temp2 = findName(root, firstTemp, lastTemp);
  6.  
  7.  
  8. if(temp2!=NULL){
  9.  
  10.  
  11.  
  12. while(temp2->myBuddies!=NULL){
  13. temp1 = find(root, temp2->myBuddies->buddy);
  14. temp1->myBuddies = delete(temp1->myBuddies, temp2->ID);
  15. temp2->myBuddies = delete(temp2->myBuddies, temp1->ID);
  16. temp1->numBuddies--;
  17. temp2->numBuddies--;
  18. }
  19. deleteNode(root, temp2->ID);
  20. fprintf(ofp,"Deleted %s %s.\n", firstTemp, lastTemp);
  21. }
  22.  
  23. ----------------------------------------------------------------------------
  24. BSTnode* find(BSTnode *current_ptr, int val) {
  25.  
  26. // Check if there are nodes in the tree.
  27. if (current_ptr != NULL) {
  28.  
  29. // Found the value at the root.
  30. if (current_ptr->ID == val)
  31. return current_ptr;
  32.  
  33. // Search to the left.
  34. if (val < current_ptr->ID)
  35. return find(current_ptr->left, val);
  36.  
  37. // Or...search to the right.
  38. else
  39. return find(current_ptr->right, val);
  40.  
  41. }
  42. else
  43. return NULL;
  44. }
  45. -----------------------------------------------------------------
  46. buddies* delete(buddies *front, int num) {
  47.  
  48. buddies *temp, *del;
  49. temp = front;
  50.  
  51. // Only need to delete if the list is not null.
  52. if (temp != NULL) {
  53.  
  54. // Take care of the case where first node needs to be deleted.
  55. if (temp->buddy == num) {
  56. del = temp -> next;
  57. free(temp);
  58. return del;
  59. }
  60. // Otherwise, loop until you find the node to delete and do so.
  61. while (temp->next != NULL) {
  62. if (temp ->next->buddy == num) {
  63. del = temp -> next;
  64. temp->next = temp ->next->next;
  65. free(del);
  66. return front;
  67. }
  68. temp = temp -> next;
  69. }
  70.  
  71. }
  72. return front;
  73. }
Add Comment
Please, Sign In to add comment