Thread: Function call to linked list

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    43

    Function call to linked list

    hello..i was wondering if anyone could take a look at my code which makes two linked lists from infiles. the lists work fine as i have tested printing them out i just need help to make a function that builds the link list instead of the way i have it now(i am not very good with functions). Any help would be much appreciated

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    struct info_node
    {
        char fname[20] ;
        char lname[20] ;
        int rank ;
        float score ;
        struct info_node * next ;
    } ;
    
    
    int main()
    {
    	FILE* infile ;
    	FILE* infile2 ;
    	struct info_node* link1, * link2 ;
     	struct info_node* top, * top2 ;	
    	struct info_node* move_pntr = NULL, * move_pntr2 = NULL ;
    	struct info_node* pre_pntr ;
    	struct info_node* current_pntr ;
    	int num = 0 ;
    
        infile = fopen("p3data.txt", "r") ;
    	infile2 = fopen("p3purge_data.txt", "r") ;
    
    	link1 = (info_node *)malloc(sizeof(info_node) ) ;
    	link2 = (info_node *)malloc(sizeof(info_node) ) ;
    
    		
    	fscanf( infile, "%s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;		 
    	fscanf( infile2, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
    
    	link1->next = NULL ;
    	link2->next = NULL ;
    
    	top = link1 ;
    	top2 = link2 ;
    
    	while( !feof(infile) )
        {
    		link1 = (info_node *)malloc(sizeof(info_node) ) ;
    		
    		
    		fscanf( infile, "%s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;	 
    		
    
    		link1->next = NULL ;
    		
    
    		//pre_pntr = top ;
    		//current_pntr = top ;
    
    		link1->next = top ;
    		top = link1 ; //save block of mem
    
    
    	}
    	while(!feof(infile2) )
    	{
    		link2 = (info_node *)malloc(sizeof(info_node) ) ;
    		fscanf( infile2, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
    		link2->next = NULL ;
    
    		link2->next = top2 ;
    
    		top2 = link2 ; //save block of mem
    
    	}
    
    fclose(infile);
    fclose(infile2);
    return (0) ;
    } //end of func main()

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Without looking terribly deeply into your code, what is wrong with it as-is?

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    nothing.. i need it to be in a function though

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So:
    1. Take the code that builds the linked list.
    2. Cut and paste it to somewhere else.
    3. Decide which variables need to be passed in from the main, and which can remain local to the function (and therefore should be taken out of main completely).
    4. Decide what should be returned by the function (hint: a pointer to the head of the list).
    5. Write a function signature and prototype and definition using that information.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    
    struct info_node
    {
    	char fname[20] ;
    	char lname[20] ;
    	int rank ;
    	float score ;
    	struct info_node * next ;
    } ;
    
    char *mangle_filename(const char *in)
    {
    	size_t len = strlen(in);
    	char *out, *end, *pos;
    
    	out = malloc(len + 6);
    
    	if(!out)
    		return 0;
    
    	pos = strrchr(in, '.');
    	strncpy(out, in, pos-in);
    	strcat(out, "_data");
    	strcat(out, pos);
    	return out;
    }
    
    void copied_and_pasted(const char *ifilename1)
    {
    	FILE* infile ;
    	FILE* infile2 ;
    	struct info_node* link1, * link2 ;
     	struct info_node* top, * top2 ;	
    	struct info_node* move_pntr = NULL, * move_pntr2 = NULL ;
    	struct info_node* pre_pntr ;
    	struct info_node* current_pntr ;
    	int num = 0 ;
    	char *ifilename2 = mangle_filename(ifilename1);
    
    	if(!ifilename2)
    	{
    		fputs("Uh oh... out of memory.", stderr);
    		return;
    	}
    
    	infile = fopen(ifilename1, "r") ;
    	infile2 = fopen(ifilename2, "r") ;
    	free(ofilename);
    
    	link1 = (info_node *)malloc(sizeof(info_node) ) ;
    	link2 = (info_node *)malloc(sizeof(info_node) ) ;
    
    		
    	fscanf( infile, "&#37;s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;		 
    	fscanf( infile2, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
    
    	link1->next = NULL ;
    	link2->next = NULL ;
    
    	top = link1 ;
    	top2 = link2 ;
    
    	while( !feof(infile) )
    	{
    		link1 = (info_node *)malloc(sizeof(info_node) ) ;
    		
    		
    		fscanf( infile, "%s%s%d%f", link1->fname , link1->lname, &link1->rank, &link1->score ) ;	 
    		
    
    		link1->next = NULL ;
    		
    
    		//pre_pntr = top ;
    		//current_pntr = top ;
    
    		link1->next = top ;
    		top = link1 ; //save block of mem
    
    
    	}
    	while(!feof(infile2) )
    	{
    		link2 = (info_node *)malloc(sizeof(info_node) ) ;
    		fscanf( infile2, "%s%s%d%f", link2->fname , link2->lname, &link2->rank, &link2->score ) ;
    		link2->next = NULL ;
    
    		link2->next = top2 ;
    
    		top2 = link2 ; //save block of mem
    
    	}
    
    	fclose(infile);
    	fclose(infile2);
    }
    
    int main()
    {
    	copied_and_pasted("p3data.txt");
    	return (0) ;
    } //end of func main()
    Last edited by master5001; 10-13-2008 at 05:32 PM.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I maybe added a grand total of a dozen lines to your existing code. But to be honest, you need to really look at some of your return values. Don't always just assume a file will open up. Don't always assume malloc() will give you a return value. Assume nothing about any area where errors can be introduced.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    tabstop..do you think i should be including getting the block of memory and the scanning of the infile inside this function?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The getting the block of memory definitely belongs inside the function.

    As to the scanning, that gets a little tricky. I would recommend passing in the values to be added to the list into the function, rather than reading it in. (This way your list can adapt to keyboard input, or even other file input.)

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well bless the OPs heart for not just taking my code and slapping it into his source and calling it a day

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    haha sorry master, the code works well but i would rather learn then copy and paste .

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Lol. No hard feelings. I didn't do anything more than copy and paste your main() function into another function. But I ultimately added that mangle_name() function just to make things a little more automated. Perhaps that will be a keeper for your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM