0

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);
    }
}
  • Have you tried stepping through it with a debugger? – Barmar Oct 04 '18 at 17:10
  • [don't cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Oct 04 '18 at 17:11
  • Apart from the answer Barmar gave, the last line in the while loop `scanf("%c",&ch)` will read a new line character, if the user presses Enter after typing in the integer value. So the while loop will break out even if the user did not type any character except `'y' or 'Y'`. – GAURANG VYAS Oct 04 '18 at 17:24
  • Thanks everyone. And @Barmar I am learning basics now. Don't understand how does debugger works. So, did not do anything with debugger. – Meraj Alam Oct 04 '18 at 18:07
  • and thanks @GAURANGVYAS too. I understood your what you said and can you please tell me how to fix this? – Meraj Alam Oct 04 '18 at 18:10
  • Learning to use the debugger should be your first step. It's necessary for any effective programming. – Barmar Oct 04 '18 at 19:13
  • @MerajAlam Change it to `scanf(" %c", &ch);`. The space at the beginning tells `scanf()` to skip over any whitespace before reading a character. – Barmar Oct 04 '18 at 19:15
  • Better yet, stop using `scanf()` completely. Use `fgets()` to read a line at a time. If you need to parse the line you can use `sscanf()`. – Barmar Oct 04 '18 at 19:16
  • Yeah, it ran. Thanks for your help. – Meraj Alam Oct 05 '18 at 10:46

1 Answers1

2

When the list is not empty, your insertAtBeginning() function first links the new element to the old list, then it does:

temp->link = NULL;

so that the link to the old list contents is lost. That should only be done when creating the first element of the list. It should be in an else clause.

You can also take *head = temp; out of the if block, since it needs to be done in either case.

void insertAtBeginning(Node** head, int dat) {
    Node *temp = malloc(sizeof(Node));
    temp->data = dat;
    if(*head != NULL){
        temp->link = *head;
    } else {
        temp->link = NULL;
    }
    *head = temp;
}

However, now that I look at it, the if is not necessary, since *head will be NULL in exactly the case where you want to assign NULL. So it can just be:

void insertAtBeginning(Node** head, int dat) {
    Node *temp = malloc(sizeof(Node));
    temp->data = dat;
    temp->link = *head;
    *head = temp;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612