dimanche 24 mai 2020

Why deleting function gives error on linked-list in C

I am currently working on a program based on linked-list. But my delete function causes crashes on my program. I want to allow users to delete a flight by it's fligt number. But I don't know what causes crash. How to fix this? Thanks

struct flight {
    int number;
    char source[20];
    char destination[20]; 
    struct flight* next;
};

void enter();
void display();
void delete();
int count();

typedef struct flight NODE;

NODE* head_node, * first_node, * temp_node = 0, * prev_node, next_node;
int data;
char data2[20], data3[20];
void delete()
{
    temp_node = (NODE*)malloc(sizeof(NODE));
    temp_node = first_node;
    int counter, flightno, j;
    temp_node->number = data;
    counter = count();
    printf("\nEnter flight number to delete: \n");
    scanf("%d", &flightno);

    for (j = 0; j <= counter; j++) 
    {
        if (flightno == data) {
            temp_node = temp_node->next;
            first_node = temp_node;
            printf("\nFlight log deleted.\n");
        }
        else 
        {
            printf("Flight number not found.");
        }
    }
}
int count()
{
    int count = 0;
    temp_node = first_node;
    while (temp_node != 0) {
        count++;
        temp_node = temp_node->next;
    }
    return count;
}

Aucun commentaire:

Enregistrer un commentaire