0 Replies - 890 Views - Last Post: 31 March 2007 - 08:48 PM Rate Topic: -----

#1 kkgaming  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 75
  • Joined: 07-February 07

Link List (scanning from a in file)

Posted 31 March 2007 - 08:48 PM


#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;	
}

//how many elements are in the list
//pass it a pointer
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);
}

//pass a pointer from start of list and it does transversel
//if its not null it will print it out and if null it stops
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);
}

//This rights it to the outfile.
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;

//delete once ready

	for (i = 2; i <= 7; ++i)
	{
		tmp = GetNode(i);
		tail->next = tmp;
		tail = tmp;
	}
//walks through list calling printNode

	PrintList( head );

	fprintf(outfile, "Starting --\n");
	PrintListFile(outfile, head );
	fprintf(outfile, "Stopping --\n");
	i = Length( head );
	printf("List size: %d\n", i); 


	//test case above
  /* insertion sort */
	srand( time(0) );

	BasePtr start, p, add;
	int x;
	tmp = GetNode(x);//only works if the first number is the smallest (adjust to over come that weakness)
	start=tmp;

	for (i = 1; i < 10; ++i)
	{
		//x = rand() % 100 + 1; //make the program work with normal numbers (f scanf command)
		//printf("Inserting %d\n", x);
		fscanf(infile, "%d",p);

		add = GetNode(x);
		p = FindInsLoc( start, add );
		start = InsertNodeAfter( start, p, add );
		//printf("Version %d: \n", i);
		//PrintList( start );
	}

	printf("Sorted list ----- \n");
	PrintList( start );

	return 0;
}



Alright instead of printing random numbers in the list I am trying to scan in numbers I have in a infile which are (1, 2, 3, 4)...however its only printing out the 4 (the last element). I want it to print all those values. Here is just that code so its easier to read.

 /* insertion sort */
	srand( time(0) );

	BasePtr start, p, add;
	int x;
	tmp = GetNode(x);//only works if the first number is the smallest (adjust to over come that weakness)
	start=tmp;

	for (i = 1; i < 10; ++i)
	{
		//x = rand() % 100 + 1; //make the program work with normal numbers (f scanf command)
		//printf("Inserting %d\n", x);
		fscanf(infile, "%d",p);

		add = GetNode(x);
		p = FindInsLoc( start, add );
		start = InsertNodeAfter( start, p, add );
		//printf("Version %d: \n", i);
		//PrintList( start );
	}

	printf("Sorted list ----- \n");
	PrintList( start );

	return 0;
}



This post has been edited by kkgaming: 31 March 2007 - 09:14 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1