Explanation for deleting node from Linked List.

I have found this below function for deleting a node from Linked List from the book named [DATA STRUCTURE AND ALGORITHMS MADE EASY](https://www.amazon.in/Data-Structures-Algorithms-Made-Easy-ebook/dp/B01N4OA309?tag=googinhydr18418-21)


The code is:-
void DeleteFromLinkedList(struct ListNode **head, int position){
int k=1;
struct ListNode *p, *q;
if(*head==NULL){
printf("List Empty");
return;
}
p=*head;
//from the beginning
if(position==1){
*head=(*head)->next;
free(p);
return;
}

else{
//Traverse the list until arriving at the position from which we want to delete
while((p!=NULL) && (k<position)){
k++;
q=p;
p=p->next;
}
if(p==NULL) //At the end
printf("Position does not exist");
else{  //From the middle
q->next=p->next;
free(p);
}
}
}

Open in new window


Now my question is what is the meaning of
(k<position)

Open in new window

here what is the purpose of using this
 (k<position)

Open in new window

and wh at is the use of
intk=1;

Open in new window


please explain me in detail the above code.
Sourodip KunduAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
Hi Sourodip Kundu,

first k<position as part is a boolean expressions which results in true if the value of k is smaller than the value of position.

It is used together with p!=NULL as a condition for a while loop - a while loop executes the code in its body as long as the condition statement (here (p!=NULL) && (k<position)) is true.

In words this means "execute the code in the while-loop as long as the pointer p is not NULL AND the value of k is smaller than the value of position.

Thus the while loop iterates through the elements of the list until either p, which is set to the next node in the list with every iteration, becomes NULL (which happens when the end of the list is reached) or k, which is incremented with each iteration, becomes equal to position.

So, after the while loop is through either p points to the element with (1-based) index position and is removed from the list and deleted, or p is NULL in case position is larger than the count of elements in the list.

Hope this helps,

ZOPPO
0
 
Sourodip KunduAuthor Commented:
What will happen if we define
int k=0;

Open in new window

0
 
ZoppoCommented:
This would change the way how the passed index position is treated.

With 'k=1' the passed index is treated as 1-based, so passing 1 directs to the first element, 2 to the second, a.s.o., the index for the last element equals to the number of nodes in the list.

This is unusual in C/C++ where usually (at least for arrays) inidcies are zero-based (0 directs to the first element, 1 to the second, ..., the last element's index is <number of nodes> - 1.

To use second method (zero-based indicies) with your list two changes are needed, first is the int k=0; you mentioned, the second one is the first if-statement, it needs to be changed to if(position==0).

Hope this helps,

ZOPPO
0
 
Sourodip KunduAuthor Commented:
Thank you, I got it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.