So I was learning about linked list and I created a method to delete the list, I am getting run-time error(unusual flow of control) with the goto statement in main function which I'm using as a looped confirmation prompt, here:
Code:
#include <stdio.h>#include <stdlib.h>
struct Node{
int data;
struct Node *next;
}*head;
struct Node* push(struct Node *current_head, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = current_head;
return new_node;
}
void displayList(struct Node* current_head)
{
while(current_head != NULL){
printf("%d\n", current_head->data);
current_head = current_head->next;
}
}
struct Node* deleteList(struct Node* current_head){
struct Node *next;
while(current_head != NULL){
next = current_head->next;
free(current_head);
current_head = next;
}
printf("The list is deleted.");
return current_head;
}
int main(){
int num;
puts("Enter the elements:(-1 to end)");
while(num != -1){
scanf("%d", &num);
if(num != -1)
head = push(head, num);
else
break;
}
puts("Displaying the List:");
displayList(head);
puts("Do you want to empty list before exiting?");
askAgain: {
puts("Y or N?");
char choice;
scanf("%c", choice);
if(choice == 'Y' || choice == 'N' || choice == 'n' || choice == 'y')
{
if(choice == 'Y' || choice == 'y'){
head = deleteList(head);
}
}
else
{
printf("Invalid Input. Try again:");
goto askAgain;
}
}
return 0;
}