How to Properly Implement the Delete() Algorithm for Linked Lists

In summary: I tried it again... Do I have to change something ? (Thinking)In summary, the conversation discussed the implementation of the algorithm Delete() for linked lists. The code presented included a function to delete a specific element from the list, but there were some issues with the code that needed to be addressed. These included the need for a check to see if the list was empty, as well as handling the case where the element to be deleted was the first element in the list. There was also a discussion about properly freeing memory when deleting an element from the list. Overall, the conversation highlighted the importance of carefully checking and debugging code to ensure it functions correctly.
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Smile)

I want to implement the algorithm [m]Delete()[/m] for linked lists.
That's what I have tried:

Code:
void Delete(pointer L,Type x){
      if (L==NULL){
         printf("There is no element x in the list. \n");
         return;
      }
      pointer q=L;
      while (q!=NULL && q->next->data!=x)
               q=q->next;
      q->next=q->next->next;
}

Could you tell me if it is right or if I have done something wrong? (Thinking)
 
Technology news on Phys.org
  • #2
evinda said:
Code:
1. void Delete(pointer L,Type x){
2.      if (L==NULL){
3.         printf("There is no element x in the list. \n");
4.         return;
5.      }
6.      pointer q=L;
7.      while (q!=NULL && q->next->data!=x)
8.               q=q->next;
9.      q->next=q->next->next;
}

Hi! (Happy)

In line 7, there should be a check if [m]q->next[/m] is NULL before dereferencing it. (Worried)
The check [m]q!=NULL[/m] is redundant then.In line 9, you are assuming you have found x, but that is not necessarily the case.
It may still be that x is not in the list. (Doh)In line 9 you are indeed removing the element that [m]q->next[/m] point to from the list (if it was found).
However, usually this memory has to be released with [m]free()[/m]. (Sweating)
 
  • #3
I like Serena said:
Hi! (Happy)

In line 7, there should be a check if [m]q->next[/m] is NULL before dereferencing it. (Worried)
The check [m]q!=NULL[/m] is redundant then.In line 9, you are assuming you have found x, but that is not necessarily the case.
It may still be that x is not in the list. (Doh)In line 9 you are indeed removing the element that [m]q->next[/m] point to from the list (if it was found).

So, should it be like that? (Thinking)

Code:
1. void Delete(pointer L,Type x){
2.      if (L==NULL){
3.         printf("There list is empty. \n");
4.         return;
5.      }
6.      pointer q=L;
7.      while (q->next!=NULL && q->next->data!=x)
8.               q=q->next;
9.      if (q->next->data==x)   q->next=q->next->next;
10.         else printf("There is no element x in the list. \n");
}
I like Serena said:
However, usually this memory has to be released with [m]free()[/m]. (Sweating)

How can this memory be released with [m]free()[/m]? (Thinking)
 
  • #4
evinda said:
So, should it be like that? (Thinking)

Code:
1. void Delete(pointer L,Type x){
2.      if (L==NULL){
3.         printf("There list is empty. \n");
4.         return;
5.      }
6.      pointer q=L;
7.      while (q->next!=NULL && q->next->data!=x)
8.               q=q->next;
9.      if (q->next->data==x)   q->next=q->next->next;
10.         else printf("There is no element x in the list. \n");
}

If x could not be found, q->next will end up as NULL.
Then line 9 will cause some chaos and madness. (Worried)

There is another issue.
Suppose that x is the very first element in the list, what will happen then? (Wondering)
How can this memory be released with [m]free()[/m]? (Thinking)

With something like:
Code:
if (q->next->data==x) {
    free(q->next);
    q->next=q->next->next;
}
This assumes the node was originally allocated with malloc() or calloc(). (Wasntme)
 
  • #5
I like Serena said:
If x could not be found, q->next will end up as NULL.
Then line 9 will cause some chaos and madness. (Worried)

There is another issue.
Suppose that x is the very first element in the list, what will happen then? (Wondering)

With something like:
Code:
if (q->next->data==x) {
    free(q->next);
    q->next=q->next->next;
}
This assumes the node was originally allocated with malloc() or calloc(). (Wasntme)
the code
if (q->next->data==x) {
free(q->next);
q->next=q->next->next;
}is dangerous coding as we have freed q->next we cannot use q->next-> next and need to be replaced by

if (q->next->data==x) {
pointer p = q->next->next;
free(q->next);
q->next=p;
}
 
Last edited:
  • #6
I like Serena said:
If x could not be found, q->next will end up as NULL.
Then line 9 will cause some chaos and madness. (Worried)

There is another issue.
Suppose that x is the very first element in the list, what will happen then? (Wondering)

Code:
1. void Delete(pointer L,Type x){
2.      if (L==NULL){
3.         printf("There list is empty. \n");
4.         return;
5.      }
6.      pointer q=L;
7.      if (q->data==x){
8.            q=q->next;
9.            return;
10.     while (q->next!=NULL && q->next->data!=x)
11.              q=q->next;
12.     if (q->next->data==x)&&(q->next->next!=NULL)   q->next=q->next->next;
13.      else if  (q->next->data==x)   
14.      else printf("The element x does not exist. \n")
}

So, should it be like that? (Thinking) If so, what could we do in line 13? :confused:
 
  • #7
evinda said:
Code:
1. void Delete(pointer L,Type x){
2.      if (L==NULL){
3.         printf("There list is empty. \n");
4.         return;
5.      }
6.      pointer q=L;
7.      if (q->data==x){
8.            q=q->next;
9.            return;
10.     while (q->next!=NULL && q->next->data!=x)
11.              q=q->next;
12.     if (q->next->data==x)&&(q->next->next!=NULL)   q->next=q->next->next;
13.      else if  (q->next->data==x)   
14.      else printf("The element x does not exist. \n")
}

So, should it be like that? (Thinking) If so, what could we do in line 13? :confused:

I'm afraid there are a couple more issues. (Sweating)

In line 8 you are changing [m]q[/m]. However, that will not be enough. [m]L[/m] will need to be changed - and not only here, but effectively also in the caller. (Worried)

Line 12 is not quite right yet.
Note that when the while-loop in line 10 finishes without finding x, we will have q->next==NULL. That means that line 12 will fail, not to mention line 13. (Doh)
 
  • #8
I tried it again... Do I have to change something ? (Thinking)

Code:
Delete(pointer L, Type x){
  if (L==NULL){
     printf("The list is empty. \n");
     return;
  }
  q=L;
  if (q->data==x){
      q=q->next;
      L=q;
      return;
  }
  while (q->next!=NULL and q->next->data!=x) q=q->next;
  if (q->next==NULL){
     printf("There isn't an element of which the value is equal to %d\n",x);
     return;
  }
  if (q->next->data==x){
      q=q->next->next;
      L=q;
  }
 

FAQ: How to Properly Implement the Delete() Algorithm for Linked Lists

How do you delete a node from a linked list?

To delete a node from a linked list, you need to find the node you want to delete and adjust the pointers of the previous and next nodes to skip over the node you want to delete. This essentially removes the node from the linked list and frees up the memory it was occupying.

What is the time complexity of deleting a node from a linked list?

The time complexity of deleting a node from a linked list depends on the position of the node. If the node is at the beginning of the linked list, the time complexity is O(1) as only the first node needs to be adjusted. However, if the node is at the end of the linked list, the time complexity is O(n) as all nodes need to be traversed before the last node can be adjusted.

Can you delete multiple nodes from a linked list in one operation?

No, you cannot delete multiple nodes from a linked list in one operation. Each node needs to be deleted individually by adjusting the pointers of the previous and next nodes.

What happens to the data in the node when it is deleted from a linked list?

When a node is deleted from a linked list, the data in the node is also deleted. This is because the node is essentially removed from the linked list and the memory it was occupying is freed up.

How is deleting a node from a linked list different from deleting from an array?

Deleting a node from a linked list and deleting from an array are different operations. In a linked list, deleting a node involves adjusting the pointers of the previous and next nodes to remove the node from the list. In an array, deleting an element involves shifting all the elements after the deleted element to fill the empty space. Additionally, deleting from an array has a time complexity of O(n) while deleting from a linked list can have a time complexity of either O(1) or O(n) depending on the position of the node.

Similar threads

Replies
2
Views
2K
Replies
7
Views
3K
Replies
2
Views
1K
Replies
9
Views
2K
Replies
1
Views
1K
Replies
4
Views
2K
Replies
1
Views
1K
Replies
3
Views
1K
Back
Top