/* Jason Boxall 1/23/96 CSC 131 Lab #11 */

/* this program illustrates using a self-referential list of structures */

/* implemented as a stack. */

#include 

#include 

FILE *fin,*fout;

typedef struct studentinfo

    {

    char fname[10];

    char lname[15];

    int age;

    float gpa;

    struct studentinfo *next;

} RECORD;

RECORD *create(RECORD *);

void fill(RECORD *);

RECORD *push(RECORD *);

RECORD *pop(RECORD *);

void display(RECORD *);

void main()

    {

    fin=fopen("Lab11inp.dat","r");

    fout=fopen("Lab11out.rst","w");

    RECORD *first,*process;

    first=NULL;

    while(!feof(fin))

    first=create(first);

    display(first);

    first=pop(first);

    display(first);

    first=push(first);

    display(first);

}

RECORD *create(RECORD *first)

    {

    RECORD *process;

        if(first==NULL){

        first=(RECORD *)malloc(sizeof(RECORD));

        first->next=NULL;

    }

        else{

        process=(RECORD *)malloc(sizeof(RECORD));

        process->next=first;

        first=process;

    }

    fill(first);

    return first;

}

void fill(RECORD *first)

    {

    fscanf(fin,"%s%s%d%f",first->fname,

    first->lname,

    &first->age,

    &first->gpa);

}

RECORD *push(RECORD *first)

    {

    RECORD *process;

    process=(RECORD *)malloc(sizeof(RECORD));

    process->next=first;

    first=process;

    printf("\nEnter the following to create a new record:\n");

    fprintf(fout,"\nEnter the following to create a new record:\n");

    printf("First Name: ");

    scanf("%s",first->fname);

    fprintf(fout,"First Name: ");

    fprintf(fout,"%s\n",first->fname);

    printf("Last Name: ");

    scanf("%s",first->lname);

    fprintf(fout,"Last Name: ");

    fprintf(fout,"%s\n",first->lname);

    printf("Age:");

    scanf("%d",&first->age);

    fprintf(fout,"Age:");

    fprintf(fout,"%d\n",first->age);

    printf("GPA:");

    scanf("%4f",&first->gpa);

    fprintf(fout,"GPA:");

    fprintf(fout,"%4.2f\n",first->gpa);

    return first;

}

RECORD *pop(RECORD *first)

    {

    RECORD *tmp;

    tmp=first;

    first=first->next;

    free(tmp);

    return first;

}

void display(RECORD *first)

    {

        if(first==NULL){

        printf("The stack is empty.\n\n");

        fprintf(fout,"The stack is empty.\n\n");

    }

        else{

        printf("\nThe stack is:\n\n");

        fprintf(fout,"\nThe stack is:\n\n");

        printf("First Last \n");

        fprintf(fout,"First Last \n");

        printf("Name NameAgeQPR\n");

        fprintf(fout,"Name NameAgeQPR\n");

        printf("-------------------------------------\n\n");

        fprintf(fout,"-------------------------------------\n\n");

            while(first!=NULL){

            printf("%-10s%-15s%3d\t%5.2f\n",first->fname,

            first->lname,

            first->age,

            first->gpa);

            fprintf(fout,"%-10s%-15s%3d\t%7.2f\n",first->fname,

            first->lname,

            first->age,

            first->gpa);

            first=first->next;

        }

    }

}