In: Computer Science
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.
#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;
}
}