the following is a part of C code for singly linked list:
for(tptr = start; tptr != NULL && tptr->data < newnode->data; prev=tptr, tptr= tptr->next);
How do I convert this for loop in C to for loop in Python?
the following is a part of C code for singly linked list:
for(tptr = start; tptr != NULL && tptr->data < newnode->data; prev=tptr, tptr= tptr->next);
How do I convert this for loop in C to for loop in Python?
In C
for(tptr = start; tptr != NULL && tptr->data < newnode->data; prev=tptr, tptr= tptr->next);
is equivalent to
tptr = start;
while (tptr != NULL && tptr->data < newnode->data) {
prev = tptr;
tptr = tptr->next;
}
A C-style for loop is rather a while loop. Expressing it in Python with a for construct would be certainly possible (e.g., writing a for loop that runs forever and breaking out if the condition is not met), but it would not be best practice.
In Python, I would recommend to express your example with a while loop (unless it is a simple for-loop like iterating over the numbers 0 to n).