[help] - adding to end of linkedlist

This is a discussion on [help] - adding to end of linkedlist within the C Programming forums, part of the General Programming Boards category; Hi, I am having problems with the following code, it seems to have gone into an infinite loop while trying ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    [help] - adding to end of linkedlist

    Hi,

    I am having problems with the following code, it seems to have gone into an infinite loop while trying to add current to the end of the list. I would much appreciate someone explaining where I have gone wrong and how I should fix it.

    Thanks heaps

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct list_el {
       char *val;
       struct list_el * next;
    };
    
    typedef struct list_el item;
    
    int main() {
    	item *current, *head;
    	head = NULL;
    	current = NULL;
    
    	// create file
    	FILE *battingFile;
    
    	// open file
    	battingFile = fopen("BATTING.txt","r");
    
    	// error if cannot open
    	if (battingFile == NULL) 
    	{
    		printf("Can't open input file!\n");
    	}
    
    	char line[300];
    
       	while (fgets(line, 300 ,battingFile)) 
    	{	
    		while(line[0] != '#')
    		{	
    			//printf("%s", line);
    			current = (item *)malloc(sizeof(item));
    			if(head == NULL)
    				{	
    					// add info to front of list
    					head = (item *)malloc(sizeof(item));
    					head->val = line;
    					current = head;
    				}
    				else
    				{
    					// go to the end of list
    					while(current->next != NULL)
    					{
    						current = current->next;
    					}
    					
    					// add to end of list
    					current->val = line;
    				}
    		}
      	}
    return 1;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, if it's a linked list, maybe you should be ... setting links? Right now you have 300 disconnected nodes, so you can't walk through the list at all.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    I modified the code to the following to actually connect the list, but am still the program is hanging, I know something is wrong but I just can't see it.

    Code:
    while (fgets(line, 300 ,battingFile)) 
    	{	
    		while(line[0] != '#')
    		{	
    			//printf("%s", line);
    			if(head == NULL)
    				{	
    					// add info to front of list
    					head = (item *)malloc(sizeof(item));
    					head->val = line;
    					head->next = NULL;
    				}
    				else
    				{
    					// go to the end of list
    					while(current == NULL)
    					{	
    						current = (item *)malloc(sizeof(item));
    						current->val = line;
    						current->next = NULL;
    						current = current->next;
    					}
    				}
    		}
      	}

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Nope, still not building a list there,
    You'll need to set something->next equal to something other than NULL for all nodes with successors.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Java Linked List implementation equivalent in C?
    By pbtrn10k in forum C Programming
    Replies: 8
    Last Post: 08-19-2009, 10:48 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  3. Something about a linkedlist program..
    By ozumsafa in forum C Programming
    Replies: 8
    Last Post: 10-17-2007, 01:14 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21