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

add_s t udent (5 points) search_student (5 points) add_absent (10 points) last_a

ID: 3863075 • Letter: A

Question

add_student (5 points)

search_student (5 points)

add_absent (10 points)

last_absent (15 points)

remove_one (15 points)

Assume that the following information has been entered:

Name

Standard

Absents

Tom

2nd

01/14/2017, 01/15/2017

Jerry

2nd

02/18/2017

Oswald

3rd

None

Doraemon

3rd

02/01/2017, 02/16/2017, 02/28/2017

Belle

4th

None

Sinchan

1st

02/22/2017

list_by_name

list_by_standard

Name

Standard

Absents

Tom

2nd

01/14/2017, 01/15/2017

Jerry

2nd

02/18/2017

Oswald

3rd

None

Doraemon

3rd

02/01/2017, 02/16/2017, 02/28/2017

Belle

4th

None

Sinchan

1st

02/22/2017

Student Information Please enter your selection a: add a new student to the list s search for a student on the list r: remove a student from the list c add an absence date for a student 1: display last absence date for a student n display list of students by name b: display list of students of a given standard q: quit Please enter the student's info in the following format name standard Tom :2nd Student added to list successfully

Explanation / Answer

Please comment if anything else is also needed.

void add_student(char* name, char* standard)
{
struct student* s = NULL;
struct container* ptr = NULL , *temp = NULL;

temp = (struct container*)malloc(sizeof(struct container));

s = (struct student*) malloc(sizeof(struct student));

snprintf(s->name, sizeof(s->name),"%s", name);
snprintf(s->standard, sizeof(s->standard),"%s", standard);
s->absents = NULL;

temp->student = s;
temp->next = NULL;

if(list == NULL){
list = temp;
}
else{

ptr = list;
while(ptr->next != NULL){
ptr = ptr->next;
}

ptr->next = temp;
}
}

struct student* search_student(char* name)
{
struct container* ptr = list;

if(list == NULL){
return NULL;
}

while(ptr != NULL)
{
if( (!strncmp( ptr->student->name, name, strlen(name))) && (strlen(ptr->student->name) == strlen(name)) ){

break;
}
ptr = ptr->next;
}

if(ptr == NULL)
return NULL;
else
return ptr->student;

}

void add_absent(char* name, char* date)
{
struct absent* temp = NULL, *itr = NULL;
struct student* st= search_student(name);
struct container* ct = list;

itr = st->absents;

temp = (struct absent*)malloc(sizeof(struct absent));

snprintf(temp->date, sizeof(temp->date),"%s", date);
temp->next = NULL;

if(itr == NULL){
st->absents = temp;
}
else{
while(itr->next != NULL){
itr = itr->next;
}
itr->next = temp;
}

while(ct != NULL){
itr = ct->student->absents;
while(itr != NULL){

itr = itr->next;
}
ct = ct->next;
}

}

char* last_absent(char* name)
{
struct absent* itr = NULL;

struct student* st = search_student(name);

itr = st->absents;

if(itr == NULL){
return NULL;
}
else{
while(itr->next != NULL){
itr= itr->next;
}
return itr->date;
}
}

void remove_one(char* name)
{
struct container* ptr = NULL, *itr = NULL;
struct absent* temp1 = NULL , *temp2 = NULL;

struct student* st = search_student(name);

ptr = list;
while(ptr->student != st){
itr = ptr;
ptr = ptr->next;
}
if(ptr == list){
list = list->next;
ptr->next = NULL;
}
else{
itr->next = ptr->next;
ptr->next = NULL;
}
temp1 = ptr->student->absents;

while(temp1 != NULL){

temp2= temp1;
temp1 = temp1->next;
free(temp2);
}

free(ptr->student);
free(ptr);

ptr= list;
while(ptr!= NULL){
ptr= ptr->next;
}

}

struct container* list_of_standard(char* standard)
{
struct container* newList = NULL, *itr = list , *temp = NULL, *curr = NULL;
struct absent* abs = NULL ,*currAbs = NULL, *tempAbs = NULL, *itrAbs = NULL;

if(list == NULL)
return NULL;

while(itr != NULL)
{
if( !strncmp(itr->student->standard, standard, strlen(standard)))
{
temp = (struct container*)malloc(sizeof(struct container));

temp->student = (struct student*)malloc(sizeof(struct student));

temp->next = NULL;
snprintf(temp->student->name , sizeof(temp->student->name) , "%s", itr->student->name);
snprintf(temp->student->standard , sizeof(temp->student->standard) , "%s", standard);

temp->student->absents = NULL;
itrAbs = itr->student->absents;

while(itrAbs != NULL){

tempAbs = (struct absent*)malloc(sizeof(struct absent));
snprintf(tempAbs->date, sizeof(tempAbs->date), "%s", itrAbs->date);
tempAbs->next = NULL;

if(temp->student->absents == NULL){
temp->student->absents = currAbs = tempAbs;
}
else{
currAbs->next = tempAbs;
currAbs = currAbs->next;
}

itrAbs = itrAbs->next;
}

if(newList == NULL){
newList = curr = temp;
}
else{
curr->next = temp;
curr = temp;
}
}
itr = itr->next;
}

return newList;
}

struct container* list_by_name()
{
struct container* newList = NULL, *itr = list , *temp = NULL, *loc = NULL, *back = NULL;
struct absent* abs = NULL ,*currAbs = NULL, *tempAbs = NULL, *itrAbs = NULL;

if(list == NULL)
return NULL;

while(itr != NULL)
{
temp = (struct container*)malloc(sizeof(struct container));
temp->student = (struct student*)malloc(sizeof(struct student));

temp->next = NULL;
snprintf(temp->student->name , sizeof(temp->student->name) , "%s", itr->student->name);
snprintf(temp->student->standard , sizeof(temp->student->standard) , "%s", itr->student->standard);

temp->student->absents = NULL;
itrAbs = itr->student->absents;

while(itrAbs != NULL){

tempAbs = (struct absent*)malloc(sizeof(struct absent));
snprintf(tempAbs->date, sizeof(tempAbs->date), "%s", itrAbs->date);
tempAbs->next = NULL;

if(temp->student->absents == NULL){
temp->student->absents = currAbs = tempAbs;
}
else{
currAbs->next = tempAbs;
currAbs = currAbs->next;
}

itrAbs = itrAbs->next;
}

if(newList == NULL){
newList = temp;
}
else{

loc = newList;
while(loc != NULL){

if( strcmp(temp->student->name, loc->student->name) > 0){

back = loc;
loc = loc->next;
}
else{
break;
}
}

if(loc == newList){
temp->next = newList;
newList = temp;
}
else if( loc == NULL){
back->next = temp;
}
else{
back->next = temp;
temp->next = loc;
}
}
itr = itr->next;
}

return newList;
}