4.Deletion of specific node from the list

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int deleteNode(struct Node** head_ref, int key) {
    struct Node* temp = *head_ref;
    struct Node* prev = NULL;

    if (temp != NULL && temp->data == key) {
        *head_ref = temp->next;
        free(temp);
        return 1;
    }

    while (temp != NULL && temp->data != key) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) {
        return 0;
    }

    prev->next = temp->next;
    free(temp);
    return 1;
}

int main() {
    int i, n, dl;
    scanf("%d", &n);

    struct Node *p, *q, *head;
    q = malloc(sizeof(struct Node));
    scanf("%d", &q->data);
    q->next = NULL;
    head = q;
    p = head;

    for (i = 2; i <= n; i++) {
        q = malloc(sizeof(struct Node));
        scanf("%d", &q->data);
        q->next = NULL;
        p->next = q;
        p = p->next;
    }

    scanf("%d", &dl);

    int deleted = deleteNode(&head, dl);
    if (!deleted) {
        printf("Value not found\n");
    }

   else{
    p = head;
    while (p != NULL) {
        printf("%d ", p->data);
        p = p->next;
    }}

    return 0;
}