Instruction: Do not forget to use - malloc for a new node - free to delete it. W
ID: 657488 • Letter: I
Question
Instruction: Do not forget to use - malloc for a new node
- free to delete it.
Write a menu based program to maintain student records. Your program should take the following inputs: 1. Student first name (max. 20 characters)
2. Student last name, (max. 20 characters) 3. Student scores (float/double), eg. 85.4 4. Zip code (may be a string)
You have to create a structure Student with above information. Then create a linked list where each list node contains a Student.
Your program should be able to take records of a minimum of 5 students. After taking the records, you should provide 7 functionalities to the user.
1. Print records
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<algorithm.h>
#include<string.h>
using namespace std;
struct node
{
double score;
char last[20];
char first[20];
struct node *next;
};
void push(struct node** head_ref, char first[],char last[],double score)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
strcpy(new_node->first,first);
strcpy(new_node->last,last);
new_node->score = score;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
/* Function to print nodes in a given linked list */
void print_List(struct node *node)
{
while (node != NULL)
{
printf("First Name: %10s, Last Name: %10s, Score: %0.2lf ", node->first,node->last,node->score);
node = node->next;
}
}
void insert(struct node **head_ref,char first[],char last[],double score)
{
push(head_ref,first,last,score);
}
void del(struct node **head_ref,char last[])
{
struct node *head = *head_ref;
struct node *next = NULL;
struct node *prev = NULL;
while(head)
{
next = head->next;
if(strcmp(head->last,last) == 0)
{
if(next)
{
strcpy(head->first,next->first);
strcpy(head->last,next->last);
head->score = next->score;
head->next = next->next;
free(next);
}
else
{
prev->next = NULL;
free(head);
}
}
prev = head;
head = head->next;
}
}
void search(struct node **head_ref,char zip[])
{
struct node *head = *head_ref;
while(head)
{
if(head==zip)
{
printf("First Name: %10s, Last Name: %10s, Score: %0.2lf ", head->first,head->last,head->score);
}
head = head->next;
}
}
int count(struct node *node)
{
int cnt = 0;
while (node != NULL)
{
cnt++;
node = node->next;
}
return cnt;
}
void sort_score(struct node *list)
{
int cnt = count(list);
struct node* temphead = list;
double tempscore;
char first[20],last[20];
for (int i=0; i<cnt; i++)
{
while (temphead->next)
{
if (temphead->score > temphead->next->score)
{
tempscore = temphead->score;
temphead->score = temphead->next->score;
temphead->next->score = tempscore;
strcpy(first,temphead->first);
strcpy(temphead->first,temphead->next->first);
strcpy(temphead->next->first,first);
strcpy(last,temphead->last);
strcpy(temphead->last,temphead->next->last);
strcpy(temphead->next->last,last);
}
else
temphead = temphead->next;
}
temphead = list;
}
}
void score_median(struct node *head)
{
int cnt = count(head);
sort_score(head);
struct node *temp = head;
int a = 0;
double ans = 0;
if(cnt % 2 ==0)
{
while(a+1 != cnt/2)
{
temp = temp->next;
a++;
}
ans = temp->score;
ans = ans + temp->next->score;
ans = ans/2.0;
}
else
{
while(a != cnt/2)
{
temp = temp->next;
a++;
}
ans = temp->score;
}
int above=0;
temp = head;
while(temp)
{
if(temp->score > ans)
above++;
temp = temp->next;
}
cout<<"Median score of all "<<cnt<<" students is "<<ans<<endl;
cout<<above<<" Students have score more than median score"<<endl;
}
int main()
{
struct node *list = NULL;
int choice,cnt;
push(&list,"ankit","agarwal",8);
push(&list,"jas","buttons",7);
push(&list,"sam","wesley",6);
push(&list,"gabo","pit",9);
push(&list,"sonia","vinci",10);
print_List(list);
char first[20],last[20];
double score;
do
{
cout<<"1. Print records"<<endl;
cout<<"2. Add a new record"<<endl;
cout<<"3. Delete record(s)"<<endl;
cout<<"4. Search by Zip code"<<endl;
cout<<"5. Sort by score range"<<endl;
cout<<"7. Find the median score"<<endl;
cout<<"0. Exit the program"<<endl;
cout<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: print_List(list);
break;
case 2: cout<<"Enter first and last name and score of student"<<endl;
cin>>first>>last>>score;
insert(&list,first,last,score);
break;
case 3: cout<<"Enter last name of student to delete record"<<endl;
cin>>last;
del(&list,last);
break;
case 4: cout<<"Enter Zip code of student to search records"<<endl;
cin>>zip;
search(&list,zip);
break;
case 5: sort_score(list);
print_List(list);
break;
case 7: score_median(list);
break;
case 0: cout<<"Exiting the program"<<endl;
break;
default: choice = 0;
cout<<"Choice not in Menu ,Exiting"<<endl;
break;
}
}
while(choice != 0);
system("pause");
return 0;
}