Hey guys could someone please assist me in sorting a linked list by a particular element for example by surname. So when the application reads the file it sorts data in the file by surname.
Code:
void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
{
/* declaration of variables*/
char* description;
char* surname;
char* firstName;
char* address;
char* suburb;
char sTmp[100];
char cTmp[100];
char* unitpricePtr;
char* stocklevelPtr;
char* postCodePtr;
char* phoneNumPtr;
float unitprice;
int stocklevel;
int postCode;
int phoneNum;
/* pointing nodes to customer variables*/
CustomerNodeType* newCust;
CustomerNodeType* curCust;
CustomerNodeType* prevCust;
StockNodeType* newStock;
StockNodeType* curStock;
StockNodeType* prevStock;
/* variables for files*/
FILE *cFile;
FILE *sFile;
if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
stockFile,"r")) == NULL)
{
fprintf(stderr, "error opening file\n");
exit(EXIT_FAILURE);
}
else
{
printf("file was opened correctly");
}
while(fgets(cTmp, 100, cFile ) != NULL)
{
/* tokenizing variables*/
surname = strtok(cTmp, ",");
firstName = strtok(NULL, ",");
address = strtok(NULL, ",");
suburb = strtok(NULL, ",");
postCodePtr = strtok(NULL, ",");
phoneNumPtr = strtok(NULL,"\0");
postCode = atoi(postCodePtr);
phoneNum = atoi(phoneNumPtr);
/* allocating memory using malloc for nodes*/
newCust = malloc(sizeof(*newCust));
/* start of linked list*/
curCust = ts -> headCust;
prevCust = NULL;
while(curCust !=NULL)
{
prevCust = curCust;
curCust = curCust -> nextCust;
}
/* copy the string variables into newCust node*/
strcpy(newCust->surname,surname);
strcpy(newCust->firstName,firstName);
strcpy(newCust->address,address);
strcpy(newCust->suburb,suburb);
/* newCust is postCode*/
newCust->postCode=postCode;
/* newCust is phoneNum*/
newCust->phoneNum=phoneNum;
/* printing the preloaded data*/
printf("\t%s", newCust -> firstName);
printf("\t%s", newCust -> surname);
printf("\t%s", newCust -> address);
printf("\t%s", newCust -> suburb);
printf("\t%d", newCust -> postCode);
printf("\t%d", newCust -> phoneNum);
/* keep count of each customer in loop*/
ts -> customerCount++;
/* start of linked list*/
if(prevCust == NULL)
{
ts ->headCust = newCust;
}
else
{
prevCust -> nextCust = newCust;
}
/* if there is a previous node get the next node and make
it the new node*/
}
fclose(cFile);
}