VD DSLK
VD DSLK
Bài 1:
#include<iostream>
#include<string>
using namespace std;
struct SinhVien
{ int masv;
string tensv;
string lop;
float dtk;
string hanhkiem;};
typedef SinhVien SV;
struct Danhsach
{ SV data;
Danhsach *next;
};
typedef Danhsach *Node;
SV nhap()
{ SV sinhvien;
cout<<"Nhap ma sinh vien:";cin>>sinhvien.masv;
cout<<"Nhap ten sinh vien:";fflush(stdin);getline(cin,sinhvien.tensv);
cout<<"Nhap lop:";fflush(stdin);getline(cin,sinhvien.lop);
cout<<"Nhap diem tong ket:";cin>>sinhvien.dtk;
cout<<"Nhap hanh kiem(tot,kha,trung
binh,yeu):";fflush(stdin);getline(cin,sinhvien.hanhkiem);
return sinhvien;}
void khoitao(Node &L)
{ L=NULL;}
void inthongtin(Node p)
{ cout<<"Ma sinh vien:"<<p->data.masv<<" | "<<" Ten sv:"<<p->data.tensv
<<" | "<<" Lop:"<<p->data.lop<<" | "<<" DTK:"<<p->data.dtk<<" | "<<" Hanh kiem:"<<p-
>data.hanhkiem<<endl;
}
Node taonut(SV data)
{ Node newnode=new Danhsach;
newnode->next=NULL;
newnode->data=data;
return newnode;}
void chennutcuoi(Node &L,SV data)
{ Node newnode,p;
newnode=taonut(data);
if(L==NULL) L=newnode;
else
{p=L;
while(p->next!=NULL)
{ p=p->next;}
p->next=newnode;}
}
Node timkiem(Node L)
{ if(L==NULL)
cout<<"Danh sach rong!"<<endl;
else{
Node p;
p=L;
while(p!=NULL)
{if(p->data.lop.compare("D13CNPM")==0)
inthongtin(p);
p=p->next;}
}
}
void duyetds(Node L)
{ if(L==NULL)
cout<<"Danh sach rong!"<<endl;
else{
Node p;
p=L;
int i=0;
while(p!=NULL)
{cout<<"Thong tin sinh vien thu "<<++i<<endl;
inthongtin(p);cout<<"\n";
p=p->next;}
}
}
void xoanutcuoi(Node &L)
{ if(L==NULL) return;
Node p=L;
Node q=NULL;
while(p->next!=NULL)
{ q=p;
p=p->next;}
if(q!=NULL)
{q->next=NULL;
delete p;}
else{L=NULL;
delete p;}
}
int main()
{ int n;
cout<<"Nhap so luong sinh vien:";cin>>n;
Node L;
khoitao(L);
cout<<"----Nhap thong tin sinh vien----"<<endl;
for(int i=0;i<n;i++)
{ cout<<"Nhap thong tin sinh vien thu "<<i+1<<endl;
SV sv=nhap();
chennutcuoi(L,sv);}
cout<<"----Hien thi thong tin vua nhap----"<<endl;
duyetds(L);
cout<<"----Danh sachn sinh vien co lop D13CNPM----"<<endl;
timkiem(L);
cout<<"----Xoa phan tu cuoi----"<<endl;
xoanutcuoi(L);
duyetds(L);
}
Bài 16 :
#include<iostream>
#include<string>
using namespace std;
struct Duan
{ int maduan;
string tenduan;
string quimoduan;
float tongkinhphi;
};
typedef Duan DA;
//Nhap du lieu cho data
DA inputDA()
{ DA duan;
cout<<"- Ma du an:";cin>>duan.maduan;fflush(stdin);
cout<<"- Ten du an:";getline(cin,duan.tenduan);fflush(stdin);
cout<<"- Qui mo du an:";getline(cin,duan.quimoduan);
cout<<"- Tong kinh phi:";cin>>duan.tongkinhphi;
return duan;}
struct TypeofNode
{ DA data;
struct TypeofNode *next;};
typedef struct TypeofNode *Node;
//In thong tin cua mot nut
void thongtinnut(Node p)
{
cout<<"- Ma du an:"<<p->data.maduan<<endl;
cout<<"- Ten du an:"<<p->data.tenduan<<endl;
cout<<"- Qui mo du an:"<<p->data.quimoduan<<endl;
cout<<"- Tong kinh phi:"<<p->data.tongkinhphi<<endl;
}
//Khoi tao
void khoitao(Node &L)
{ L=NULL;}
//Kiem tra rong
int isEmpty(Node L)
{return ((L==NULL)? true:false);}
//Tao mot nut
Node createnode(DA data)
{ Node newnode= new TypeofNode;
newnode->next= NULL;
newnode->data=data;
return newnode;}
//Chen mot nut vao dau danh sach
void insertHead (Node &L,DA data)
{ Node newnode=createnode(data);
if(L==NULL)
{L=newnode;}
else {
newnode->next=L;
L=newnode;}
}
//Xoa nut ra khoi danh sach L
void deletelist (Node p,Node &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
{ Node q;
q=L;
while(q!=NULL && q->next!=p)
q=q->next;
q->next=p->next;}
p->next=NULL;
delete p;}
}
}
//thong tin nhung du an co tong kinh phi 5000-10000 ty
void inthongtin(Node L)
{ if(L==NULL) cout<<"Danh sach rong!";
else
{ Node q;
q=L;
while (q!=NULL)
{if(q->data.tongkinhphi>=5000 && q->data.tongkinhphi <=10000)
thongtinnut(q);
q=q->next;}
}}
//Tim kiem theo ma du an
Node searchbymada (int maduan, Node L)
{ if(L==NULL) return NULL;
else
{ Node q;
q=L;
while (q!=NULL && q->data.maduan != maduan)
q=q->next;
return q;}
}
//Duyet ds
void traverse (Node L)
{ if (L==NULL)
cout<<"Danh sach rong!\n";
else
{ Node q;
q=L;
int i=0;
while(q!=NULL)
{
cout<<"Thong tin du an "<<++i<<":"<<endl;
thongtinnut(q);
q=q->next;}
}
}
int main()
{ int n;
cout<<"Nhap so luong du an:";
cin>>n;
Node L;
khoitao(L);
int j=n+1;
for(int i=0;i<n;i++)
{ cout<<"Nhap thong tin du an thu "<<--j<<":"<<endl;
DA duan = inputDA();
insertHead(L,duan);
}
cout<<"Danh sach du an vua nhap:"<<endl;
traverse(L);
cout<<"Thong tin du an co tong chi phi tu 5000-10000 ty la:"<<endl;
inthongtin(L);
int mada;
Node found;
cout<<"\n======================================"<<endl;
cout<<"-------------XOA DU AN----------------"<<endl;
fflush(stdin);
cout<<"Nhap ma du an de tim kiem:";
cin>>mada;
found=searchbymada(mada,L);
if(found==NULL) cout<<"Khong tim thay du an voi ma "<<mada;
else{
cout<<"Tim thay du an voi ma "<<mada<<"\n";
thongtinnut(found);
cout<<"----Xoa du an khoi danh dach----"<<endl;
deletelist(found,L);
cout<<"Danh sach du an sau khi xoa:"<<endl;traverse(L);
}
}
}
// Xoa nut p khoi danh sach L
void deleteList (NodePtr p, NodePtr &L , NodePtr &R){
if(p == NULL)
cout <<" Phan tu can xoa khong ton tai trong danh sach !"<<endl;
else
{
if (isEmpty(L,R))
cout<<"Danh sach rong !"<<endl;
else {
if(L == R && p==L)
{
L==NULL;
R==NULL;
}
else if (p==L){
L= L->next;
L->prev = NULL;
p->next = NULL;
}
else if (p == R){
R = R ->prev;
R ->next = NULL;
p->prev = NULL;
}
else {
p->prev -> next=p->next;
p->next -> prev = p->prev;
p->next = NULL;
p->prev = NULL;
}
delete p;
}
}
}
void deleteAtHead (NodePtr &L, NodePtr &R)
{
deleteList(L,L,R);
}
void deleteAtTail (NodePtr &L, NodePtr &R)
{
deleteList(R,L,R);
}
//Xoa tat ca cac nut co ho ten cho truoc
void DeleteByKey (NodePtr &L, NodePtr &R , string tenNhanVien){
if ( L== NULL)
cout << "Danh sach rong !\n";
else {
NodePtr p,q;
q=L;
int i =0;
while (q!=NULL){
p=q;
q=q->next;
if(p->data.tenNhanVien.compare(tenNhanVien)==0)
deleteList(p,L,R);
}
}
}
// Tim kiem theo ma nhan vien
NodePtr searchFromHeadByMaNhanVien(string maNhanVien,NodePtr L)
{
if(L == NULL) return NULL;
else {
NodePtr q;
q = L;
while (q != NULL && q->data.maNhanVien.compare(maNhanVien)!=0)
q= q->next;
return q;
}
}
// Tim kiem theo ma nhan vien
NodePtr searchFromTailByMaNhanVien(string maNhanVien,NodePtr R)
{
if(R == NULL) return NULL;
else {
NodePtr q;
q = R;
while (q != NULL && q->data.maNhanVien.compare(maNhanVien)!=0)
q= q->prev;
return q;
}
}
// Duyet danh sach tu dau
void traverseFromHead(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 nhan vien :"<<++i<<":"<<endl;
printNodeInfo(q);
q=q->next;
}
}
}
// Duyet danh sach tu cuoi
void traverseFromTail(NodePtr R)
{
if (R==NULL)
cout<<"Danh sach rong !\n";
else
{
NodePtr q;
q=R;
int i=0;
while (q!=NULL){
// In thong tin sinh vien
cout <<" Thong tin nhan vien :"<<++i<<":"<<endl;
printNodeInfo(q);
q=q->prev;
}
}
}
// Sap xep danh sach tang dan theo ten bang cach duyet danh sach tu dau
void sortListByNameFromHead(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.tenNhanVien.compare(q->data.tenNhanVien)>0) {
TypeofData data =p->data;
p->data =q->data;
q->data = data;
}
}
}
}
// Sap xep danh sach tang dan theo ten bang cach duyet danh sach tu cuoi
void sortListByNameFromTail(NodePtr &R) {
if(R==NULL)
{
cout<<"Danh sach rong , khong sap xep !" <<endl;
}
else {
for(NodePtr p=R;p->prev!=NULL;p=p->prev)
for(NodePtr q=p->prev; q!=NULL;q=q->prev)
{
if(p->data.tenNhanVien.compare(q->data.tenNhanVien)>0) {
TypeofData data =p->data;
p->data =q->data;
q->data = data;
}
}
}
}int main () {
int n ;
cout<<"Nhap so luong Nhan Vien:" ;
cin>>n;
NodePtr L,R;
initialize(L,R);
for(int i=0;i<n;i++) {
cout<<"Nhap thong tin nhan vien " <<i+1<<":"<<endl;
TypeofData nhanvien = inputTypeofData();
//insertHead(L,R,nhanvien);
insertTail(L,R,nhanvien);
}
cout <<"===>Danh sach nhan vien vua nhap (FROM HEAD ):" <<endl;
traverseFromHead(L);
//cout <<"===>Danh sach nhan vien vua nhap (FROM TAIL):"<<endl;
//traverseFromTail(R);
cout <<"\n============================================" <<endl;
cout<<" XOA NHAN VIEN DAU DANH SACH " <<endl;
deleteAtHead(L,R);
cout<<"===> Danh sach nhan vien sau khi xoa Dau (FROM HEAD):" << endl;
traverseFromHead(L);
//cout<<"===> Danh sach nhan vien sau khi xoa Dau (FROM Tail):" << endl;
//traverseFromTail(R);
cout<<"\
n======================================================="<<endl;
cout<< " XOA NHAN VIEN CUOI DANH SACH "<<endl;
deleteAtTail(L,R);
cout <<"====> Danh sach nhan vien sau khi Xoa cuoi ( FROM HEAD): "<<endl;
traverseFromHead(L);
//cout <<"====> Danh sach nhan vien sau khi Xoa cuoi ( FROM Tail): "<<endl;
//traverseFromTail(R);
string manv;
NodePtr found;
/*
cout<<"======================================================="<<endl
;
cout<< " XOA NHAN VIEN "<<endl;
fflush(stdin);
cout<<"Nhap ma nhan vien de tim kiem :";
getline(cin,manv);
found = searchFromHeadByMaNhanVien(manv,L);
if(found==NULL) cout <<"Khong tim thay nhan vien voi ma " <<manv;
else{
cout <<"Tim nhan nhan vien voi ma "<<manv<<":"<<endl;
printNodeInfo(found);
cout<<"Xoa nhan vien khoi danh sach ..."<<endl;
deleteList(found,L,R);
cout<<"===> Danh sach nhan vien sau khi xoa Dau (FROM HEAD):" <<
endl;
traverseFromHead(L);
//cout<<"===> Danh sach nhan vien sau khi xoa Dau (FROM Tail):" << endl;
//traverseFromTail(R);
}*/
cout<<"\
n======================================================="<<endl;
cout<< " THEM NHAN VIEN SAU MOT NHAN VIEN CHO TRUOC "<<endl;
fflush(stdin);
cout<<"Nhap ma nhan vien de tim kiem :";
getline(cin,manv);
found = searchFromHeadByMaNhanVien(manv,L);
if(found==NULL) cout <<"Khong tim thay nhan vien voi ma " <<manv;
else{
cout <<"Tim thay nhan nhan vien voi ma "<<manv<<":"<<endl;
printNodeInfo(found);
cout<<"Nhap thong tin nhan vien can them ,sau nhan vien :"<<found-
>data.tenNhanVien<<":"<<endl;
TypeofData nhanvien = inputTypeofData();
insertAfter (nhanvien,found,L,R);
cout<<"===> Danh sach nhan vien sau khi THEM (FROM HEAD):" << endl;
traverseFromHead(L);
//cout<<"===> Danh sach nhan vien sau khi THEM (FROM Tail):" << endl;
//traverseFromTail(R);
}
/* cout<<"\
n======================================================="<<endl;
cout<< " XOA TAT CA NHAN VIEN CO TEN CHO TRUOC "<<endl;
string tenNhanVien;
fflush(stdin);
cout<<"Nhap ten nhan vien de xoa:";
getline(cin,manv);
DeleteByKey(L,R,manv);
cout<<"===> Danh sach sau khi xoa het nhan vien co ten la :"
<<tenNhanVien<<"(FROM HEAD):"<<endl;
traverseFromHead(L);
//cout<<"===> Danh sach sau khi xoa het nhan vien co ten la :"
<<tenNhanVien<<"(FROM Tail):"<<endl;
//traverseFromTail(R);*/
cout<<"\
n======================================================="<<endl;
cout<<"SAP XEP DANH SACH "<<endl;
sortListByNameFromHead(L);
//sortListByNameFromTail(R);
cout <<"===>Danh sach nhan vien sau khi sap xep (FROM HEAD ):" <<endl;
traverseFromHead(L);
//cout <<"===>Danh sach nhan vien sau khi sap xep (FROM TAIL):"<<endl;
//traverseFromTail(R);
}
Bài 2:
#include<iostream>
#include<string>
using namespace std;
struct SinhVien
{ int masv;
string tensv;
string lop;
float dtk;
string hanhkiem;};
typedef SinhVien SV;
struct Danhsach
{ SV data;
Danhsach *next;
Danhsach *prev;
};
typedef Danhsach *Node;
SV nhap()
{ SV sinhvien;
cout<<"Nhap ma sinh vien:";cin>>sinhvien.masv;
cout<<"Nhap ten sinh vien:";fflush(stdin);getline(cin,sinhvien.tensv);
cout<<"Nhap lop:";fflush(stdin);getline(cin,sinhvien.lop);
cout<<"Nhap diem tong ket:";cin>>sinhvien.dtk;
cout<<"Nhap hanh kiem(tot,kha,trung
binh,yeu):";fflush(stdin);getline(cin,sinhvien.hanhkiem);
return sinhvien;}
void khoitao(Node &L,Node &R)
{ L=NULL;
R=NULL;}
int isEmpty(Node L,Node R)
{ return ((L==NULL||R==NULL)?true:false);}
void inthongtin(Node p)
{ cout<<"Ma sinh vien:"<<p->data.masv<<" | "<<" Ten sv:"<<p->data.tensv
<<" | "<<" Lop:"<<p->data.lop<<" | "<<" DTK:"<<p->data.dtk<<" | "<<" Hanh kiem:"<<p-
>data.hanhkiem<<endl;
}
Node taonut(SV data)
{ Node newnode=new Danhsach;
newnode->next=NULL;
newnode->prev=NULL;
newnode->data=data;
return newnode;}
void chennutcuoi(Node &L,Node &R,SV data)
{ Node newnode=taonut(data);
if(L==NULL||R==NULL)
{ L=newnode;
R=newnode;
return;}
R->next=newnode;
newnode->prev=R;
R=newnode;}
Node timkiemtudau(Node L)
{ if(L==NULL)
cout<<"Danh sach rong!"<<endl;
else{
Node q=L;
while(q!=NULL)
{if(q->data.lop=="D13CNPM")
inthongtin(q);
q=q->next;}
}
}
void xoanut(Node &L,Node &R,Node p)
{ if(p==NULL)
cout<<"Phan tu can xoa khong ton tai!"<<endl;
else{
if(isEmpty(L,R))
cout<<"Danh sach rong!"<<endl;
else{
if(L==R and p==L)
{L==NULL;
R==NULL;}
else{
if(p==L)
{ L=L->next;
L->prev=NULL;
p->next=NULL;}
else{if(p==R)
{R=R->prev;
R->next=NULL;
p->prev=NULL;}
else
{p->prev->next=p->next;
p->next->prev=p->prev;
p->next=NULL;
p->prev=NULL;}
delete p;
}
}
}
}
}
void xoacuoi(Node &L,Node &R)
{ xoanut(L,R,R);}
void duyetdstudau(Node L)
{ if(L==NULL)
cout<<"Danh sach rong!"<<endl;
else
{ Node q;
q=L;
int i=0;
while(q!=NULL)
{ cout<<"Thong tin sinh vien thu "<<++i<<endl;
inthongtin(q);
q=q->next;}
}
}
int main()
{ int n;
cout<<"Nhap so luong sinh vien:";cin>>n;
Node L,R;
khoitao(L,R);
cout<<"----Nhap thong tin sinh vien----"<<endl;
for(int i=0;i<n;i++)
{ cout<<"Nhap thong tin sinh vien thu "<<i+1<<endl;
SV sv=nhap();
chennutcuoi(L,R,sv);}
cout<<"----Hien thi danh sach sinh vien vua nhap----"<<endl;
duyetdstudau(L);
cout<<"----Thong tin sinh vien lop D13CNPM----"<<endl;
timkiemtudau(L);
cout<<"----Xoa sinh vien cuoi----"<<endl;
xoacuoi(L,R);
duyetdstudau(L);
}