In: Computer Science
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
#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; } } }