Question

Using the program segment and sample txt file below, write a program that contains a linked...

Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to:

1. initialize list of students

2. add additional student to front of list

3. add additional student to rear of list

4. delete student

5. sort students alphabetically

6. sort students by idNum

7. show number of students in list

8. print students

9. quit program

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The program should be divided into the following segments

1. mainDriver.c. (contains main()function)
2. sll_list.h (given below. header file for this singly linked-list program)
3. sll_list.c (implementation file for singly linked-list program)
4. students.txt (given below, contains students for initializing linked-list)

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Function prototypes:

list_t *newList( ); // creates a new list; returns a pointer to the new list

student_t *addStudent( FILE *inFile, int whichInput );
// called by addToFront() and addToRear() functions
// creates and initializes new student node from input file pointer sent in (either file pointer specified on command-line or stdin that has been reset)
// whichInput is intended to be a flag will which determine if the user prompts are to be printed or not
// returns a pointer to the student that was just created

int printMenu( );
// prints the menu to the user; returns the menu choice

void initializeList( list_t *list, FILE *inFile );
// calls addToFront() sending input file pointer (file specified at the command-line)
// note – first value in input file is number of students in file – the first value read in from file so you know how many times the loop needs to go

void addToFront( list_t *list, int whichInput, FILE *inFile );
// creates new student by calling addStudent() then adds returned student to front of list

void addToRear( list_t *list, int whichInput, FILE *inFile );
// creates new student by calling addStudent() then adds returned student to end of list

void deleteStudent( list_t *list, int idNum );
// deletes the student with the cuid sent in as idNum

void sortListByLN( list_t *list );
// sorts the list alpahabetically by last name

void sortListByNum( list_t *list );
// sorts the list by cuid

int size( list_t *list );
// returns the size of the list

int isEmpty( list_t *list );
// returns 1 if the list is empty and 0 if it is not empty

void printStudents ( list_t *list );
// prints the students in the list


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// ssl_list.h //

#ifndef SLL_LIST_H
#define SLL_LIST_H


typedef struct {
   char month[4];
   int day, year;
} dateOfBirth;

typedef struct student {
int age;
float gpa;
int Num;
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 Num );
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 */

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx

students.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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

answer:

Solution(s):

The below code represents the implementation of all the functions that are applied on the students list...

#include<stdio.h>
typedef struct {
char month[4];
int day, year;
} dateOfBirth;

