Thread: Link List problem

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Link List problem

    Dear Programmers,

    i have a linked list problem. I have have a number, for example 197, and put it in an array----> [0] = 1 , [1]=9, [2]=7

    So far so good. Now, i want to make a linked list of these numbers. I have made the following code:

    The problem is, that it's nog working. I think there is something wrong in the linking....

    Code:
    
                    int length = 0;
    	char buffer[50];			
    	itoa(num, buffer, 10);
    
    	//find the length of the charater number
    	for(int i=0; i<10; i++){
    		if(buffer[i] == '\0'){
    			length = i;
    		}
    	}
    	printf("%d\n",length);
    
    	struct node *head;
    	struct node *lp;
    
    	if(length>0){									//allocate memory for the first elemement.
    		//head = (node*)malloc(sizeof(struct node));
    		head =(struct node *) malloc(sizeof(struct node));
    		head->data = buffer[0];
    		printf("%c\n",head->data);
    		head->next=0;
    	}
    	lp = head;
    
    	for(int i=1; i<length;i++){				//because 0 is done above)
    		//lp->next = (node*)malloc(sizeof(struct node));
    		lp->next =(struct node *) malloc(sizeof(struct node));
    		//lp =(struct node *) malloc(sizeof(struct node));
    		lp = lp->next;
    		lp->data = buffer[i];
    		printf("%c\n",lp->data);
    		head->next=0;
    	}
    
    
    
    
    
    /*	printing the entire list */
    	//lp = head;
    	while( lp != NULL )					//continue whilst there are nodes left 
    	{
          printf("%c\n", lp->data );		//print out the current node           
          lp = lp->next;					//goto the next node in the list       
    	}
    
    
    return head;

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    By the looks of it your problem lies in the following code:
    Code:
    	for(int i=1; i<length;i++){				//because 0 is done above)
    		//lp->next = (node*)malloc(sizeof(struct node));
    		lp->next =(struct node *) malloc(sizeof(struct node));
    		//lp =(struct node *) malloc(sizeof(struct node));
    		lp = lp->next;
    		lp->data = buffer[i];
    		printf("%c\n",lp->data);
    		head->next=0;
    	}
    Consider one iteration of this loop. Initially lp and head are the same. You then allocate more memory for the next node using malloc - This bit is fine. You then reassign lp to be the next one in the chain using lp = lp->next, which is fine. Then you assign some content to the structure and print it to the screen, which is ok again, but then you assign the pointer head->next to zero, which is not fine.

    This basically creates a linked list and then removes the pointer from the 'head' structure in the list. Do you now understand the problem? I think what you mean is 'lp->next = 0', not 'head->next = 0'.

    Also, in this bit:
    Code:
    /*	printing the entire list */
    	//lp = head;
    	while( lp != NULL )					//continue whilst there are nodes left 
    	{
          printf("%c\n", lp->data );		//print out the current node           
          lp = lp->next;					//goto the next node in the list       
    	}
    You want to uncomment the 'lp = head' line. You want that in there to start from the top of the list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Linked List Problem
    By Brew in forum C Programming
    Replies: 6
    Last Post: 03-22-2003, 02:39 AM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM