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

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Delete List
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Delete() algorithm for linked lists, focusing on the correctness and potential issues in the provided code. Participants explore various aspects of the algorithm, including edge cases, memory management, and error handling.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants point out that the original implementation does not check if q->next is NULL before dereferencing it, which could lead to errors.
  • There is a concern that the algorithm assumes the element x is found without verifying, which could cause issues if x is not present in the list.
  • Participants discuss the need to release memory using free() when deleting nodes, highlighting that this is necessary if nodes were allocated with malloc() or calloc().
  • One participant suggests that if x is the first element, the pointer L must be updated to reflect this change.
  • Another participant raises the issue that if q->next is NULL after the while loop, subsequent checks will fail, indicating a need for better error handling.
  • There is a proposal to modify the code to handle the case where x is the first element and to ensure that the linked list's head pointer is updated accordingly.

Areas of Agreement / Disagreement

Participants express multiple competing views regarding the implementation details and error handling of the Delete() algorithm. There is no consensus on a final solution, as various issues and potential improvements are still being discussed.

Contextual Notes

Limitations include unresolved assumptions about memory allocation, the need for proper error handling when x is not found, and the implications of modifying the head pointer of the linked list.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
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
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)
 
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)
 
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)
 
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:
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:
 
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)
 
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;
  }
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K