Search code examples
clinked-listdynamic-memory-allocationsingly-linked-listfunction-definition

Why is my compiler skipping function call?


#include<stdio.h>
#include<malloc.h>
struct node
{
    int data;
    struct node*next;
};
struct node*start;
void create(struct node*ptr)
{
    char ch;
    do
    {
     printf("Enter the data of node\n");
     scanf("%d",&ptr->data);
     fflush(stdin);
     printf("Do you wish to continue?(y/n)\n");
     ch=getchar();
     if(ch=='y')
     {
         ptr=ptr->next;
     }
     else
        ptr->next=NULL;
    }while(ch=='y');
}
void insert(struct node*ptr)
{
    struct node*p;
    p=(struct node*)malloc(sizeof(struct node));
    printf("Enter the value of data for node1\n");
    scanf("%d",&p->data);
    fflush(stdin);
    p->next=ptr;
    ptr=p;
}
void display(struct node*ptr)
{
    printf("Your Linked list is\n");
    while(ptr!=NULL)
    {
        printf("%d ",ptr->data);
        ptr=ptr->next;
    }
    printf("\n");
}
int main()
{
    printf("Hello and welcome to Linked List program\n");
    start=(struct node*)malloc(sizeof(struct node));
    create(start);
    display(start);
    printf("Let us now add a node to your linked list\n");
    insert(start);
    display(start);
    return 0;
}

My compiler is skipping function call insert and display. I've checked logic for all the functions they seems right to me. Moreover display and create before printf are working. The functions after print statement (i.e. insert and display function) are not working.


Solution

  • The function create can invoke undefined behavior if you will try to append one more node because in this case after this statement

    ptr=ptr->next;
    

    the pointer ptr has indeterminate value.

    At least you should write

     if(ch=='y')
     {
         ptr->next = malloc( sizeof( struct node ) );
         ptr = ptr->next;
     }
    

    Though you also need to check that the memory allocation was successful.

    The function insert does not change the original pointer start in this statement

    ptr=p;
    

    because the function deals with a copy of the value of the original pointer start. Instead it changes the local variable ptr.

    The function should be written at least like

    struct node * insert(struct node*ptr)
    {
        struct node*p;
        p=(struct node*)malloc(sizeof(struct node));
        printf("Enter the value of data for node1\n");
        scanf("%d",&p->data);
        fflush(stdin);
        p->next=ptr;
        return p;
    }
    

    and called like

    start = insert( start );
    

    Though again the function does not check whether the memory was allocated successfully.

    Pay attention to that it is a bad idea to declare the pointer start as a global variable.

    And for example memory allocation for the first node should not be done in main. It should be done in a function.

    Functions should do one thing for example to allocate a node and insert it in the list. Any prompts that ask the user to enter a value should be done either in main or in another function.