Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- else if(strcmp(cmd, "DELETE")==0){
- fscanf(ifp, "%s", &firstTemp);
- fscanf(ifp, "%s", &lastTemp);
- temp2 = findName(root, firstTemp, lastTemp);
- if(temp2!=NULL){
- while(temp2->myBuddies!=NULL){
- temp1 = find(root, temp2->myBuddies->buddy);
- temp1->myBuddies = delete(temp1->myBuddies, temp2->ID);
- temp2->myBuddies = delete(temp2->myBuddies, temp1->ID);
- temp1->numBuddies--;
- temp2->numBuddies--;
- }
- deleteNode(root, temp2->ID);
- fprintf(ofp,"Deleted %s %s.\n", firstTemp, lastTemp);
- }
- ----------------------------------------------------------------------------
- BSTnode* find(BSTnode *current_ptr, int val) {
- // Check if there are nodes in the tree.
- if (current_ptr != NULL) {
- // Found the value at the root.
- if (current_ptr->ID == val)
- return current_ptr;
- // Search to the left.
- if (val < current_ptr->ID)
- return find(current_ptr->left, val);
- // Or...search to the right.
- else
- return find(current_ptr->right, val);
- }
- else
- return NULL;
- }
- -----------------------------------------------------------------
- buddies* delete(buddies *front, int num) {
- buddies *temp, *del;
- temp = front;
- // Only need to delete if the list is not null.
- if (temp != NULL) {
- // Take care of the case where first node needs to be deleted.
- if (temp->buddy == num) {
- del = temp -> next;
- free(temp);
- return del;
- }
- // Otherwise, loop until you find the node to delete and do so.
- while (temp->next != NULL) {
- if (temp ->next->buddy == num) {
- del = temp -> next;
- temp->next = temp ->next->next;
- free(del);
- return front;
- }
- temp = temp -> next;
- }
- }
- return front;
- }
Add Comment
Please, Sign In to add comment