How to delete all the nodes in singly circular linked list? Is this code the right one ? I don't get the output in codeblocks when I tried for it. What is the mistake?
void deleteall()
{
struct node *temp1=head,*temp2;
do
{
while(temp1->next!=head)
{
temp1=temp1->next;
}
temp2=head;
head=head->next;
temp1->next=head;
free(temp2);
}while(temp1!=head);
head=NULL;
free(temp1);
printf("deleted successfully\n");
}
To delete a singly circular linked list, start from the next
of head
and keep on deleting until you reach to head
and then delete head
of the list, like this:
struct node *temp1 = head->next, *temp2;
while (temp1 != head) {
temp2 = temp1->next;
free(temp1);
temp1 = temp2;
}
free (head);
head = NULL;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.