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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.