0 Replies - 1345 Views - Last Post: 16 September 2008 - 10:36 AM Rate Topic: -----

#1 sat_0101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-September 08

how to implement this code using linked list

Posted 16 September 2008 - 10:36 AM

I have included the C++ program and a set of sample data. This program is written using array. This program just reads the input data and prints it. How do I convert this code using a linked list and the date has to be sorted. The date 1998-21-21 comes before 1990-11-39. The sample data is given below. This has to implemented with linked list. Can someone help me with this issue.



#include	<stdio.h>
#include	<iostream.h>
#include	<ctype.h>
#include	<stdlib.h>
#include	<string.h>
#include	<strings.h>

//----------------------------------------------------------------------
// MACRO definitions
//----------------------------------------------------------------------
#define EQUAL		!strcmp

#define NUKE_NL(x)	{ char *_nl = &x[strlen(x)-1];	\
			  if(*x && *_nl=='\n') *_nl = '\0';		\
		}


//----------------------------------------------------------------------
// TYPE definitions
//----------------------------------------------------------------------
typedef	struct {
	char	*cust;
	char	*prod;
	short	day;
	short	month;
	short	year;
	char	*state;
	long	quant;
}	Sale;

typedef	struct {
	char	*prog_name;
	char	*infile_name;
	char	*outfile_name;
	FILE	*infile;
	FILE	*outfile;
	int		num_records;
}	GlobalInfo;


//----------------------------------------------------------------------
// FUNCTION PROTOTYPE declaration
//----------------------------------------------------------------------
FILE	*open_file(char*, char*);
short	read_record(Sale&);
void	output_record(Sale, short);



//----------------------------------------------------------------------
// GLOBAL VARIABLES declaration
//----------------------------------------------------------------------
GlobalInfo	G;
Sale		Sales[1000];


//----------------------------------------------------------------------
int	main(int argc, char *argv[])
//----------------------------------------------------------------------
{
	G.prog_name=strdup(argv[0]);

	//--------------------------------------------------------------
	// check for command line arguments
	//--------------------------------------------------------------
	if (argc != 2) {
		cerr << "Usage: " << G.prog_name << " <input file containing Sales data>" << endl;
		exit(1);
	}

	//--------------------------------------------------------------
	// check for the validity of the input file - by opening it
	//--------------------------------------------------------------
	G.infile_name=strdup(argv[1]);
	if ((G.infile = open_file(G.infile_name, "r")) == (FILE*)NULL) {
		cerr << G.prog_name << ": Failed to read the input file specified [" << G.infile_name << "]."
			<< endl;
		exit(1);
	}

	//--------------------------------------------------------------
	// read records from the input file
	//--------------------------------------------------------------
	int	i=0;
	while (read_record(Sales[i])) {
		output_record(Sales[i], i);
		i++;
	}

	G.num_records=i;
} // main()


//----------------------------------------------------------------------
FILE*	open_file(char *file_name, char *type)
//----------------------------------------------------------------------
{
	FILE	*fp;

	if (!(fp = fopen(file_name, type))) {
		fprintf(stderr, "Error Opening File <%s> for [%s]\n",
			file_name, type);
		exit (1);
	}

	return fp;
} // open_file()


//----------------------------------------------------------------------
short	read_record(Sale &a_rec)
//----------------------------------------------------------------------
// reads a record from the input file and returns 1 upon a successful read.
// in case of failure or EOF, it will return 0.
{
	char	line[1024];
	char	*begin,
			*end;
	short	field=0;

	if (fgets(line, 1024, G.infile) == (char*)NULL) {
		return 0;
	}
	// remove a NL - new line char - at the end
	NUKE_NL(line);

	begin=line;
	while ((end=strchr(begin, '|')) != (char*)NULL) {
		field++;
		*end=NULL;

		switch(field) {
			case	1:
				a_rec.cust=strdup(begin);	break;
			case	2:
				a_rec.prod=strdup(begin);	break;
			case	3:
				a_rec.day=atoi(begin);		break;
			case	4:
				a_rec.month=atoi(begin);	break;
			case	5:
				a_rec.year=atoi(begin);		break;
			case	6:
				a_rec.state=strdup(begin);	break;
		}

		begin=end+1;
	}

	// last field is not delimited by a '|'
	a_rec.quant=atoi(begin);
	return 1;
} // read_rec()


//----------------------------------------------------------------------
void	output_record(Sale a_rec, short	rec_num)
//----------------------------------------------------------------------
{
	char	tmpdate[16];

	sprintf(tmpdate, "%d/%d/%d", a_rec.month, a_rec.day, a_rec.year);

	cout << "Record " << rec_num+1 << ":" << endl;
	cout << "\tCustomer :\t" << a_rec.cust << endl;
	cout << "\tProduct  :\t" << a_rec.prod << endl;
	cout << "\tSaleDate :\t" << tmpdate << endl;
	cout << "\tQuantity :\t" << a_rec.quant << endl << endl;
}




sample data:

Bloom|Pepsi|2|12|1991|NY|4232
Knuth|Bread|23|5|1995|PA|4167
Emily|Pepsi|22|1|1996|CN|4404
Emily|Fruits|11|1|1990|NJ|4369
Helen|Milk|7|11|1996|CN|210
Emily|Soap|2|4|1992|CN|2549
Bloom|Eggs|30|11|1990|NJ|559
Bloom|Yogurt|25|7|1994|PA|17
Helen|Pepsi|14|3|1992|NJ|3891
Emily|Bread|28|9|1995|PA|42
Sam|Cookies|20|11|1994|NY|3376
Knuth|Milk|5|2|1997|PA|126
Helen|Coke|11|4|1991|NY|668
Emily|Butter|5|7|1995|NJ|3840
Emily|Yogurt|7|10|1995|NY|730
Sam|Soap|12|2|1991|NJ|165
Knuth|Coke|6|1|1993|CN|1557
Sam|Milk|9|8|1991|NY|1132
Helen|Yogurt|6|2|1990|PA|4001
Sam|Milk|6|1|1993|PA|2298
Knuth|Pepsi|21|12|1998|CN|653
Knuth|Eggs|19|12|1996|NJ|1339
Helen|Coke|19|10|1992|NJ|2662
Knuth|Milk|25|3|1991|CN|58
Bloom|Yogurt|3|4|1991|NJ|1203
Helen|Milk|6|11|1991|NY|2422

Is This A Good Question/Topic? 0
  • +

Page 1 of 1