I am not able to understand the problem After the first iteration when I take the new value of ch, the program ends At some point of time I thought my printList() is not working but this does not looks the case, please help.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
typedef struct node Node;
void insertAtBeginning(Node** head, int dat) {
Node *temp = (Node *)malloc(sizeof(Node));
temp->data = dat;
if(*head != NULL){
temp->link = *head;
*head = temp;
}
temp->link = NULL;
*head = temp;
}
void printList(Node* head) {
printf("The list is : ");
while (head != NULL) {
printf("%d ", head->data);
head = head->link;
}
printf("\n");
}
void main() {
Node *head = NULL;
char ch;
int element;
printf("Do you want to insert an element? (Y/N) : ");
scanf("%c", &ch);
while (ch == 'Y' || ch == 'y')
{
printf("Enter the element : ");
scanf("%d", &element);
insertAtBeginning(&head, element);
printList(head);
printf("Do you want to insert more element? (Y/N)"); //this where i think it is not working
scanf("%c", &ch);
}
}