You are on page 1of 4

#include<iostream>

using namespace std;


class node{
public:
string name,reg;
float gpa;
node *next;
};
class list{
node *head;
node *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void insert_end(node x)
{
node * n=new node();
n->name=x.name;
n->reg=x.reg;
n->gpa=x.gpa;
n->next=NULL;
if(head==NULL)
{
head=n;
tail=n;
}
else
{
tail->next=n;
tail=n;
}
}
void insert_front(node x)
{
node * n=new node();
n->name=x.name;
n->reg=x.reg;
n->gpa=x.gpa;
n->next=NULL;

if(head==NULL)
{
head=n;
tail=n;
}
else
{
n->next=head;
head=n;
}
}
node* getNode(string n) {
node* temp = head, * pretemp = head;
while (temp != NULL) {
if (temp->name == n) {
return pretemp;
}
pretemp = temp;
temp = temp->next;
}
}
void inserBeforeName(node* s, node x) {
node* n = new node;
n->name=x.name;
n->reg=x.reg;
n->gpa=x.gpa;
n->next = s->next;
s->next = n;
}
void display()
{
float gp;
cout<<"Enter gpa :"<<endl;
cin>>gp;
node *temp = head;
while(temp != NULL)
{
if(temp->gpa>gp)
{

cout << temp->name << endl;


cout << temp->reg << endl;
cout << temp->gpa<< endl;
temp = temp->next;
}
}
}
void sort()
{
node* ptr = head, * preptr;

while (ptr->next != NULL) {


preptr = ptr->next;

while (preptr != NULL) {


if (ptr->gpa < preptr->gpa) {
swap(ptr->name, preptr->name);
swap(ptr->reg, preptr->reg);
swap(ptr->gpa, preptr->gpa);
}
preptr = preptr -> next;
}

ptr = ptr->next;
}
}
};
int main()
{
list obj;
node obj1;
char ch;
int choice;
int val;
do{
cout<<"Press 1 to insert at front:"<<endl;
cout<<"Press 2 to insert at end:"<<endl;
cout<<"Press 3 to insert at middle:"<<endl;
cout<<"Press 4 sort:"<<endl;
cout<<"Press 5 to display:"<<endl;
cin>>choice;
switch(choice)
{
case 1:
{

cout<<"Enter Name:"<<endl;
cin>>obj1.name;
cout<<"Enter regno:"<<endl;
cin>>obj1.reg;
cout<<"Enter gpa:"<<endl;
cin>>obj1.gpa;
obj.insert_front(obj1);
break;
}
case 2:
{
cout<<"Enter Name:"<<endl;
cin>>obj1.name;
cout<<"Enter regno:"<<endl;
cin>>obj1.reg;
cout<<"Enter gpa:"<<endl;
cin>>obj1.gpa;
obj.insert_end(obj1);
break;
}
case 3:
{
cout<<"Enter Name:"<<endl;
cin>>obj1.name;
cout<<"Enter regno:"<<endl;
cin>>obj1.reg;
cout<<"Enter gpa:"<<endl;
cin>>obj1.gpa;
string nam;
string n;
cout<<"Enter Name to insert before sudent:"<<endl;
cin>>n;
obj.inserBeforeName(obj.getNode(n),obj1);
break;
}
case 4:
{

obj.sort();
break;
}
case 5:
{
obj.display();
break;
}
default:
cout<<"wrong input:"<<endl;
break;
}
cout<<"Press y/Y to enter more entries:"<<endl;
cin>>ch;
}while(ch=='y'|| ch=='Y');
return 0;
}

You might also like