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 Python. A file exists on the disk named students.txt. The file contains several records, and...
Using Python. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s score for the final exam. Write code that deletes the record containing “John Perz”as the student name. This the code i have so far but it's not working: import os def main(): found=False search='John Perz' student_file = open('student.txt','r') temp_file = open('temp_students.txt','w') name=student_file.readline() score='' while name !='': score=student_file.readline() name=name.rstrip('/n') score=score.rstrip('/n') if name...
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file....
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an array of objects and practice passing objects to and return objects from functions. String functions practice has also been included. Extend the BankAccount class. The member fields of the class are:...
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...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
Create this C++ program using classes 1. Create a file text file with a string on...
Create this C++ program using classes 1. Create a file text file with a string on it 2. Check the frecuency of every letter, number and symbol (including caps) 3. Use heapsort to sort the frecuencys found 4. Use huffman code on the letters, symbols or numbers that have frecuencys I created the file, and the frecuency part but i'm having trouble with the huffman and heapsort implementation.
Working with Files in C++. Create the following  program called payroll.cpp.  Note that the file you...
Working with Files in C++. Create the following  program called payroll.cpp.  Note that the file you read must be created before you run this program.  The output file will be created automatically by the program.  You can save the input file in the same directory as your payroll.cpp file by using Add New Item, Text File.   // File:  Payroll.cpp // Purpose:  Read data from a file and write out a payroll // Programmer:  (your name and section) #include <cstdlib>...
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...
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...
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...
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”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT