Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I. Create students: Create a Struct called Student with the following attributes

ID: 657336 • Letter: I

Question

I. Create students: Create a Struct called Student with the following attributes in a file named Student. c Name (String) GPA (double) Also in the Student. c file define functions setName (char* name[size])St getName() setID(int) setGPA(double GPA) getGPA() which will initialize/fetch the attributes All the function of the Student. c file should be declared/enlisted in Student. h file II. Create a linked list of students: Write a C file named LinkedLists.c which can instantiate a number of student as node and store them in it. The memory for each of them should be allocated dynamically using malloc The functions in the file should be able to Create a node to store a student instance Delete nth Student in the linked list Find nth student in the list Find all the information of a student given the ID Print info of all the students All the functions should be places in LinkedList file as well

Explanation / Answer

a)

#include <stdio.h>

#include <string.h>     

#include <stdlib.h>     

#include "studentfile.h" /* header file GPA lib */

#define BUFSIZE 1024

#define TRUE 1

#define FALSE 0

static

StudentNode highestLowestGPA(StudentNode head, int isFindHighest);

void set_Name(StudentNode sn)

{

    if(sn == NULL)

{

        fprintf(stderr, "Error: invalid argument passed to printStudent() ");

        return;

    }

    printf(" id: %d; name: %6s; birthday: %8s %2d, %d; GPA: %.2f ",

           sn->id, sn->name, gpa_monthString(sn->birthday.month),

           sn->birthday.day, sn->birthday.year, sn->gpa);

}     

int getId(StudentNode sn) {

    return sn->id;

}

char *getName(StudentNode sn)

{

    return sn->name;

}

double getGPA(StudentNode sn)

{

    return sn->gpa;

}

void setGPA(StudentNode sn, double gpa)

{

    sn->gpa = gpa;

}

StudentNode gpa_createStudent(int id, const char *name,

                     struct Birthday b, double gpa) {

    StudentNode sn;

    sn = (StudentNode) malloc(sizeof(struct ListStudent));

    if(sn == NULL) {

        fprintf(stderr, "Error: not enough memory. ");

        return NULL;

    }

    sn->id = id;

    sn->name = strdup(name);

    sn->birthday = b;

    sn->gpa = gpa;

    sn->next = NULL;

    return sn;

}

void gpa_printList(StudentNode head)

{

    while(head != NULL)

{

        set_Name(head);

        head = head->next;

    }

}  

StudentNode gpa_find(StudentNode head, int id, const char *name) {

    while(head!=NULL && !(!strcmp(head->name, name) &&

          head->id == id)) {

       head = head->next;

    }

    return head;

}

int gpa_aboveGPA(StudentNode head, double gpa) {

    int num;

    for(num=0; head != NULL; head=head->next) {

        if(head->gpa >= gpa) {

            set_Name(head);

            num++;

        }

    }

    return num;

}

void gpa_sortGPA(StudentNode *head) {

    StudentNode sni, min;      

    StudentNode remainingHead;

    remainingHead = *head;

    *head = NULL;             

    while(remainingHead != NULL) {      

        sni = remainingHead;

        min = sni;

        while(sni!= NULL) {

          if(sni->gpa < min->gpa) {

            min = sni;

          }

          sni = sni->next;

        }      

        gpa_delist(&remainingHead, min);           

        gpa_append(head, min);

    }

}

int gpa_insert(StudentNode *head, StudentNode sn, int pos) {

    StudentNode tmp = *head;

    int i = 0;  

    if(pos == 1) {

        *head = sn;

        (*head)->next = tmp;

        return 0;

    }

    while(tmp) {

        if(++i >= pos-1)

            break;

        tmp = tmp->next;

    }

    if(i != pos-1) {

        fprintf(stderr, "Error: Wrong position for insert(). ");

        return -1;

    }  

    sn->next = tmp->next;

    tmp->next = sn;

    return 0;

}

const char *gpa_monthString(int month)

{

    static const char *monthName[] = {"January", "February", "March",

           "April", "May", "June", "July", "August", "September",

           "October", "November", "December"};

    return (month < 1 || month > 12) ? NULL : monthName[month-1];

}

static

StudentNode highestLowestGPA(StudentNode head, int isFindHighest) {

    StudentNode sn;

    double gpa;

    gpa = head->gpa;     

    sn = head;          

    while(head != NULL) {

        if(isFindHighest == TRUE && head->gpa > gpa) {

            gpa = head->gpa;

            sn= head;

        }

        else if(isFindHighest == FALSE && head->gpa < gpa) {

            gpa = head->gpa;

            sn = head;

        }

        head = head->next;

    }

    return sn;

}

   

          

b)

LINKEDLIST.c

typedef struct _student

{

            int   id;                   

            char name[100];     

            float  gpa;                

}

student;

typedef struct _node

{

            student   k;

            struct _node    *next;

}

node, *list;

/* insert a student record into the list */

void insert_list (list *L, student k)

{

            node    *p;

            /* get a free node */

            p = (node *) malloc (sizeof (node));

            /* put the record in the list */

            p->k = k;

            /* link it to the first node in the list */

            p->next = *L;

            *L = p;

}

student *search_list (list L, int id)

{

            node    *p;

                        for (p=L; p && p->k.id != id; p=p->next);

                        if (p)

                        return &p->k;

            else

            return NULL;

}

/* make an empty list */

void create_list (list *L)

{

            *L = NULL;

}

Main code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include<LINKEDLIST.h>

int main ()

{

            list       C;       

            student            s, *p;

            int        id;

            FILE   *f;

            f = fopen ("List", "r");

            if (!f)

         {

                        perror ("List");

                        exit (1);

            }         

            create_list (&C);

            /* read student records, inserting them into the list */

            for (;;)       

        {

                        fscanf (f, "%d %s %f ", &s.id, s.name, &s.gpa);

                        /* reached end of file get out of loop */

                        if (feof (f)) break;

                        insert_list (&C, s);

            }

            fclose (f);

            /* ask for IDs, print records */

            for (;;)

         {

                        printf ("Enter student ID, -1 to finish: ");

                        scanf ("%d", &id);

                        if (id == -1) break;

                        /* find the student */

                        p = search_list (C, id);

                        /* If not found */

                        if (!p)

                                    printf ("ID #%d not found! ", id);

                        else

                        /* print the record */

                                    printf ("%d %s %0.2f ", p->id, p->name, p->gpa);

            }

            exit (0);

}