Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why exit code 139 at row no 47,what wrong in my code;

–1 vote
asked Nov 25, 2020 by Tarun Kumar Tarlana (350 points)
#include <stdio.h>
#include <stdlib.h>

  struct node
  {
      int data;
      struct node*link;
  }*head;
  //fun to create linkedlist
   void createlist(int n);
   void traverslist();
   
int main()
{
    int n; //no of ele to be in linkedlist
    printf("enter total no of node:" );
    scanf("%d",&n);
    
    createlist(n);
    traverslist();
    
    return 0;
}

 void createlist(int n)
 {
    struct node *temp,*newnode;
    int data, i;
    head =(struct node *)malloc(sizeof(struct node));/*allocated first node*/
    //terminate if memory not allocated
    if (head=NULL)
    {
        printf("unable to allocate memory");
        exit(1);
    }
    
    // take input from user
    printf("enter the data of node 1:");
    scanf("%d",&data);
     
     head->data=data;
     head->link=NULL;
     
     temp=head;
     for(i=2;i<=n;i++)
     {
         newnode=malloc(sizeof(struct node));
         
         if (newnode=NULL)
         {
             printf("unable to allocate memory");
            break;
         }
         
         printf("enterthe data of node %d:",i);
         scanf("%d",&data);
         
         
        newnode->data=data;
        newnode->link=NULL;
        
        temp->link=newnode;
        temp=temp->link;
        
     }
    
 }
 
  void traverslist()
  {
      struct node *temp;
      if(head =NULL)
      { printf("list is empty");
      return ;}
      
      temp=head;
      int i=1;
      while(temp!=NULL)
      {printf(" data %d:%d\n",i,temp->data);
      temp= temp->link;
        i++;  
      }
  }

1 Answer

0 votes
answered Nov 25, 2020 by xDELLx (10,500 points)
edited Nov 25, 2020 by xDELLx
    head =(struct node *)malloc(sizeof(struct node));/*allocated first node*/
    printf("Before if comparison address of head %x\n",head);
    //terminate if memory not allocated
    if (head=NULL)
    {
        printf("unable to allocate memory");
        exit(1);
    }
    printf("After if comparison address of head %x\n",head);
    // take input from user
    

What do you think does the red highlighted code do ??

Try to print the address of head before the If statement & after checking it.

Hint : Please check syntax to how to compare & the syntax to assign variables.

:)

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...