bài em đang làm như thế này muốn nó sắp xếp theo năm sinh và xóa hạnh kiểm yếu thì làm như thế nào ạ
#include<iostream>
#include<string>
using namespace std;
struct SinhVien
{
string maLop;
string maSinhVien;
string tenSinhVien;
int namSinh;
string queQuan;
float diemTK;
string hanhKiem;
};
typedef SinhVien TypeOfData;
TypeOfData inputTypeOfData()
{
TypeOfData SinhVien;
fflush(stdin);
cout<<" - Ho ten:" ;
getline(cin,SinhVien.tenSinhVien);
cout<<" - Nam sinh: ";
cin>>SinhVien.namSinh;
fflush(stdin);
cout<<" - Ma sinh vien: ";
getline(cin,SinhVien.maSinhVien);
fflush(stdin);
cout<<" - Ma lop: ";
getline(cin,SinhVien.maLop);
fflush(stdin);
cout<<" - Que Quan: ";
getline(cin,SinhVien.queQuan);
cout<<" - Diem tong ket: ";
cin>>SinhVien.diemTK;
cout<<" - Hanh Kiem: ";
cin>>SinhVien.hanhKiem;
return SinhVien;
}
struct TypeOfNode
{
TypeOfData data;
struct TypeOfNode *next;
};
typedef struct TypeOfNode *NodePtr;
//// In thong tin cua mot nut
void printNodeInfo(NodePtr pnode)
{
cout<<" - Ho Ten: "<< pnode->data.tenSinhVien << endl;
cout<<" - Nam sinh: "<< pnode->data.namSinh << endl;
cout<<" - Ma sinh vien: "<< pnode->data.maSinhVien << endl;
cout<<" - Ma lop: "<< pnode->data.maLop << endl;
cout<<" - Que Quan: "<< pnode->data.queQuan << endl;
cout<<" - Diem tong ket: "<< pnode->data.diemTK << endl;
cout<<" - Hanh Kiem: "<<pnode->data.hanhKiem<<endl;
}
//// khoi tao
void initialize(NodePtr &L)
{
L = NULL;
}
//// Kiem tra rong
int isEmpty(NodePtr L)
{
return ((L == NULL) ? true : false);
}
//// Tao mot nut
NodePtr createNode(TypeOfData data){
NodePtr newNode = new TypeOfNode;
newNode->next = NULL;
newNode->data = data;
return newNode;
}
//// Them mot phan tu vao dau danh sach
void insertHead(NodePtr &L, TypeOfData data){
NodePtr newNode = createNode(data);
if (L == NULL){
L = newNode;
}
else {
newNode->next = L;
L = newNode;
}
}
//// Them mot phan tu vao cuoi danh sach
void insertTail(NodePtr &L, TypeOfData data){
NodePtr newNode, p;
newNode = createNode(data);
if (L == NULL){
L = newNode;
}
else {
p=L;
while (p->next !=NULL){
p=p->next;
}
p->next = newNode;
}
}
//// Them mot phan tu vao sau mot nut danh sach---chu y
void insertAfter(TypeOfData data)
{
NodePtr newNode, p;
newNode = createNode(data);
if (p == NULL)
cout <<"Khong them duoc!" <<endl;
else
{
NodePtr q = createNode(data);
q->next = p->next;
p->next = q;
}
}
//// xoa nut p khoi danh sach L
void deleteList(NodePtr p, NodePtr &L)
{
if (p == NULL)
cout<<"Phan tu can xoa khong ton tai trong danh sach! "<<endl;
else
{
if (isEmpty(L))
cout<<"Danh sach rong!" <<endl;
else
{
if (p == L)
{
L = L->next;
}
else
{
NodePtr q;
q = L;
while (q != NULL && q->next !=p)
q = q->next;
q->next = p->next;
}
p->next = NULL;
delete p;
}
}
}
//// Xoa mot phan tu dau danh sach
void deleteHead(NodePtr &L){
if(L == NULL) return;
NodePtr p = L;
L = L->next;
p->next = NULL;
delete p;
}
/// Xoa 1 phan tu cuoi danh sach
void deleteTail(NodePtr &L) {
if (L==NULL) return;
NodePtr p=L;
NodePtr q=NULL;
while (p->next != NULL){
q = p;
p = p->next;
}
if (q!= NULL)
{
q->next = NULL;
delete p;
}
else {
L = NULL;
delete p;
}
}
///// tim kiem theo ma sinh vien
NodePtr searchByMaSinhVien(string maSinhVien, NodePtr L)
{
if (L == NULL) return NULL;
else
{
NodePtr q;
q = L;
while (q != NULL && q->data.maSinhVien.compare(maSinhVien) != 0)
q = q->next;
return q;
}
}
///// duyet danh sach
void traverse(NodePtr L)
{
if (L == NULL)
cout<<" Danh sach rong!\n";
else
{
NodePtr q;
q = L;
int i=0;
while (q != NULL)
{///In thong tin sinh vien
cout<<"Thong tin sinh vien:" << ++i << ":" <<endl;
printNodeInfo(q);
q = q->next;
}
}
}
///// sap xep danh sach tang dan theo nam sinh
void sortListByName(NodePtr &L)
{
if (L == NULL)
{
cout<<"Danh sach rong, khong sap xep!" << endl;
}
else {
for (NodePtr p = L; p->next != NULL; p = p->next)
for (NodePtr q = p->next; q != NULL; q = q->next)
{
if (p->data.tenSinhVien.compare(q->data.tenSinhVien) > 0)
{
TypeOfData data = p->data;
p->data = q->data;
q->data = data;
}
}
}
}
int main()
{
int n;
cout<<"Nhap so luong sinh vien: ";
cin>>n;
NodePtr L;
initialize(L);
for(int i=0;i<n;i++)
{
cout<<"Nhap thong tin sinh vien:" << i+1 << ":" << endl;
TypeOfData SinhVien = inputTypeOfData();
//inserHead(L, SinhVien);
insertTail(L, SinhVien);
}
cout<<"===> Danh sach sinh vien vua nhap:" << endl;
traverse(L);
cout<<"============================================" << endl;
cout<<" Xoa sinh vien o dau danh sach" <<endl;
deleteHead(L);
cout<<"===> Danh sach sinh vien sau khi xoa o dau danh sach:"<< endl;
traverse(L);
cout<<"============================================" << endl;
cout<<" Xoa sinh vien o cuoi danh sach" << endl;
deleteTail(L);
cout<<"===> Danh sach sinh vien sau khi xoa o cuoi danh sach:" << endl;
traverse(L);
string masv;
NodePtr found;
cout<<"==========================================="<< endl;
cout<<" XOA SINH VIEN"<<endl;
fflush(stdin);
cout<<"Nhap ma sinh vien de tim kiem: ";
getline(cin,masv);
found = searchByMaSinhVien(masv, L);
if (found == NULL) cout<<"Khong tim thay sinh vien voi ma: "<< masv;
else {
cout<<"Tim thay sinh vien voi ma: "<<masv << ":" << endl;
printNodeInfo(found);
cout<<"Xoa sinh vien khoi danh sach ... "<<endl;
deleteList(found,L);
cout<<"===> Danh sach sinh vien sau khi xoa: ";
traverse(L);
}
cout<<"=========================="<<endl;
cout<<" THEM SINH VIEN " << endl;
fflush(stdin);
cout<<"Nhap ma sinh vien de tim kiem: ";
getline(cin, masv);
found = searchByMaSinhVien(masv, L);
if (found == NULL) cout<<"Khong tim thay sinh vien voi ma: "<<masv;
else{
cout<<"Tim thay sinh vien voi ma:"<< masv <<":"<< endl;
printNodeInfo(found);
cout<<"Nhap thong tin sinh vien can them sau sinh vien: " << found->data.tenSinhVien << ": "<<endl;
TypeOfData SinhVien = inputTypeOfData();
cout<<"===> Danh sach sinh vien sau khi Them:" <<endl;
traverse(L);
}
cout<<"============================================"<<endl;
cout<<" SAP XEP DANH SACH" << endl;
sortListByName(L);
cout<<"===> Danh sach sinh vien sau khi sap xep:" <<endl;
traverse(L);
}