|
Newbie
Join Date: Oct 2005
Posts: 7
Rep Power: 0 
|
well im really trying hard but these link lists are ... difficult
#include <stdio.h>
#include <stdlib.h>
#define CITYNAMESIZE 30
struct cityRecord
{
double x, y;
char city[CITYNAMESIZE];
struct cityRecord *next;
};
typedef struct cityRecord cityNode;
void getFiles(char filename[]);
FILE* fileOpen(char *filename, char *mode);
void exitIfFault(FILE *fp, char filename[]);
void reorderList();
void printDistance();
void calculateDistance();
void selectFirstNode();
void printList();
cityNode* createNode(double x, double y, char tempCity);
void addIntoLinkList(char tempCity, double x, double y);
int main(int argc, char *argv[])
{
cityNode *start;
start = NULL;
getFiles(argv[1]);
/* printList */
/* selectFirstNode */
/* calculateDistance */
/* printDistance */
/* reorderList */
/* printList */
/* calculateDistance */
/* printDistance */
return 0;
}
void printList()
{
}
void selectFirstNode()
{
}
void calculateDistance()
{
}
void printDistance()
{
}
void reorderList()
{
}
cityNode* createNode(double x, double y, char tempCity)
{
cityNode *tempPtr;
tempPtr = (cityNode*) malloc(sizeof(cityNode));
if(tempPtr == NULL)
{
printf("Memory allocation error\n");
exit(EXIT_FAILURE);
}
tempPtr->x = x;
tempPtr->y = y;
tempPtr->city = tempCity;
tempPtr->next = NULL;
return tempPtr;
}
void addIntoLinkList(char tempCity, double x, double y)
{
cityNode *temp, *prev, *loc;
temp = createNode(tempCity,x,y);
if(*start ==NULL)
{
/* add first item to the list */
*start = temp;
}
}
void getFiles(char filename[])
{
FILE *fp; /* defines a file pointer */
int status;
double x, y;
char tempCity[CITYNAMESIZE];
/* open stream to file */
fp=fileOpen(filename, "r");
while(!feof(fp))
{
/* lf -long float = double*/
status = fscanf(fp, "%s%lf%lf", tempCity, &x, &y);
if(status == -1)
{
/* do nothing */
}
else if(status == 3) /* if there are 3 arg in the line in the file */
{
addIntoLinkList(tempCity,x,y);
/* addnode */
/* no printing here */
printf("%-12s %-8.2f %-8.2f\n", tempCity, x, y);
}
else
{
printf("\n");
printf("ERROR - Corrupted record in file\n");
exit(0);
}
}
}
FILE* fileOpen(char *filename, char *mode)
{
FILE *fp = fopen(filename, mode);
if(fp == NULL)
{
printf("ERROR - Unable to access %s\n", filename);
exit(1);
}
return fp;
}
|