Exercise 1: Use a linked list to solve this exercise. Write a native C++ program
ID: 3875821 • Letter: E
Question
Exercise 1: Use a linked list to solve this exercise.
Write a native C++ program that allows an unlimited number of student’s data (student’s ID, names, score, subject (at least one)) to be entered and stored within the linked list.
The program should be able to perform the following operations:
- Read and store new student’s data into the list;
- Delete student from the list;
- Sort the student by score
- And print the list of student. The output should be as follows: Student’s ID, followed by his names, followed the subject, followed by the test score, followed by the test grade. Assume the following grade scale: [90 – 100] = A; [80 – 90[ = B; [70 – 80[ = C; [60 – 70[ = D; [0 – 60[= F; (More than 100 and less than 0) = N.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int studentid;
char name[30];
char subject[30];
int score;
struct node* next;
char grade;
};
void display(struct node * disp)
{
int count=1;
if(disp==NULL)
cout<<" The list is empty .";
else
{
while(disp!=NULL)
{
cout<<" Student "<<count<<" info .";
cout<<" Id : "<<disp->studentid;
cout<<" Name : "<<disp->name;
cout<<" Subject : "<<disp->subject;
cout<<" Score : "<<disp->score;
cout<<" Grade : "<<disp->grade;
disp=disp->next;
count++;
}
}
}
void swap(struct node *a, struct node *b)
{
int id = a->studentid;
char n[30];
char s[30];
strcpy(n,a->name);
strcpy(s,a->subject);
int sc = a->score;
a->studentid = b->studentid;
strcpy(a->name,b->name);
strcpy(a->subject,b->subject);
a->score = b->score;
b->studentid = id;
strcpy(b->name,n);
strcpy(b->subject,s);
b->score=sc;
}
void main()
{
clrscr();
struct node * head=NULL;
int i,data,key,cont,count=0;
int studentid;
char name[30];
char subject[30];
int score;
char grade;
while(cont){
cout<<" 1. Create a new students data 2. Delete a student from the list ";
cout<<" 3. Sort 4. Print List 5. Quit ";
cout<<" Enter your choice : ";
cin>>i;
if(i==1)
{
if(head==NULL)
{
struct node * temp =(struct node *) malloc(sizeof(struct node));
cout<<" Enter student id : ";
cin>>studentid;
cout<<" Enter students name : ";
gets(name);
cout<<" Enter the subject : ";
gets(subject);
cout<<" Enter the score : ";
cin>>score;
head=temp;
temp->studentid=studentid;
temp->score=score;
strcpy(temp->name,name);
strcpy(temp->subject,subject);
temp->next=NULL;
if(score>90 && score<=100)
grade='A';
else if(score>80 && score<=90)
grade='B';
else if(score>70 && score<=80)
grade='C';
else if(score>60 && score<=70)
grade='D';
else if(score>0 && score<=60)
grade='E';
else
grade='N';
temp->grade=grade;
}
else
{
struct node * temp1 = head;
struct node * temp =(struct node *) malloc(sizeof(struct node));
cout<<" Enter student id : ";
cin>>studentid;
cout<<" Enter students name : ";
gets(name);
cout<<" Enter the subject : ";
gets(subject);
cout<<" Enter the score : ";
cin>>score;
temp->studentid=studentid;
temp->score=score;
strcpy(temp->name,name);
strcpy(temp->subject,subject);
while(temp1->next!=NULL)
temp1=temp1->next;
temp1->next=temp;
if(score>90 && score<=100)
grade='A';
else if(score>80 && score<=90)
grade='B';
else if(score>70 && score<=80)
grade='C';
else if(score>60 && score<=70)
grade='D';
else if(score>0 && score<=60)
grade='E';
else
grade='N';
temp->grade=grade;
}
display(head);
}
else if(i==3)
{
int swapped, i;
struct node *ptr1;
struct node *lptr = NULL;
/* Checking for empty list */
if (head == NULL)
cout<<" List is empty ";
else
{
do
{
swapped = 0;
ptr1 = head;
while (ptr1->next != lptr)
{
if (ptr1->score > ptr1->next->score)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
}
else if(i==2)
{
struct node * temp=head;
if(temp==NULL)
cout<<" List is empty .";
else
{
head=head->next;
cout<<" Deleted studentid : "<<temp->studentid;
delete(temp);
}
}
else if(i==4)
{
cout<<" Student list is as follows .";
display(head);
}
else if(i==5)
cont=0;
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.