0% found this document useful (0 votes)
55 views20 pages

VD DSLK

The document describes code for managing a linked list of student data in Vietnamese. It defines structures to store student and node data, functions to initialize a linked list, insert and delete nodes, search by student ID, and sort the list by name. It provides functions to input student data, print node info, traverse the list, and demonstrates usage in a main function that inputs data, performs various operations, and prints results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views20 pages

VD DSLK

The document describes code for managing a linked list of student data in Vietnamese. It defines structures to store student and node data, functions to initialize a linked list, insert and delete nodes, search by student ID, and sort the list by name. It provides functions to input student data, print node info, traverse the list, and demonstrates usage in a main function that inputs data, performs various operations, and prints results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Danh sách liên kết

Danh sách liên kết đơn


#include<iostream>
#include<string>
using namespace std;
struct SinhVien
{string malop;
string masinhvien;
string tensinhvien;
int namsinh;
string quequan;};
typedef SinhVien TypeofData;
//Nhap du lieu cho data
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);
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;
}
//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 trong ds
void insertAfter (TypeofData data, NodePtr p,NodePtr &L)
{ if(p==NULL)
cout<<"Khong them duoc!"<<endl;
else
{NodePtr q=createnode(data);
q->next=p->next;
p->next=q;}
}
//Xoa nut p ra 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)//p o dau ds
{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 ds
void deleteHead(NodePtr &L)
{if(L==NULL) return;
NodePtr p=L;
L=L->next;
p->next=NULL;
delete p;}
//Xoa mot phan tu o cuoi ds
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 searchbymasv (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 ds
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
cout<<"Thong tin sinh vien"<<++i<<":"<<endl;
printNodeinfo(q);
q=q->next;}
}
}
//Sap xep danh sach tang dan theo ten
void sortlistbyname (NodePtr &L)
{ if (L==NULL)
cout<<"Danh sach rong, khong sap xep!\n";
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 thon gtin sinh vien thu "<<i+1<<":"<<endl;
TypeofData sinhvien = inputTypeofData();
//insertHead(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<<"\n======================================"<<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;//Ma sinh vien: Nhap tu ban phim de tim kiem
NodePtr found;//Nut chua ket qua tim kiem
cout<<"\n======================================"<<endl;
cout<<"XOA SINH VIEN"<<endl;
fflush(stdin);
cout<<"Nhap ma sinh vien de tim kiem:";
getline(cin,masv);
found=searchbymasv(masv,L);
if(found==NULL) cout<<"Khong tim thay sinh vien voi ma "<<masv;
else{
cout<<"Tim thay sinh vien voi ma "<<masv;
printNodeinfo(found);
cout<<"Xoa sinh vien khoi danh dach"<<endl;
deletelist(found,L);
cout<<"Danh sach sinh vien sau khi xoa:"<<endl;traverse(L);
}
cout<<"\n======================================"<<endl;
cout<<"THEM SINH VIEN"<<endl;
fflush(stdin);
cout<<"Nhap ma sinh vien de tim kiem:";
getline(cin,masv);
found=searchbymasv(masv,L);
if(found==NULL) cout<<"Khong tim thay sinh vien voi ma "<<masv;
else{
cout<<"Tim thay sinh vien voi ma "<<masv;
printNodeinfo(found);
cout<<"Nhap thong tin sinh vien can them,sau sinh vien:"<<found-
>data.tensinhvien<<":"<<endl;
TypeofData sinhvien = inputTypeofData();
insertAfter(sinhvien,found,L);
cout<<"Danh sach sinh vien sau khi them:"<<endl;traverse(L);}
cout<<"\n======================================"<<endl;
cout<<"SAP XEP DANH SACH"<<endl;
sortlistbyname(L);
cout<<"Danh sach sinh vien sau khi sap xep:"<<endl;traverse(L);
}

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);
}
}

Danh sách liên kết đôi


#include<iostream>
#include<string>
using namespace std;
struct NhanVien{
string maNhanVien;
string tenNhanVien;
int namSinh;
float heSoLuong;
};
typedef NhanVien TypeofData;
struct TypeofNode
{
TypeofData data;
struct TypeofNode *next;
struct TypeofNode *prev;
};
typedef struct TypeofNode *NodePtr;
TypeofData inputTypeofData()
{
TypeofData nhanvien;
cout <<"- Ho ten :";
fflush(stdin);
getline(cin,nhanvien.tenNhanVien);
cout << "- Nam sinh :";
cin>> nhanvien.namSinh;
cout <<"- Ma nhan vien :";
fflush(stdin);
getline(cin,nhanvien.maNhanVien);
cout <<"- He so luong :";
cin>>nhanvien.heSoLuong;
return nhanvien;
}
//Khoi tao
void initialize (NodePtr &L, NodePtr &R)
{
L = NULL;
R = NULL;
}
//KIEM TRA RONG
int isEmpty(NodePtr L , NodePtr R)
{
return ((L == NULL || R == NULL )? true : false);
}
// Tao mot nut
NodePtr CreateNode (TypeofData data){
NodePtr newNode = new TypeofNode;
newNode -> next = NULL;
newNode -> prev = NULL;
newNode -> data = data;
return newNode;
}
//IN thong tin cua 1 nut
void printNodeInfo (NodePtr pnode)
{
cout << " - Ho ten :"<< pnode->data.tenNhanVien <<endl;
cout << " - Nam sinh :"<< pnode->data.namSinh <<endl;
cout << " - Ma nhan vien :"<< pnode->data.maNhanVien <<endl;
cout << " - He so luong :"<< pnode->data.heSoLuong <<endl;
}
//Them 1 phan tu vao dau danh sach
void insertHead(NodePtr &L, NodePtr &R , TypeofData data){
NodePtr newNode = CreateNode(data);
if(L == NULL || R == NULL){
L = newNode ;
R = newNode ;
return;
}
L->prev = newNode;
newNode->next = L;
L = newNode;
}
// Them 1 phan tu vao cuoi danh sach
void insertTail(NodePtr &L, NodePtr &R , TypeofData data){
NodePtr newNode = CreateNode(data);
if(L == NULL || R == NULL){
L = newNode ;
R = newNode ;
return;
}
R->next = newNode;
newNode->prev= R;
R = newNode;
}
// Them 1 phan tu vao sau 1 nut trong danh sach
void insertAfter(TypeofData data,NodePtr p, NodePtr &L, NodePtr &R ){
if (p == NULL)
cout <<"Khong them duoc !"<<endl;
else
{
if(L == NULL && R == NULL){
insertHead(L,R,data);
return;
}
else if(p == R)
{
insertTail(L,R,data);
return;
}
else
{ NodePtr q = CreateNode(data);
q->prev = p;
q->next = p->next;
p->next->prev = q;
p->next = q;
}
}

}
// 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);
}

You might also like