We are supposed to delete the record (say n) of data type struct record* corresponding to uid from the BST. Remove n from the lists of friends of other records (a linked list with list_records* friends as the head of the list, which is given as one of the fields) and release the memory for the linked list nodes. Release memory for all the nodes in the list of friends of n. Return a copy of the value of the deleted node. If the node is not present, return a dummy record with -1 in the status field.
This is the segment of the code I wrote. free_memory is a library required to be installed. The commented code gives an error Verification failed. Ensure that there aren't duplicates in friend list
and the non-commented code gives the error of Integrity was violated
. I can't find any logical error in this code since days and I tried changing this function many times but it still gave an error.
struct record delete_record_bst(char uid[MAX_LEN])
{
struct record* found = search_record(uid, bst_root);
if (found == NULL) {
struct record dummy = { .status = -1 };
return dummy;
}
struct list_records* tmp = found->friends;
// while (tmp!= NULL) {
// struct record* friend_record = tmp->record;
// if (friend_record != NULL) {
// struct list_records* friend_list = friend_record->friends;
// deleteNode(friend_list, found);
// }
// tmp = tmp->next;
// }
// struct record deleted_value = *found;
// struct list_records* tmp1 = found->friends;
// while (tmp1 != NULL) {
// struct list_records* next = tmp1->next;
// free_memory(tmp1);
// tmp1 = next;
// }
// bst_root = delete(bst_root, found);
// num_bst_nodes--;
// free_memory(found);
// return deleted_value;
struct list_records* prev=NULL;
while (tmp!=NULL){
struct record* friend_record=tmp->record;
deleteNode(friend_record->friends,found);
prev=tmp;
tmp=tmp->next;
free_memory(prev);
}
bst_root=delete(bst_root,found);
num_bst_nodes--;
return *found;
}
struct record* delete(struct record *bst_root,struct record *r){
if (bst_root == NULL) {
return NULL;
}
if (cmp_uid(bst_root->uid,r->uid)==0) {
if (bst_root->left == NULL) {
struct record *ret = bst_root->right;
free_memory(bst_root);
return ret;
}
else if (bst_root->right == NULL) {
struct record *ret = bst_root->left;
free_memory(bst_root);
return ret;
}
else if (bst_root->right != NULL && bst_root->left != NULL) {
struct record *min_node = find_min(bst_root->right);
struct record* left1=bst_root->left;
struct record* right1=bst_root->right;
memcpy(bst_root,min_node,sizeof(struct record));
bst_root->left=left1;
bst_root->right=right1;
bst_root->right = delete(bst_root->right, min_node);
return bst_root;
}
}
else if (cmp_uid(r->uid,bst_root->uid)>0) {
bst_root->right = delete(bst_root->right, r);
}
else {
bst_root->left = delete(bst_root->left, r);
}
return bst_root;
}
void deleteNode(struct list_records* head,struct record* r)
{
struct list_records* temp = head;
struct list_records* prev=NULL;
if (temp != NULL && cmp_record(temp->record,r)==0) {
head = temp->next;
free_memory(temp);
return;
}
while (temp != NULL && cmp_record(temp->record,r)!=0) {
prev = temp;
temp = temp->next;
}
if (temp != NULL){
prev->next = temp->next;
free_memory(temp);
}
}
I see one glaring issue that isn't helping, however, your errors really don't mean anything to us =]
In deleteNode
, any changes to head
will be local to that function. Since C does not have any "pass by reference" ability, everything is "pass by value." Normally you will want to pass the address of something you want to change. You are passing a pointer to a function, but in this case the pointer itself is what you are trying to modify. In this case, it is better to think of the address as an integer value. In order to modify it, you will need a pointer to it. Here, you must pass a pointer to the address, or in other words, a pointer to a pointer.
In order to actually modify head
, you need to pass the address of it:
deleteNode(&friend_record->friends,found);
...and change the function to:
void deleteNode(struct list_records** head,struct record* r)
{
struct list_records* temp = *head;
struct list_records* prev=NULL;
if (temp != NULL && cmp_record(temp->record,r)==0) {
*head = temp->next;
free_memory(temp);
return;
}
while (temp != NULL && cmp_record(temp->record,r)!=0) {
prev = temp;
temp = temp->next;
}
if (temp != NULL){
prev->next = temp->next;
free_memory(temp);
}
}