Hiện tại bên KHMT có cái Môn "Cấu trức dữ lại" rất đau đầu Mình học mãi mà vẫn ko hiểu lắm Nếu ai có những bài hay mà dễ hiểu thì xin post lên mấy bài tham khảo Nhưng mình chưa học C++ nên mấy cái bài sử dụng C++ thì mình chưa rành lắm nến nếu sử dụng C thì càng tốt Thanks...!
Mã: #include <iostream> #define maxl 50 using namespace std; typedef struct sinhvien { char masv[10]; char hoten[30]; float diem; };sinhvien a[maxl]; int i,j,n,k; int dk=0; //Nhap danh sach sinh vien void nhapsv(sinhvien &x) { cout<<"Thong tin sv["<<i+1<<"]"<<endl; cout<<"Masv : ";cin>>x.masv; cout<<"Hoten : ";cin>>x.hoten; cout<<"Diem : ";cin>>x.diem; } //In danh sach sinh vien void insv(sinhvien &x) { cout<<x.masv<<" "<<x.hoten<<" "<<x.diem<<endl; } //Chen sinh vien vao danh sach void chensv(int k,sinhvien a[],int &n) { if(n==maxl) cout<<"Danh sach day !"<<endl; else if(k<0||k>n) cout<<"Vi tri chen khong hop le!"<<endl; else { for(i=n-1;i>=k;i--) a[i+1]=a[i]; nhapsv(a[k]); n++; cout<<"Danh sach sau khi chen!"<<endl; for(i=0;i<n;i++) insv(a[i]); } } //tim kiem nhi phan theo diem dieu kien diem dc sap xep int timkiemnp(float x,sinhvien a[],int n) { int l=0;int r=n-1; while(l<=r) { k=(l+r)/2; if(x<a[k].diem) r=k-1; else if(x>a[k].diem) l=k+1; else return k+1; } return 0; } //tim kiem tuan tu theo masv int timkiemttm(char msv[],sinhvien a[],int n) { int i=0; while((strcmp(a[i].masv,msv)!=0)&&(i<n)) i++; if(i>n-1) return 0; else return i+1; } //Tim kiem tuan tu theo ho ten int timkiemttt(char ht[],sinhvien a[],int n) { int i=0; while((strcmp(a[i].hoten,ht)!=0)&&(i<n)) i++; if(i>n-1) return 0; else return i+1; } //Ham lua chon void luachon() { int so; cout<<"Lua chon : ";cin>>so; switch(so) { case 1: cout<<endl<<"_______________________________"<<endl; cout<<"Nhap so sinh vien: ";cin>>n; for(i=0;i<n;i++) nhapsv(a[i]); dk=1; luachon(); break; case 2:if(dk==0) { cout<<"Danh sach rong (chon 1 de nhap truoc)"<<endl; luachon(); } else { cout<<endl<<"_______________________________"<<endl; cout<<"In danh sach"<<endl; for(i=0;i<n;i++) insv(a[i]); luachon(); }break; case 3:if(dk==0) { cout<<"Danh sach rong (chon 1 de nhap truoc)"<<endl; luachon(); } else { cout<<endl<<"_______________________________"<<endl; cout<<"Nhap vi tri chen: "; int k;cin>>k; chensv(k-1,a,n); luachon(); }break; case 4:if(dk==0) { cout<<"Danh sach rong (chon 1 de nhap truoc)"<<endl; luachon(); } else { cout<<endl<<"_______________________________"<<endl; cout<<"Tim (Masv-Hoten-Diem/'m'-'h'-'d'):"; char c; cin>>c; if(c=='m') { cout<<"Nhap masv can tim: "; char msv[10];cin>>msv; int vt=timkiemttm(msv,a,n); if(vt==0) cout<<"Khong tim thay trong danh sach"<<endl; else cout<<"Vi tri can tim thay :"<<vt<<endl; luachon(); } else if(c=='d') { cout<<"Nhap diem can tim: "; float d;cin>>d; int vt=timkiemnp(d,a,n); if(vt==0) cout<<"Khong tim thay trong danh sach"<<endl; else cout<<"Vi tri can tim thay :"<<vt<<endl; luachon(); } else if(c=='h') { cout<<"Nhap ho ten can tim: "; char ht[30];cin>>ht; int vt=timkiemttt(ht,a,n); if(vt==0) cout<<"Khong tim thay trong danh sach"<<endl; else cout<<"Vi tri can tim thay :"<<vt<<endl; luachon(); } else cout<<"Chon khong dung !"<<endl; } default:cout<<"Ban da thoat chuong trinh !"<<endl; } } //Ham chinh void main() { cout<<"_________________________________"<<endl; cout<<"Nhan (1) Nhap danh sach"<<endl; cout<<"Nhan (2) Xuat danh sach"<<endl; cout<<"Nhan (3) Chen danh sach"<<endl; cout<<"Nhan (4) TKiem danh sach"<<endl; cout<<"Nhan (Khac) De thoat ! "<<endl; cout<<"Nhan (5) TKiem danh sach"<<endl; cout<<"_________________________________"<<endl; luachon(); } có ai thắc góp ý code để hoàn chỉnh hơn nha!