Question

In: Computer Science

Using the 3 program files below to read the sample students.txt file and create a singly...

Using the 3 program files below to read the sample students.txt file and create a singly linked-list of students. The code in sll_list.h and mainDriver.c shouldn't be changed, only sll_list.c. The finished program should print out a list that initializes the sampled 5 students and allows the user to use the functions found in mainDriver.c.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// mainDriver.c //

#include "sll_list.h"

int main()

{

   int choice,list_size,id;

   do

   {

       printf("1. initialize list of students\n2. add additional student to front of list\n3. add additional student to rear of list\n4. delete student\n5. sort students alphabetically\n6. sort students by idNum\n7. show number of students in list\n8. print students\n9. quit program\n");

     scanf("%d",&choice);

       list_t l;

       l.head=NULL;

       l.tail=NULL;

       switch(choice)

       {

           case 1://initialize list of students

                   scanf("%d",list_size);

                   l.size=list_size;

                   intializeList(list)t *list, FILE *inFile);

                   break;

           case 2://add additional student to front of the list:

                   addToFront(l);

                   break;

           case 3://add additional student to tail of the list:

                   addToRear(l);

                   break;

           case 4://delete student

                   scanf("%d",id);

                   deleteStudent(l,id);

                   break;

           case 5://sort students aplhabetically

                   sortListByLN(l);

                   break;

           case 6://sort students by id

                   sortListByNum(l);

                   break;

           case 7:   //number of students in the list

                   size(l);

                   break;

           case 8:   //printing students in the list

                   printStudents(l);

                   break;

           case 9:

                   printf("quiting from the program");

                   break;

       }

   }while(choice!=9);

   return 0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

// sll_list.c //

#include "sll_list.h"

list_t *newList(){

list_t *m;

m=(list_t *)malloc(sizeof(list_t));

m->head = NULL;

m->tail = NULL;

m->size = 0;

return m;

}

student_t *addStudent( FILE *inFile, int whichInput)

{

student_t *k;

sscanf(inFile, "%d %d %d %s %d %s %s %f", &(k->cuid),&(k->age),&(k->dob.day),

(k->dob.month),&(k->dob.year),(k->firstName),(k->lastName),&(k->gpa));

return k;

}

int printMenu(){

int choice;

    printf("1. initialize list of students\n2. add additional student to front of list\n3. add additional student to rear of list\n4. delete student\n5. sort students alphabetically\n6. sort students by idNum\n7. show number of students in list\n8. print students\n9. quit program\n");

    scanf("%d",&choice);

    while( 0>choice && choice>10){

    printf("Enter valid choice 1-9");

    printf("1. initialize list of students\n2. add additional student to front of list\n3. add additional student to rear of list\n4. delete student\n5. sort students alphabetically\n6. sort students by idNum\n7. show number of students in list\n8. print students\n9. quit program\n");

    scanf("%d",&choice);

}

return choice;

}

void intializeList(list_t *list, FILE *inFile)

{

int size = 0;

int whichInput= 0;

sscanf(inFile, "%d", &size);

for(size; size > 0; size--);

{

    addToFront(list, whichInput *inFile);

}

}

void addToFront(list_t *list, int whichInput, FILE *inFile)

{

student_t *k;

k=addStudent(*inFile, whichInput);

k->next = list->head->next;

list->head->next = k;

}

void addToRear( list_t *list, int whichInput, FILE *inFile )

{

student_t *k;

k=(student_t *)malloc(sizeof(student_t));

scanf("%d %d %d %s %d %s %s %f",&(k->cuid),&(k->age),&(k->dob.day),(k->dob.month),&(k->dob.year),(k->firstName),(k->lastName),&(k->gpa));

k->next=l.tail;

l.tail=k;

}

void deleteStudent(list_t l,int cuid)

{

student_t *k,*p;

if(l.head!=NULL)

{

    k=l.head;

    while(k!=NULL)

    {

      if(k->cuid==cuid)

      {

        p->next=k->next;

        free(k);

        break;

      }

      else

      {

        p=k;

        k=k->next;

      }

    }

}

}

void sortListByLN( list_t *list )

{

student_t *t,*p;

int i=0;

if(l.head==NULL)

{

}

else{

    t = l.head;

    p=NULL;

    while(t->next != p)

    {

      if(strcmp(t->firstName,t->next->firstName)>0)

      {

        swap(t,p);

        i==1;

      }

      t=t->next;

    }

    p=t;

}

}

void swap(student_t *s,student_t *p)