typedef struct student {
int age;
float gpa;
int idNum;
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;
void intialize_students(list_t t)
{
int i;
student_t *k,*p;
for(i=0;i<t.size;i++)
{
k=(student_t *)malloc(sizeof(student_t));
scanf("%d %d %d %s %d %s %s %f",&(k->idNum),&(k->age),&(k->dob->day),&(k->dob->month),&(k->dob->year),&(k->firstName),&(k->lastName),&(k->gpa));
if(t.head==NULL)
t.head=k;
else
{
p=t.head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=k;
k->next=t.tail;
}
}
}
void add_additional_student_front(list_t l)
{
student_t *k;
k=(student_t *)malloc(sizeof(student_t));
scanf("%d %d %d %s %d %s %s %f",&(k->idNum),&(k->age),&(k->dob->day),&(k->dob->month),&(k->dob->year),&(k->firstName),&(k->lastName),&(k->gpa));
k->next=l.head;
l.head=k;
}
void add_additional_student_tail(list_t l)
{
student_t *k;
k=(student_t *)malloc(sizeof(student_t));
scanf("%d %d %d %s %d %s %s %f",&(k->idNum),&(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 delete_sutdent(list_t l,int id)
{
student_t *k,*p;
if(l.head!=NULL)
{
k=l->head;
while(k!=NULL)
{
if(k->idNum==id)
{
p->next=k->next;
free(k);
break;
}
else
{
p=k;
k=k->next;
}
}
}
}
void sort_aplhabet(list_t l)
{
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.idNum=s->idNum;
temp.gpa=s->gpa;
temp.dob.day=s->dob->day;
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->dNum=p->idNum;
s->gpa=p->gpa;
s->dob->day=p->dob->day;
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->idNum=temp.idNum;
p->gpa=temp.gpa;
p->dob->day=temp.dob.day;
p->dob->month=temp.dob.month;
p->dob->year=temp.dob.year;
strcpy(p->firstName,temp.firstname);
strcpy(p->lastName,temp.lastname);
}
void sort_idnum(list_t l)
{
student_t *t,*p;
int i=0;
if(l.head==NULL)
{
}
else{
t = l.head;
p=NULL;
while(t->next != p)
{
if(t->idNum>t->next->idNum)
{
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)
{
printft("%d\t%s\t%s\t%d\t%s\t%s\t%f\n",temp->idNum,temp->lastName,temp->firstName,temp->dob.day,temp->dob.month,temp->dob.year,temp->gpa);
temp=temp->next;
}
}
}
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;
intialize_students(l);
break;
case 2://add additional student to front of the list:
add_additional_student_front(l);
break;
case 3://add additional student to tail of the list:
add_additional_student_tail(l);
break;
case 4://delete student
scanf("%d",id);
delete_sutdent(l,id);
break;
case 5://sort students aplhabetically
sort_aplhabet(l);
break;
case 6://sort students by id
sort_idnum(l);
break;
case 7: //number of students in the list
count_students(l);
break;
case 8: //printing students in the list
print_students(l);
break;
case 9:
printf("quiting from the program");
break;
}
}while(choice!=9);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using the program segment and sample txt file below, write a program that contains a linked...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • In c programming . Part A: Writing into a Sequential File Write a C program called...

    In c programming . Part A: Writing into a Sequential File Write a C program called "Lab5A.c" to prompt the user and store 5 student records into a file called "stdInfo.txt". This "stdInfo.txt" file will also be used in the second part of this laboratory exercise The format of the file would look like this sample (excluding the first line) ID FIRSTNAME LASTNAME GPA YEAR 10 jack drell 64.5 2018 20 mina alam 92.3 2016 40 abed alie 54.0 2017...

  • C program of a dictionary using linked list

    six options in the menu:Create a new dictionary.2. Add a word to a dictionary. 3. Delete a word from a dictionary. 4. Find a word in a dictionary. 5. Delete a dictionary. 6. Exit.each word represented by typedef struct Word { char** translations; struct Word* next; } Word;each dictionary represented bytypedef struct { char** languages; int numOfLanguages; Word* wordList; } Dictionary;languages will include a pointer to an array of strings so that the first word is the origin language and...

  • Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include...

    Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> //#define constant values #define MAX_URL_LENGTH 50 #define TRUE 1 #define FALSE 0 //typedef for the Element struct which constains a c string to store a URL in the BrowserList typedef struct { char szURL[MAX_URL_LENGTH]; } Element; //Typedef for a node in the doubly linked list (has next and previous pointers). typedef struct NodeDL { Element element; struct NodeDL *pNext;...

  • Write a C++ program that includes the following: Define the class Student in the header file Stu...

    Write a C++ program that includes the following: Define the class Student in the header file Student.h. #ifndef STUDENT_H #define STUDENT_H #include <string> #include <iostream> using namespace std; class Student { public: // Default constructor Student() { } // Creates a student with the specified id and name. Student(int id, const string& name) { } // Returns the student name. string get_name() const { } // Returns the student id. int get_id () const { } // Sets the student...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

  • Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

    Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation. Once you have fully implemented the table, create a main.c file that implements a testing framework for your table. Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list. table.h #ifndef _TABLE_H #define _TABLE_H //----------------------------------------------------------------------------- // CONSTANTS AND...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • in this coding please add an address for the student to enter... note that this coding use linked list and a file text t...

    in this coding please add an address for the student to enter... note that this coding use linked list and a file text to store its data ...Please make sure the address will work with all the functions below..to check whether your coding is correct after you finished adding a new dummy student data and its information close the program and reopen it and choose the option 5 ... I've tried adding an address before but an error occurred when...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT