-1

I'm facing a problem with storing characters in the following code. It is compiled but it is not taking 2nd,4th,6th... characters as inputs.

struct ll {
    char data;
    struct ll *next;
};

Here is the code for creating a Linked list.

struct ll* create_ll(struct ll *start){
    struct ll *p1,*p2;
    char a;
    printf("Enter q to stop\n");
    printf("Enter data:");
    scanf("%c",&a);
    while(a != 'q'){
    p1 = (struct ll*)malloc(sizeof(struct ll*));
    p1 -> data = a;
    if(start == NULL){
        start = p1;
        p2 = p1;
        p1 -> next = NULL;
        }
    else{
        p2 -> next = p1;
        p1 -> next = NULL;
        p2 = p1;
        }
    printf("Enter data:");
    scanf("%c",&a);;
    }
    return start;       
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Srikanth S.
  • 41
  • 1
  • 6

1 Answers1

2

Change

scanf("%c",&a);

to

scanf(" %c",&a);
    // ^ Space here

Your scanf is reading your enter key as \n character

You could have seen it as I suspect the program outputs Enter data: twice each time.

Eregrith
  • 4,263
  • 18
  • 39