void delete(struct node *ptr, int index) {
// If node to be deleted is the head node
if (index == 1) {
head = ptr -> next;
free(ptr);
}
// Deleting other nodes
else {
for (int i = 0; i < index - 2; i++) {
ptr = ptr -> next;
}
// Find the node to be deleted
struct node *toDelete = ptr -> next;
// Replace the link
ptr -> next = toDelete -> next;
// Deallocate the memory
free(toDelete);
}
}