#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Please help!! reg: my code for insertion and print linked list


    i am trying this code for a very long time but can not pointing out where error is in this ... please anyone help me out by figuring out my error ....

    :o
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct Node
    {
    int data;
    struct Node* next;
    }node;


    node* insert(node** head1,int ni)
    {
    node *p;

    if((*head1)==NULL)
    {
    (*head1)=(node*)malloc(sizeof(node));
    p=*head1;
    }
    else
    {
    p=(node*)malloc(sizeof(node));
    p=p->next;
    p->next=NULL;
    }
    p->data=ni;

    return (*head1);

    }
    void print(node **head2)
    {
    node *d;
    d=*head2;
    while(d!=NULL)
    {
    printf("%d *",d->data);
    d=d->next;
    }

    }
    //void del(int nd);
    int main()
    {
    node* q;
    node* head=NULL;
    int ni,nd;
    char ch;
    while(1)
    {

    printf("\nEnter your choice :Insert - I Print - P Delete - D: ");
    scanf("%c",&ch);
    if(ch=='0')
    break;
    else
    {
    switch(ch)
    {
    case 'I' : printf("Enter the no: ");
    scanf("%d",&ni);
    q=insert(&head,ni);
    printf("\n*\n*%d*\n*\n",q->data);

    break;
    case 'P' : print(&head);
    break;
    case 'D' : printf("Enter the num u want to delete: ");
    scanf("%d",&nd);
    // del(nd);
    break;
    default : break;
    }
    getchar();
    }
    }
    return 0;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0
    It would also help if you described the error.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    sorry.. for that .. i m reposting this code


    Originally Posted by salem
    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.
    sorry.. for that .. i m reposting this code

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct Node
    {
    	int data;
    	struct Node* next;
    }node;
    
    
    node* insert(node** head1,int ni)
    {
    	node *p;
    
    	if((*head1)==NULL)
    	{
    		(*head1)=(node*)malloc(sizeof(node));
    		p=*head1;
    	}
    	else
    	{
    	          p->next=(node*)malloc(sizeof(node));
    		  p=p->next;
    		  p->next=NULL;
    	}
    	p->data=ni;
    
    	return (*head1);
    					
    }
    void print(node **head2)
    {
    	node *d;
    	d=*head2;
    	while(d!=NULL)
    	{
    		printf("%d *",d->data);
    		d=d->next;
    	}
    
    }
    //void del(int nd);
    int main()
    {
    	node* q;
    	node* head=NULL;
    	int ni,nd;
    	char ch;
    	while(1)
    	{
    
    	printf("\nEnter your choice :Insert - I  Print - P  Delete - D: ");	
    	scanf("%c",&ch);
    	if(ch=='0')
    		break;
    	else
    	{
    	switch(ch)
    	{
    		case 'I' : printf("Enter the no: ");
    			   scanf("%d",&ni);
    			   q=insert(&head,ni);
    			   printf("\n*\n*%d*\n*\n",q->data);
    
    			   break;
    		case 'P' : print(&head);
    			   break;
    		case 'D' : printf("Enter the num u want to delete: ");
    		   	   scanf("%d",&nd);
    //			   del(nd);	   
    			   break;
    		default  : break;	   
    	}
    	getchar();
    	}
    	}
    	return 0;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    sorry! i didn't mention the error ... actually the problem is this code is running correctly for insertion of very first num in empty list but when I am trying to insert the second num it shows seg. fault and shows (windows is checking a solution for the problem)
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > p->next=(node*)malloc(sizeof(node));
    What is p pointing to when you do this?

    Also, since this is a C program, there is no utility in casting malloc.
    You should be able to just do.
    p->next=malloc(sizeof(node));
    If you get error messages, you're doing something else wrong, and that needs to be fixed by doing something other than casting malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Thanks for your kind suggestion... i have tried the casting issue previously but that didn't help .... please help me if you can correct this code ... i have mentioned the error that it successfully insert the first num but giving error when i insert second no in linked list...
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Of course I could fix your code, but that wouldn't help you in learning how to diagnose and solve the same problem in future.
    Code:
    node* insert(node** head1,int ni)
    {
    	node *p;  // this is pointing at garbage
    
    	if((*head1)==NULL)
    	{
    		(*head1)=(node*)malloc(sizeof(node));
    		p=*head1;  // now p is pointing at valid memory
    	}
    	else
    	{
    	          // where is p pointing here, when you do p->next on it?
    	          p->next=(node*)malloc(sizeof(node));
    		  p=p->next;
    		  p->next=NULL;
    	}
    	p->data=ni;
    
    	return (*head1);
    					
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    thank you very much for pointing out my error .... i figure out it ... here is my new code and it works fine now... thanks again...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct Node
    {
    	int data;
    	struct Node* next;
    }node;
    
    
    node* insert(node** head1,node** temp,int ni)
    {
    	node *p;
    
    	if((*head1)==NULL)
    	{
    		(*head1)=(node*)malloc(sizeof(node));
    		p=*head1;
    	}
    	else
    	{
    	    	  p=*head1;
    	          while((p->next)!=NULL) p=p->next;  //here i was mistaking 
    		  p->next=malloc(sizeof(node));
    		  p=p->next;
    	}
    	p->data=ni;
    		  p->next=NULL;
    
    	return (p);
    					
    }
    void print(node **head2)
    {
    	node *d;
    	d=*head2;
    	while(d!=NULL)
    	{
    		printf("%d *",d->data);
    		d=d->next;
    	}
    
    }
    //void del(int nd);
    int main()
    {
    	node* q;
    	node* head=NULL;
    	q=head;
    	int ni,nd;
    	char ch;
    	while(1)
    	{
    
    	printf("\nEnter your choice :Insert - I  Print - P  Delete - D: ");	
    	scanf("%c",&ch);
    	if(ch=='0')
    		break;
    	else
    	{
    	switch(ch)
    	{
    		case 'I' : printf("Enter the no: ");
    			   scanf("%d",&ni);
    			   q=insert(&head,&q,ni);
    			   printf("\n*\n*%d*\n*\n",q->data);
    
    			   break;
    		case 'P' : print(&head);
    			   break;
    		case 'D' : printf("Enter the num u want to delete: ");
    		   	   scanf("%d",&nd);
    //			   del(nd);	   
    			   break;
    		default  : break;	   
    	}
    	getchar();
    	}
    	}
    	return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo