Question

In: Computer Science

C++ language. Suppose you are given a list of students registered in a course and you...

C++ language.

Suppose you are given a list of students registered in a course and you are required to implement an Linked List of student. Each student in the list has name, regNo and cGpa. You task is to implement following function related to Linked List.

insertStudent() : This function, when called will prompt user to enter his/her choice of insertion. The options are

1. Insert student at the start of linked list

2. Insert student at the end of linked list

3. Insert student before the student who name is entered

e.g if insertStudent(Student s, “Abrar”) is called, it will search student in the linked list with a name Abrar and then insert Student s (1st argument) before Abrar.

sortBycGpa( ) : When called, this function will sort all the students in the list based on CGPA.

display( ) : This function when called displays all the data of only those students whos cGPA are greater than the entered cgpa.

e.g. if display(3.5) is called, it will display all the students whos’ CGPA is greater than 3.5.

Solutions

Expert Solution

#include<iostream>
#include<string>
using namespace std;

//create the structure student

struct student{
string name;
int regNo;
float cGpa;
struct student *next;

};

//declare the required functions
void insertStudent();
void sortBycGpa();
void display();
void insertStudent();
void insertStudent(struct student*, string );
void swapp(struct student*, struct student*);
struct student *head; //pointer for the first node

int main()
{
head = NULL; //head is pointing to null as the list is empty, at the start.

int ch;

while(1){

//display the choices

cout<<endl<<"1. Insert Student";
cout<<endl<<"2. sortBycGpa";
cout<<endl<<"3. Display";
cout<<endl<<"4. Exit";
cout<<endl<<"Enter your choice <1...4>: ";
cin>>ch;

switch(ch){
case 1: insertStudent(); //func to insert student
break;
case 2: sortBycGpa(); //func to sort by cgpa
break;
case 3: display(); //func to display student details
break;
case 4: return 0;
}
}

}

void insertStudent(){

int ins_ch;
struct student *st; // new node to be inserted

st = new student; //allocate memore for the new node

//read where the user want to insert at beg or end or before a name

cout<<endl<<"1. Insert in the Front of List.";
cout<<endl<<"2. Insert at the end of the list.";
cout<<endl<<"3. Insert before a Name.";
cout<<endl<<"Enter your choice <1...3>: ";
cin>>ins_ch;

//read the value for the new node

cout<<endl<<"Enter student name: ";
cin>>(*st).name;
cout<<endl<<"Enter the Reg. No: ";
cin>>st->regNo;
cout<<endl<<"Enter CGPA: ";
//cin.sync();
cin>>st->cGpa;

// cout<<endl<<st->name<<"\t"<<st->regNo<<"\t"<<st->cGpa;

switch (ins_ch){

// insert at the begining

    case 1: if( head == NULL ){ //do this if this is the first node
head = st; //head points to the new node
st->next = NULL; //new node's next points to null
// cout<<endl<<"This is th first node";
}
else{
st->next = head; //new node's next point to first node
head = st; //head points to new node
// cout<<endl<<"This is not the first node";
}
cout<<endl<<"Node Inserted....";
break;

//insert at the end

case 2: if( head == NULL ){ //do this if this is the first node
head = st; //head points to new node
st->next = NULL; //new node's next points to null
}
else{
struct student *tmp;
tmp = head; //tmp points to first node
while( tmp->next != NULL) //make tmp to point the last node
tmp = tmp->next;
tmp->next = st; //last nodes's next points to new node
st->next =NULL; //new node's next points to null
}
cout<<endl<<"Node Inserted....";
break;

case 3: string bname;
cout<<"\n Enter the name of the student before whome to be inserted: ";
cin>>bname;
insertStudent(st, bname); //insert node before a name
cout<<endl<<"Node Inserted....";
break;

}
}

void insertStudent(struct student *st, string bname){

struct student *tmp, *prev;

//prev and tmp points to first node
tmp = head;
prev = head;

if( tmp== NULL ){ //do this if list is empty
cout<<"\nThe list is empty...";
exit(0);
}

//make tmp to point to the node which contains the name and prev to point the //node before that
while( tmp->name != bname && tmp->next != NULL ){
prev = tmp;
tmp = tmp->next;
}

if( tmp->next== NULL && tmp->name!=bname){ //do this name not found
cout<<"\n Name not found...";
exit(0);
}

st->next = prev->next; //new node's next points to node that contains the name
if( prev == head ) //do this if list previously has only one node
prev = st;
else
prev->next = st; //prev node's next points to the new node

}

void sortBycGpa(){

struct student *temp;
struct student *lptr = NULL;
int swapped = 1;

temp = head;

if( temp == NULL ){
cout<<endl<<"List is empty....";
exit(0);
}

//sort the list

while( swapped == 1 ){
swapped = 0;
temp = head;
while( temp->next != lptr){

if( temp->cGpa > temp->next->cGpa)
{
swapp(temp, temp->next);
swapped = 1;
}

temp = temp->next;

}
lptr = temp;
}

cout<<endl<<"List sorted based on CGPA...";
}

//swap the contents of two nodes

void swapp( struct student *a, struct student *b){
string tname;
int t_rno;
float t_cgpa;

tname = a->name;
a->name = b->name;
b->name = tname;

t_rno = a->regNo;
a->regNo = b->regNo;
b->regNo = t_rno;

t_cgpa = a->cGpa;
a->cGpa = b->cGpa;
b->cGpa = t_cgpa;

}

void display(){

struct student *temp;
temp = head;

if( temp == NULL ){
cout<<endl<<"List is Empty....";
exit(0);
}

//display

while( temp!=NULL){
cout<<endl<<temp->name<<"\t"<<temp->regNo<<"\t"<<temp->cGpa;
temp = temp->next;

}

}


Related Solutions

Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
In 2017, 965 students registered for a course. Explain how you will use the random number...
In 2017, 965 students registered for a course. Explain how you will use the random number table to select a simple random sample of 20 students. Start from digit one of row 6. Fill in the blanks 1. Bar chart is normally used for ___________ data. 2. Pie chart presents ___________ data. 3. A ____________________ is used to describe the relationship between two categorical variables. 4. A ___________ histogram is one with a single peak. 5. A ___________ histogram is...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
C# language Question: You need to write a program for students who receive bursaries in the...
C# language Question: You need to write a program for students who receive bursaries in the department, managing the free hours they have to do. For each recipient you store the recipient’s name and the number of hours outstanding. All recipients start with 90 hours. Implement class Recipients which has the private attributes Name and Hours. In addition to the constructor, the class has the following methods: public String getName() // Returns the name of the recipient public int getHours()...
The rate of vocabulary memorization of the average student in a foreign language course is given...
The rate of vocabulary memorization of the average student in a foreign language course is given by dv dt = 20 t + 1 where t is the number of continuous hours of study, 0 < t ≤ 4, and v is the number of words. How many words would the student memorize in 3 hours? (Round your answer to the nearest whole number.) words
Fifteen students were registered in Section 1 and 15 students were registered in Section 2 of...
Fifteen students were registered in Section 1 and 15 students were registered in Section 2 of a research course. They took the same midterm exam, and their exam scores were distributed as follows: Section 1:          89, 56, 45, 78, 98, 45, 55, 77, 88, 99, 98, 97, 54, 34, 94 Section 2:           77, 88, 87, 67, 98, 87, 55, 77, 45, 44, 88, 99, 69, 67, 98 Calculate the mode, median, mean, range, variance, and standard deviation for both...
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
*** C language *** Make a  list of all of the different zip codes that the zombies...
*** C language *** Make a  list of all of the different zip codes that the zombies live at and a count of how many zombies live at that zip code. The list should be sorted based on the zip code with the smallest zip code printed first. Example: Zip Code # Zombies 31234 2 //variables int zip[] = {37643,31234,32785,32643,32785,32643,31234,31234,32643,32643,31234,32785,32785,32643,31234}; char zombie[]={'N','Y','Y','N','Y','Y','Y','N','Y','Y','N','Y','Y','Y','Y'}; Y = zombie N = Not zombie int zPatient = 0; int nonZpatient = 0; for(int pt = 0;...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT