1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

link list

Discussion in 'C' started by coolbeans, Apr 4, 2007.

  1. coolbeans

    coolbeans New Member

    Joined:
    Apr 4, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys my assignment is to use link list and take numbers from a infile and print out and sort the numbers. My code is below. I have done everything except I am not sure why Its printing out a zero before my numbers...well I might know but I don't know how not to print it out. Any advice?



    -----------------------------------------whole code-----------------------------------------------

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    /* --------------------------------------------------------------------- */
    
    struct NumNodeType
    {
    	int dat;
    	struct NumNodeType *next;
    };
    
    /* Define new type names:  BaseElem   and   BasePtr   
     * to refer to the list nodes.  These new names can
     * be used in place of struct NumNodeType and
     * struct NumNodeType *  repsectively.
     */
    typedef struct NumNodeType BaseElem;
    typedef struct NumNodeType * BasePtr;
    
    
    /* --------------------------------------------------------------------- */
    
    BasePtr GetNode( int val )
    {
    	BasePtr node;
    	node = malloc( sizeof(BaseElem) );
    	node->dat = val;
    	node->next = NULL;
    	return node;	
    }
    
    
    int Length( BasePtr first )
    {
    	BasePtr move;
    	int len=0;
    	move = first;
    	while (move != NULL)
    	{
    		len++;
    		move = move->next;
    	}
    	return len;
    }
    
    
    void PrintNode( BasePtr node )
    {
    	printf("Val: %d\n", node->dat);
    }
    
    
    
    void PrintList( BasePtr first )
    {
    	BasePtr move;
    	move = first;
    	while (move != NULL)
    	{
    		PrintNode( move );
    		move = move->next;
    	}
    }
    
    void PrintNodeFile( FILE *ofp, BasePtr node )
    {
    	fprintf(ofp, "Val: %d\n", node->dat);
    }
    
    void PrintListFile( FILE *ofp, BasePtr first )
    {
    	BasePtr move;
    	move = first;
    	while (move != NULL)
    	{
    		PrintNodeFile( ofp,  move );
    		move = move->next;
    	}
    }
    
    int Compare( int x, int y)
    {
    	if (x < y) return -1;
    	else if (x == y) return 0;
    	else if (x > y) return 1;
    }
    
    BasePtr FindInsLoc( BasePtr first, BasePtr node )
    {
    	BasePtr move, prev;
    	int val1, val2;
    	int res = -1;
    	val2 = node->dat;
    	move = first;
    	prev = move;
    
    	val1 = move->dat;
    	res = Compare( val1, val2 );
    
    	while ( res < 0 && move != NULL)
    	{
    		prev = move;
    		move = move->next;
    		if (move != NULL)
    		{
    			val1 = move->dat;
    			res = Compare( val1, val2 );
    		}
    	}	
    	return prev;
    }
    
    BasePtr InsertNodeAfter( BasePtr first, BasePtr loc, BasePtr node)
    {
    	//printf("Insert %d after %d\n", node->dat, loc->dat);
    	node->next = loc->next;
    	loc->next = node;
    	return first;
    }
    
    
    /* --------------------------------------------------------------------- */
    
    int main( int argc, char *argv[] )
    {
    	BasePtr head, tail, tmp;
    	int i;
    
    	FILE *infile, *outfile;
    	/* verify that 2 parameters were passed */	
    	if ( argc != 3)
    	{
    		printf("Usage: a.out infile outfile\n");
    		return 1;
    	}
    	/* open input file */
    	infile = fopen( argv[1], "r");
    	if (!infile)
    	{
    		printf("ERROR: file %s not available\n", argv[1]);
    		return 1;	
    	}
    	/* open output file */
    	outfile = fopen( argv[2], "w");
    	if (!outfile)
    	{
    		printf("ERROR: file %s not available\n", argv[2]);
    		return 1;	
    	}
         /*
    	tmp = GetNode(1);
    	head=tmp;
    	tail=tmp;
    
    	for (i = 2; i <= 7; ++i)
    	{
    		 
    		tmp = GetNode(i);
    		tail->next = tmp;
    		tail = tmp;
    	}
    
    	PrintList( head );
    
    	fprintf(outfile, "Starting --\n");
    	PrintListFile(outfile, head );
    	fprintf(outfile, "Stopping --\n");
    	i = Length( head );
    	printf("List size: %d\n", i); 
    
         */
    	/* now make it work some some random data; the above
    	 * code is a test case in a controlled situation to make
    	 * sure things appear to work
    	 */
      /* insertion sort */
    
    	BasePtr start, p, add;
    	int x;
    	int length;
    	
    	tmp = GetNode(0);
    	start=tmp;
    
    	for (i = 0; i < 10; ++i)
    	{
    		if(fscanf(infile," %d", &x) !=1)
    		{
    		   break;
    		   }
    		printf("Adding node with data value %d\n", x); /* for debugging purpose */
    		add = GetNode(x);
    		p = FindInsLoc( start, add );
    	    start = InsertNodeAfter( start, p, add );
    	   
    	}
        printf("\n Total number of nodes added = %d\n", i);
    	printf("Sorted list ----- \n");
    	PrintList( start );
    
    	return 0;
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Do you expect a helpful reply with not much info provided apart from the lines and lines of code.

    Refer to my post here
     
  3. coolbeans

    coolbeans New Member

    Joined:
    Apr 4, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Sorry thought I gave enough there. Alright I will explain more what I am trying to do and what I have done. :)

    Alright, first my goal was to take some numbers from an infile or inputfile (any numbers). They happend to be 8,0,4,9...in the input file. Then I scanf the infile for numbers below with a fscanf and then they are passed up to some helper function which I will explain If I need to. Below is the scanning part.

    Code:
    /* insertion sort */
    
    	BasePtr start, p, add;
    	int x;
    	int length;
    	
    	tmp = GetNode(0);      ----->This line I believe might be my downfall, I believe it      only works if the first number is the smallest.  
    	
            start=tmp;
    
    	for (i = 0; i < 10; ++i)
    	{
    		if(fscanf(infile," %d", &x) !=1)
    		{
    		   break;
    		   }
    		printf("Adding node with data value %d\n", x); /* for debugging purpose */
    		add = GetNode(x);
    		p = FindInsLoc( start, add );
    	    start = InsertNodeAfter( start, p, add );
    	   
    	}
        printf("\n Total number of nodes added = %d\n", i);
    	printf("Sorted list ----- \n");
    	PrintList( start );
    
    	return 0;
    }  
    ---------My output below-----------

    nuke@nuke:~$ ./a.out infile outfile
    Adding node with data value 8
    Adding node with data value 4
    Adding node with data value 0
    Adding node with data value 9

    Total number of nodes added = 4
    Sorted list -----
    Val: 0
    Val: 0
    Val: 4
    Val: 8
    Val: 9
    nuke@nuke:~$

    So I am not sure how I would get rid of that zero...the first one because there is a zero in my infile.
    :) :) :) :) :) :)
     
    Last edited by a moderator: Apr 4, 2007
  4. coolbeans

    coolbeans New Member

    Joined:
    Apr 4, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    sorry for all the smilies...didn't realize they posted at the bottom.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    You list contains a blank node at the start. Check the InsertNodeAfter

    Also you are not upadting the start in the function but just passing and returning.
     

Share This Page