Thread: Adding a new node to linkded list

  1. #16
    Registered User
    Join Date
    Jul 2010
    Posts
    20
    Okay okay

    Code:
    void 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;
         }
    }

  2. #17
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    I tried with this function. For some reason, void with append is not working. I tried using an int return type. Well this works but now I am receiving "SIGSEGV" at the statement: q->link = temp; in append.
    I mean common adding an node at the end of the linked list should not be that challenging, right?
    Here is the sinppet I am working on
    Code:
    /* Porgram appends a node at the end of the linked list*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int  data;
    	struct node *link;
    };
    
    int main(int argc, char **argv)
    {
    	struct node *p;
    	int num, j;
    	p= (struct node *)malloc(sizeof(struct node));
    	if(p->link != NULL) //To make sure you ahve reached the end of the existing list
    	{
    		p= p->link;
    	}
    	
    	printf("\n Enter num:");
    	scanf("%d", &num);
    	
    	j = append(p,num);
    	
    	
    	//printf("\n hello world\n");
    	return 0;
    }
    
    
    int append(struct node *q, int num)
    {
    	struct node *temp;
    	
    		
    	temp = (struct node *)malloc(sizeof(struct node));
    	temp->data = num;
    	temp->link = NULL;
    	q->link = temp;
    	
    	temp = q;
    	while(temp->link!=NULL)
    	{	
    		printf("%d",temp->data);
    		temp = temp->link;
    		}
    	 
    	 return(1);
    }

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
        p= (struct node *)malloc(sizeof(struct node));
        if(p->link != NULL) //To make sure you ahve reached the end of the existing list
        {
            p= p->link;
        }
    What do you suppose this does?
    *p is uninitialized.
    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.

  4. #19
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    Thanks for your help so far...

    Using *p I am trying to reach the end of the linked list. So should I construct an linked list and then try to append it?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you don't have a linked list yet. You're initially creating it, so how can you walk it when it's empty?
    What's worse is that you don't actually initialize the list. You put it in a zombie state, so to speak. Create the list. Initialize its member properly.
    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.

  6. #21
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    Here is the snippet which works fine.
    My question now is the function append does not work with void return type. It works fine with a dummy return variable of an int. Any clue why is this happening?
    Code:
    /* Porgram appends a node at the end of the linked list*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    	int  data;
    	struct node *link;
    };
    int append(struct node *,int);
    int main(int argc, char **argv)
    {
    	struct node *p, *head, *curr, *temp;
    	int num,i;
    	
    	head = (struct node*)malloc(sizeof(struct node));
    	
    	curr = head;
    	
    	for(i=1;i<=5;i++)
    	{
    		curr ->link = (struct node*)malloc(sizeof(struct node));
    		curr ->data= i;
    		temp=curr;  // temp is required becoz curr will point to 6th member and 5th element pointer should be pointing to NULL
    		curr = curr->link;
    	}
    	temp->link =NULL;
    	//Display existing linked list
    	printf("Existing List is:");
    	curr = head;
    	while(curr->link != NULL)
    	{
    		printf("\n %d", curr->data);
    		curr = curr->link;
    	}
    	printf("\n %d", curr->data);
    	
    		
    	printf("\n Enter num to be appended:");
    	scanf("%d", &num);
    	
    	i = append(head,num);
    	
    	//Display list
    	curr = head;
    	printf("\n Appended list:");
    	while(curr->link!=NULL)
    	{
    		printf("\n %d", curr->data);
    		curr = curr->link;
    	}
    	printf("\n %d", curr->data);
    	//printf("\n hello world\n");
    	return 0;
    }
    
    
    int append(struct node *head, int num)
    {
    	struct node *temp,*q ;
    	temp = head;
    	do{
    		temp = temp->link;
    		}while(temp->link!=NULL);
    	
    	//printf("\n temp->data = %d \n temp->link =%d",temp->data, temp->link);
    	q = (struct node *)malloc(sizeof(struct node));
    	q->data = num;
    	q->link = NULL;
    	temp->link = q;
    	
    	return(1);
    }

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All functions need a return type specified, or need to be void. Your append function won't work if you pass it an empty list:
    Code:
    struct node *n = NULL;
    append( n, 4 );
    n will still not point to anything once the function ends, because the only way to update a variable's value inside a function is to pass a pointer to that variable, which you aren't doing, if you're trying to modify the value of n.


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

  8. #23
    Registered User
    Join Date
    Jul 2010
    Posts
    30
    append(head, num). So append has head of an initialized linked list and num which would scaned and passed to append. So all arguments are initialized. And I have tried using void and list is not appended then.

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The return type isn't what's making your function break.
    Code:
    void append( struct node *n, int v )
    {
        while( node && node->next )
            node = node->next;
        if( node )
        {
            struct node *t = malloc( sizeof *t );
            if( t )
            {
                t->value = v;
                t->next = NULL;
            }
            node->next = t;
        }
    }
    You have to actually have a node to pass for this to work, which is basically what you're trying to do anyway.

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

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