I'm having trouble calling my linked list of Insernode. Can some one help me please.
I need ot call it when it read teh data from teh file and puts it in the lists. I also need to use the list once i eneter in there data. adn if i enter is is there a way toadd the data the comparie inser the data that i have into file saved file.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Nlen 26
typedef struct person_t {
char FName[Nlen];
char LName[Nlen];
char Sex;
char Grade;
struct person_t *Next;
} PERSON;
/*********************************************************************
Funstion to Ordered linked list
This is where i will call the Last name and put it into the linked
list ordered by last name.
********************************************************************/
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty – Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
if (Here->LName > Node->LName) {
Node->Next = Top;
Top = Node;
}
else {/* Search for place to insert new node */
Prev = Top;
Here = Prev->Next;
while ( (Here != NULL) &&
(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
}
} /* end of Top != NULL */
return Top;
}
/*************************************************************
end of Funstion to Ordered linked list
***************************************************************/
void amend_file( char file[], PERSON *STU );
void show_file( char file[], PERSON *STU );
int main (void)
{
PERSON *STU;
int Ans;
char file[] = "data.txt";
int Records [100];
do
{
printf("\n\n"
"1. Enter new record\n"
"2. Display List\n"
"3. Quit\n\n");
printf("Prompt: ");
fflush(stdout);
if ( scanf("%d", &Ans) == 1 ) /* get answer */
{
STU = (PERSON*) malloc (sizeof (PERSON));
if (STU == NULL)
{
perror("PERSON");
exit(EXIT_FAILURE);
}
switch(Ans)
{
case 1:
amend_file(file, STU); //calls the amend file funcation
InsertNode; //calls the insert Node funcation which it is not getting into becuase
//Im haveing issues with getting the file to print.
break;
case 2:
show_file(file, STU); //calls teh show file funcation
break;
case 3: default:
Ans = 3;
break;
}
} /* /get answer */
}
while ( Ans != 3 );
fflush(stdout);
free(STU);
return EXIT_SUCCESS;
}
/*************************************************************************
* amend_file: Enter stuff into the working file.
*
**************************************************************************/
void amend_file( char file[], PERSON *STU )
{
assert(STU != NULL);
FILE *fin = fopen(file, "a+");
if ( fin == NULL )
{
perror(file);
exit(EXIT_FAILURE);
}
printf("Enter new record: ");
printf ("\nEnter students First name ( up to %d letters):", Nlen);
printf ("\nEnter students Last name ( up to %d letters):", Nlen);
printf ("\nPlease enter Sex");
printf ("\nPlease enter Grade\n");
scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade);
fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
fflush(fin);
fclose(fin);
}
/******************************************************************************
* show_file: Reads the working file.
* Bare bones again...
******************************************************************************/
void show_file ( char file[], PERSON *STU )
{
assert(STU != NULL);
int b = 1;
FILE *fout = fopen(file, "r");
if (file == NULL)
{
perror(file);
exit(EXIT_FAILURE);
}
while (fscanf(fout, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade) == 4)
{
printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
STU->Sex, STU->Grade);
b++;
// }
fclose(fout);
}