Thread: Adding a new node to linkded list

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    30

    Adding a new node to linkded list

    Hi there. I am trying to add a node to linked list using function append(). It is giving me SegementViolation error at the highlighted lines. Does anybody have any clue as to what is wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int  data;
    	struct node *link;
    };
    
    int main(int argc, char **argv)
    {
    	struct node *p;
    	int num;
    	p= (struct node *)malloc(sizeof(struct node));
    	if(p->link != NULL)
    	{
    		p= p->link;
    	}
    	
    	printf("\n Enter num:");
    	scanf("%d", &num);
    	
    	append(p,num);
    	
    	printf("\n hello world\n");
    	return 0;
    }
    
    
    append(struct node **q, int num)
    {
    	struct node *temp,*r;
    	printf("\n in append");
    	
    	r = *q;
    	
    	temp = (struct node *)malloc(sizeof(struct node));
    	temp->data = num;
    	temp->link = NULL;
    	r->link = temp;
    	
    	//Display linked list elements 
    	temp = *q;
    		printf("\n Before while");
    		while(temp->link != NULL)
    	 {
    		  printf("%d",temp->data);
    		 temp = temp->link;
    	 }
    	 
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    append(p,num); // p is declared as pointer to node
    VS
    append(struct node **q, int num);

    It's a bad habit to ignore compiler warnings.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    In above snippet, I am receiving signal SIGSEGV at lines:
    1. append(p,num);
    2. r = *q;

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    I did not understand. Can you explain in details ? Please.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What does the compiler tell you when you compile this code?

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    I did not understand. Can you explain in details ? Please.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    Compiler gives the error"Program Received signal SIGSEGV".

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    That's not the compiler. That's when you run it.

    append() takes a double pointer to node. You are passing in a single pointer to node. The compiler should have warned you about this.

    Also, append() is declared without a return type, that is bad.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    No compiler gives 0 errors and 0 warnings. It is during the run I encounter this error.
    I stated the return type for append() and after that I do not receive any SIGSEGV error. Now the output screens do scan for the num but do not display the final output.
    Thank you for your help.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you aren't getting warnings then you aren't compiling with warnings enabled. At a bare minimum, your original code will complain that you are passing it the incorrect type to append.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    I do get warnings but I am not getting any of them now!!

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    20
    Maybe this will help you:

    Code:
    append(struct node *q, int num)
    {
         struct node *temp;
         printf("\n in append");
         temp = (struct node *)malloc(sizeof(struct node));
         temp->data = num;
         temp->link = NULL;
         q->link = temp;
         temp = q;
         printf("\n Before while");
         while(temp->link != NULL)
         {
              printf("%d",temp->data);
              temp = temp->link;
         }
    }
    For me, it prints zero after "Before while" because num is stored in a new node instead of current node.
    It doesn't print the inserted number because new node's link points to NULL.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're still missing a fact: append lacks a return type. And you are not returning anything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    20
    Could be void... it isn't that significant at this point. I concentrated on logical errors rather than syntactical errors. Gcc didn't throw any errors/warnings though.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But it isn't void.
    You need to tell it to be strict. Compile with C99, all warnings and pedantic.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Debugging my AVL tree program.
    By Nextstopearth in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 01:48 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Linked list probs
    By mouse163 in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2005, 05:41 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM