Saturday, 29 August 2015

insert|display|sort|delete|linklist

// own project.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class student
{
int regno;
float marks;
string grade;
student *next;
student *head,*last;
public:
student()
{
head=last=NULL;
}
void add();
void display();
void sort();
void Delete(int);
};
void student::add()
{
system("CLS");
cout<<endl;
student *s=new student();
cout<<"enter Reg No "<<endl;
cin>>s->regno;
cout<<"enter marks "<<endl;
cin>>s->marks;
s->next=NULL;
if (head== NULL)  //Case 1
{
head = s;
last = s;
}
else               //Case 2
{
last->next = s;
last = s;
}
}


void student::display()
{
system("cls");
cout << "\n\n\n";
student * head = this->head;
student * temp;

temp = head; //For assigning marks

while (temp != NULL)
{
if (temp->marks >= 80)
temp->grade = 'A';
else if (temp->marks >= 75)
temp->grade = 'B';
else if (temp->marks >= 70)
temp->grade = 'B';
else if (temp->marks >= 65)
temp->grade = 'C';
else if (temp->marks >= 60)
temp->grade = 'C';
else
temp->grade = 'F';
temp = temp->next;
}

temp = head; //For printing marks

cout << "Roll" << setw(20) << "Marks" << setw(20) << "Grade" << endl;
cout << "__________________________________________________" << endl;

while (temp != NULL)
{
cout << temp->regno << setw(20)
<< temp->marks << setw(20)
<< temp->grade << endl;
temp = temp->next;
}

cout << endl << endl << endl;
system("pause");
}
void student::sort()
{
system("CLS");
cout<<"\n\n\nOrder of sorting\n \n1. Ascending order \n2.Descending Order\n";
int order;
cin>>order;
if (order==1)
{
for (student *new1=head; new1!=NULL ; new1=new1->next)
{
for (student *pass=new1->next; pass!=NULL; pass=pass->next)
{
if(new1->regno >pass->regno)
{
int temp=new1->regno;
new1->regno=pass->regno;
pass->regno=temp;
}

if(new1->marks >pass->marks)
{
float temp=new1->marks;
new1->marks=pass->marks;
pass->marks=temp;
}
if(new1->grade >pass->grade)
{
string temp=new1->grade;
new1->grade=pass->grade;
pass->grade=temp;
}
}
}
}

if (order==2)
{
for (student *new1=head; new1!=NULL ; new1=new1->next)
{
for (student *pass=new1->next; pass!=NULL; pass=pass->next)
{
if(new1->regno < pass->regno)
{
int temp=new1->regno;
new1->regno=pass->regno;
pass->regno=temp;
}

if(new1->marks < pass->marks)
{
float temp=new1->marks;
new1->marks=pass->marks;
pass->marks=temp;
}
if(new1->grade < pass->grade)
{
string temp=new1->grade;
new1->grade=pass->grade;
pass->grade=temp;
}
}
}
}
}
void student::Delete(int deleteitem)
{
student *current;
student *trailcurrent=NULL;
bool found;
if (head==NULL)
cout<<"Cannot delete item from empty item"<<endl;
else
{
current=head;
found =false;
}
while(current!=NULL && !found)
{
if(current->regno>=deleteitem)
found =true;
else
{
trailcurrent=current;
current=current->next;
}
}
if (current==NULL)
cout<<"Cannot delete file"<<endl;
else if(current->regno==deleteitem)
{
if(head==current)
{
head=head->next;
if (head==NULL)
last=NULL;
delete current;
}

else
{
trailcurrent->next=current->next;
if(current==last)

last=trailcurrent;
delete current;
}
}
else
cout<<"Item cannot delete "<<endl;

}
void Mainmenu();
void main()
{
student F;
for(;;)
{
Mainmenu();
int ch;
cin>>ch;
switch(ch)
{
case 1:
F.add();
break;
case 2:
F.display();
break;
case 3:
F.sort();
break;
case 4:
system("CLS");
cout<<"Enter regno you want to delete "<<endl;
int n;
cin>>n;
F.Delete(n);
break;
case 5:
exit(0);

break;
default:
cout<<"Wrong selection"<<endl;
}
}
}
void Mainmenu()
{
system("cls");
cout << "\n\n\nStudent Records v0.1\n" << endl;
cout << "1. Insert Record \t" << endl
<< "2. Display Record \t(All Records) " << endl
<< "3. Sort \t\t (Asc/Desc)"<< endl
<< "4. Delete" << endl
<< "5. Exit" << endl;
}

No comments:

Post a Comment