{

student_t temp;

temp.age=s->age;

temp.cuid=s->cuid;

temp.gpa=s->gpa;

temp.dob.day=s->dob.day;

strcpy(temp.dob.month, s->dob.month);

temp.dob.year=s->dob.year;

strcpy(temp.firstName,s->firstName);

strcpy(temp.lastName,s->lastName);

s->age=p->age;

s->cuid=p->cuid;

s->gpa=p->gpa;

s->dob.day=p->dob.day;

strcpy(s->dob.month, p->dob.month);

s->dob.year=p->dob.year;

strcpy(s->firstName,p->firstName);

strcpy(s->lastName,p->lastName);

p->age=temp.age;

p->cuid=temp.cuid;

p->gpa=temp.gpa;

p->dob.day=temp.dob.day;

strcpy(p->dob.month, temp.dob.month);

p->dob.year=temp.dob.year;

strcpy(p->firstName,temp.firstName);

strcpy(p->lastName,temp.lastName);

}

void sortListByNum( list_t *list )

{

student_t *t,*p;

int i=0;

if(l.head==NULL)

{

}

else{

    t = l.head;

    p=NULL;

    while(t->next != p)

    {

      if(t->cuid>t->next->cuid)

      {

        swap(t,p);

        i==1;

      }

      t=t->next;

    }

    p=t;

}

}

void count_students(list_t l)

{

student_t temp;

int count=0;

if(l.head==NULL)

{

    printf("Student list is empty");

}

else

{

    student_t *temp=l.head;

    count=0;

    while(temp!=NULL)

    {

      count++;

      temp=temp->next;

    }

    printf("student list contains:%d students",count);

}

}

void print_students(list_t l)

{

student_t *temp;

if(l.head==NULL)

{

    printf("Student list is empty");

}

else

{

    temp=l.head;

    printf("id\t last_name\t firstname\t date\t month\t year\t gpa\n");

    while(temp!=NULL)

    {

      printf("%d \t%s \t%s \t%d \t%s \t%d \t%f \n",temp->cuid,temp->lastName,temp->firstName,temp->dob.day,temp->dob.month,temp->dob.year,temp->gpa);

      temp=temp->next;

    }

}

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// sll_list.h //

#ifndef SLL_LIST_H

#define SLL_LIST_H

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct {

                char month[4];

                int day, year;

} dateOfBirth;

typedef struct student {

int age;

float gpa;

int cuid;

dateOfBirth dob;

char lastName[20];

char firstName[15];

struct student *next;

} student_t;

typedef struct list {

student_t *head;

student_t *tail;

int size;

} list_t;

list_t *newList( );

student_t *addStudent( FILE *inFile, int whichInput );

int printMenu( );

void initializeList( list_t *list, FILE *inFile );

void addToFront( list_t *list, int whichInput, FILE *inFile );

void addToRear( list_t *list, int whichInput, FILE *inFile );

void deleteStudent( list_t *list, int idNum );

void swap( student_t *s, student_t *p);

void sortListByLN( list_t *list );

void sortListByNum( list_t *list );

int size( list_t *list );

int isEmpty( list_t *list );

void printStudents ( list_t *list );

#endif /* SLL_LIST_H */

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// student.txt //

5

19 3.2 123456789 Jan 31 1999 Smith David

20 3.0 456789123 Mar 7 1998 Jackson Susan

18 2.9 789123456 May 14 2000 Miller Scott

19 3.4 987654321 Sep 12 1999 Wilson Marie

20 3.7 654321987 Jun 21 1998 Johnson Curtis

Solutions

Expert Solution

#include "sll_list.h"

list_t *newList() {
    list_t *m;
    m=(list_t *)malloc(sizeof(list_t));
    m->head = NULL;
    m->tail = NULL;
    m->size = 0;
    return m;
}

student_t *addStudent(FILE *inFile, int whichInput) {
    student_t *k = malloc(sizeof(student_t));
    fscanf(inFile, "%d %d %d %s %d %s %s %f", &(k->cuid),&(k->age),&(k->dob.day),
    (k->dob.month),&(k->dob.year),(k->firstName),(k->lastName),&(k->gpa));
    k->next = NULL;
    return k;
}

int printMenu() {
    int choice;

    printf("1. initialize list of students\n2. add additional student to front of list\n3. add additional student to rear of list\n4. delete student\n5. sort students alphabetically\n6. sort students by idNum\n7. show number of students in list\n8. print students\n9. quit program\n");
    scanf("%d",&choice);

    while( 0>choice && choice>10){

    printf("Enter valid choice 1-9");

    printf("1. initialize list of students\n2. add additional student to front of list\n3. add additional student to rear of list\n4. delete student\n5. sort students alphabetically\n6. sort students by idNum\n7. show number of students in list\n8. print students\n9. quit program\n");

    scanf("%d",&choice);

    }

    return choice;
}

void intializeList(list_t *list, FILE *inFile) {
    int size = 0;
    int whichInput= 0;

    fscanf(inFile, "%d", &size);

    for(int i=size; size > 0; size--) {
        addToFront(list, i, inFile);
    }
}

void addToFront(list_t *list, int whichInput, FILE *inFile) {
    student_t *k;
    k=addStudent(inFile, whichInput);

    if(list->size == 0) {
        list->tail = k;
    }

    k->next = list->head;
    list->head = k;
    list->size += 1;
}

void addToRear( list_t *list, int whichInput, FILE *inFile ) {
    student_t *k;
    k=addStudent(inFile, whichInput);

    if(list->size == 0) {
        list->head = k;
        list->tail = k;
    } else {
        list->tail->next=k;
        list->tail=k;
    }
    list->size += 1;
}

void deleteStudent(list_t *l,int cuid) {
    student_t *k,*p;

    if(l->head!=NULL) {
        k=l->head;
        if(k->cuid==cuid) {
            l->head = l->head->next;
            l->size -= 1;
            free(k);
            return;
        }

        p = k;
        k = k->next;
        while(k!=NULL) {
            if(k->cuid==cuid) {
                p->next=k->next;
                l->size -= 1;
                if(k == l->tail) {
                    l->tail = p;
                }
                free(k);
                break;
            } else {
                p=k;
                k=k->next;
            }
        }
    }
}

void sortListByLN( list_t *list ) {
    student_t *t,*p;
    int i=0;

    if(list->head != NULL) {

        int swaps = 0;
        do {
            swaps = 0;
            p = list->head;
            t = p->next;

            while(t != NULL) {
                if(strcmp(p->lastName, t->lastName) > 0) {
                    swap(t,p);
                    swaps++;
                }

                p = t;
                t = t->next;
            }
        } while(swaps != 0);
    }
}

void swap(student_t *s,student_t *p) {
    student_t temp;
    temp.age=s->age;
    temp.cuid=s->cuid;
    temp.gpa=s->gpa;
    temp.dob.day=s->dob.day;
    strcpy(temp.dob.month, s->dob.month);
    temp.dob.year=s->dob.year;
    strcpy(temp.firstName,s->firstName);
    strcpy(temp.lastName,s->lastName);

    s->age=p->age;
    s->cuid=p->cuid;
    s->gpa=p->gpa;
    s->dob.day=p->dob.day;
    strcpy(s->dob.month, p->dob.month);
    s->dob.year=p->dob.year;
    strcpy(s->firstName,p->firstName);
    strcpy(s->lastName,p->lastName);

    p->age=temp.age;
    p->cuid=temp.cuid;
    p->gpa=temp.gpa;
    p->dob.day=temp.dob.day;
    strcpy(p->dob.month, temp.dob.month);
    p->dob.year=temp.dob.year;
    strcpy(p->firstName,temp.firstName);
    strcpy(p->lastName,temp.lastName);
}

void sortListByNum( list_t *list) {
    student_t *t,*p;
    int i=0;

    if(list->head != NULL) {

        int swaps = 0;
        do {
            swaps = 0;
            p = list->head;
            t = p->next;

            while(t != NULL) {
                if(p->cuid > t->cuid) {
                    swap(t,p);
                    swaps++;
                }

                p = t;
                t = t->next;
            }
        } while(swaps != 0);
    }
}

void count_students(list_t *l) {
    student_t temp;
    int count=0;
    if(l->head==NULL) {
        printf("Student list is empty");
    } else {
        student_t *temp=l->head;
        count=0;

        while(temp!=NULL) {
            count++;
            temp=temp->next;
        }
        printf("student list contains:%d students",count);
    }
}

int size( list_t *list ) {
    return list->size;
}

int isEmpty( list_t *list ) {
    return list->size == 0;
}

void print_students(list_t *l) {
    student_t *temp;

    if(l->head==NULL) {
        printf("Student list is empty");
    } else {
        temp=l->head;
        printf("id\t last_name\t firstname\t date\t month\t year\t gpa\n");
        while(temp!=NULL) {
            printf("%d \t%s \t%s \t%d \t%s \t%d \t%f \n",temp->cuid,temp->lastName,temp->firstName,temp->dob.day,temp->dob.month,temp->dob.year,temp->gpa);
            temp=temp->next;
        }
    }
}

Related Solutions

Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
How would I complete this program using three files, which are a "SoccerPlayer.h" file, a "SoccerPlayer.cpp"...
How would I complete this program using three files, which are a "SoccerPlayer.h" file, a "SoccerPlayer.cpp" file, and a "main.cpp" file. C++ Please! This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT