BỘ GIÁO DỤC VÀ ĐÀO TẠO TRƯỜNG ĐẠI HỌC TÀI NGUYÊN VÀ MÔI TRƯỜNG HÀ NỘI
KHOA: CÔNG NGHỆTHÔNG TIN
BÀI TẬP LỚN
Cấu Trúc Dữ Liệu Và Giải Thuật
Giáo Viên Hướng Dẫn: CôPhíThịHảiYến
Nhóm Sinh Viên Thực Hiện: Nhóm 2
1.
2.
3.
4.
5.
6.
7.
LêĐìnhThành
NguyễnThịYến
Chu ThịThắm
LưuThịNhậtLệ
PhạmThịTrang
NguyễnThịThùy
ĐàoĐứcTrung
DANH SÁCH LIÊN KẾT VÒNG
Danhsáchliênkếtvònglàcấutrúcdữliệugồm 1 tậpcácphầntử ,
trongđómỗiphầntửlà 1 phầncӫa 1 nútcóchӭamộtliênkếttớinútkếtiếp.
Nói “mỗiphầntửlà 1 phầncӫa 1 nút” bởivìmỗinútngoàiviệcchӭathông
tin vềphầntửcònchӭathông tin vềliênkếttớinúttiếptheotrongdanhsách.
Trongdanhsáchliênkếtđơn,
nútcuốicùngcӫadanhsáchsẽliênkếttrỏđếnmộtgiátrị NULL
chobiếtdanhsáchđãkếtthúc. Nhưngở
danhsáchliênkếtvòngnàykhôngtrỏđến NULL màtrỏvềnútđầutiên.
Ưuđiểmcủadanhsáchliênkếtvònglàbấtkỳnútnàocũngcóthểcoilàđầucӫadan
hsách.Cónghĩalàtừmộtnútbấtkỳ, ta cóthểtiếnhànhduyệt qua
toànbộcácphầntửcӫadanhsáchmàkhôngcầntrởvềnútđầutiênnhưtrongdanh
sáchliênkếtthôngthường.Tuynhiên,
nhѭợcđiểmcӫadanhsáchnàylàcóthểkhôngbiếtkhinàothìđãduyệt qua
toànbộphầntửcủadanhsách.Điềunàydẫnđếnmộtquátrìnhduyệtvôhạn,
khôngcóđiểmdừng.Đểkhắcphụcnhượcđiểmnày,
trongquátrìnhduyệtluônphảikiểmtraxemđãtrởvềnút ban đầu hay
chưa.Việckiểmtranàycóthểdựatrêngiátrịphầntửhoặcbằngcáchthêmvàomộ
tnútđặcbiệt.Quyước: Dùng con trỏ Last quảnlýdanhsáchliênkếtvòng, con
trỏnàychỉtớiphầntửcuốicùngtrongdanhsách. Nếudanhsáchrỗng: Last =
NULL Nếudanhsáchcó 1 phầntử: Last = Last->Next
Khaibáo:
Struct node
{
Int Item;
Struct node *Next;
};
Sauđây, chúng ta
sẽxétviệcsửdụngdanhsáchliênkếtvòngđểgiảiquyếtbàitoánnhưsau:
Cho mộtdanhsáchlưutrữcácsốnguyên.Viếtchươngtrìnhtạomột Menu
thựchiệncáccôngviệcsau (Sửdụngdanhsáchliênkếtvòng):
1. Khởitạodanhsách, quátrìnhnhậpsẽdừnglạikhinhậpdấu “#”
2. Kiểmtraxemtạivịtríthứ 5 cóphảilàsốnguyêntố hay không?
Nếuđúnghãyxóabỏphầntửnày.
3. Tínhtíchcácsốchẵn, dương, chia hếtcho 5 (khôngkểsố 0).
4. Sắpxếpdanhsáchtheothứtựtăngdần.
5. Xóabỏnhữngphầntửtrùngnhautrongdanhsách (chỉgiữlạimộtphầntử).
6. Đếmsốlượngcácsốhoànhảotrongdanhsách. Cácsốhoànhảonằm ở
nhữngvịtrínào?
7. Xóatấtcảcácsố 0 trongdanhsách.
8. Nhậpvàomộtsố k, đếmcácsố = k, sốlượngđếmđượclàsốchẵn hay lẻ?
9. Tínhtrungbìnhcộngcácsốlẻdѭơngtrongdanhsách.
ChươngTrình
#include<conio.h>
#include<stdio.h>
using namespace std;
struct Node{
int data;
Node *next;
};
struct List{
Node *head;
Node *tail;
};
voidInit(List &l){ // khoitao
l.head = l.tail = NULL;
}
Node *creatNode(int x ){
Node *p = new Node;
p->next = NULL;
p->data = x;
return p;
}
boolisEmpty(List l ){
if(l.head == NULL ) return true;
return false;
}
intlen(List l){
int count=0;
Node *p=l.head;
do{
count+=1;
p=p->next;
}while(p!=l.head);
return count;
}
voidaddHead(List &l, int x ){
Node *p = creatNode(x);
if(isEmpty(l)) l.head = l.tail = p;
else{
p->next = l.head; // con tro next cua p trotoidia chi cua node head(ban dau)
l.head = p; // cap nhat node head(lucsau )
}
l.tail->next = l.head; // khepvong don.
}
voidaddTail(List &l, int x ){
Node *p = creatNode(x);
if(isEmpty(l)) addHead(l,x);
else{
l.tail->next = p;
l.tail = p;
}
l.tail->next = l.head; // khepvong don
}
Node *search(List l, int k ){
Node *p = l.head;
do {
if( p->data == k ) return p;
else p = p->next;
} while( p != l.head );
return NULL;
}
voidaddMid(List &l, int x, int k ){ // chen node co data = x vaosau node co data = k;
Node *p = search(l,k);
if(p!=NULL){
Node *q= creatNode(x);
Node *r = p->next;
p->next = q;
q->next = r;
}
elsecout<<"\nKhongtimthay node co data = k.";
}
voiddelHead(List &l ){
if(!isEmpty(l)){
if(l.head != l.tail ){
Node *p = l.head;
l.head = l.head->next; // cap nhatl.head
delete p; // xoabo node head ban dau
l.tail->next = l.head;
}
elsel.head = NULL;
}
else return;
}
voiddelTail(List &l ){
if(!isEmpty(l)){
if(l.head != l.tail ){
Node *p = l.head;
Node *q = new Node;
while(p->next != l.tail ) p = p->next; // tim node ngaytruoc tail
q = p; // gan node nay cho node q
p = p->next; // p chinh la node tail can xoa
l.tail = q; // cap nhatl.tail
l.tail->next = l.head;
delete p;
} else l.head = NULL;
}
else return;
}
voiddelAtK(List &l, int index ){
if(index == 1) delHead(l);
else if( index == len(l) ) delTail(l);
else{
if(!isEmpty(l)){
Node *p = l.head;
Node *q = new Node;
for(int i=1;i
q = p;
p= p->next;
}
q->next = p->next; // cho node next cua node k-1 trotoi node k+1;
delete p;
}
}
}
// xuat thong tin ra man hinh
voidxuat(List l ){
if(l.head){
cout<< "\nDanhsachcacphantu:"<
Node *p = l.head;
do{
cout<<" " << p->data;
p = p->next;
}while( p != l.head );
}
elsecout<< "\nChuanhapdanhsach!";
cout<
}
//cau 1
voidkhoiTao(List &l){
intsoNguyen;
cout<<"NhapVaodanhsach(Bam # dung lai):"<
while(scanf("%d",&soNguyen)){
addTail(l,soNguyen);
}
}
//cau 2
voidcheckSNTandDelete(List &l,int index){
if (l.head==NULL) cout<<"Khong co phantu de kiemtra!";
else{
int i;
bool flag=true;
Node *p = l.head;
Node *q = new Node;
for(i=1;i
q = p;
p= p->next;
}
for (i=2;i<=p->data/2;i++){
if (p->data%i==0){
flag=false;
break;
}
}
if (flag){
cout<<"Phantuthu "<
data<<": la so nguyen to"<if (p==l.head){
delHead(l);
cout<<"Da xoaphantu nay!!";
}
else if (p==l.tail){
delTail(l);
cout<<"Da xoaphantu nay!!";
}
else{
q->next = p->next; // cho node next cua node k-1 trotoi node k+1;
delete p;
cout<<"Da xoaphantu nay!!";
}
}
else
cout<<"Phantuthu "<data<<": khongphai la so nguyen to"<}
}
//cau 3
void chanDuongchia5(List l){
if(l.head==NULL) cout<<"\nKhong co phantu de tinh!";
else{
intTich=1;
Node *p = l.head;
do{
if(p->data>0 && p->data%10==0) Tich*=p->data;
p=p->next;
}while(p!=l.head);
cout<<"Tich can tinh la "<}
}
//cau 4
voidsapXep(List &l){
Node *p,*q;
for(p=l.head;p!=l.tail;p=p->next)
for(q=p->next;q!=l.head;q=q->next){
if (p->data>q->data){
int temp=p->data;
p->data=q->data;
q->data=temp;
}
}
}
//cau 5
voidonlyOne(List &l){ // thuattoan: duyet 2 vong for, duyentungphantuvoicacphantu con lai.
neutrungthibo di
Node *p=l.head,*q,*temp;
do{
q=p->next;
temp=p; // cho temp ngaytruoc q
do{
if(q->data==p->data){
temp->next=q->next;
delete q;
q=temp->next;
}
temp=temp->next;
q=q->next;
}while(q!=l.head);
p=p->next;
}while(p!=l.head);
}
//cau 6
boollaSoHoanHao(int x){ // dieukien la so hoanhao
int sum=0;
for(int i=1;i<=x/2;i++){
if(x%i==0) sum+=i;
}
if(sum==x) return true;
else return false;
}
voidcheckSoHoanHao(List l){
cout<<"\nCac so hoanhao la: "<int k=0,index=0,flag=0;
Node *p=l.head;
do{
index+=1;
if(laSoHoanHao(p->data)){
flag++;
cout<data<<" o vi tri thu "<}
p=p->next;
}while(p!=l.head);
if(flag==0) cout<<"\nKhong co so hoanhaonao "<}
//cau 7
void xoaSo0(List &l){
intbienChay=1;
int temp=len(l);
Node *p=l.head,*q;
while(bienChay<=temp){
if(p==l.head&& p->data==0){
delHead(l);
delete p;
p=l.head;
continue; // check truong hop co cac so 0 dung o daungaycanhnhau
}
else if(p->data==0){
q->next=p->next;
delete p;
p=q->next;
}
q=p; // cho q chaysau p
p=p->next;
++bienChay;
}
}
//cau 8
voidtimSoBangK(List l){
cout<<"Nhapvao so k: ";
int k=0,count=0;
scanf("%d",&k);
Node *p=l.head;
do{
if(p->data==k){
count+=1;
}
p=p->next;
}while(p!=l.head);
cout<<"Co tat ca "<
if(count%2==0)
cout<<"So luongdemduoc la so chan"<else
cout<<"So luongdemduoc la so le"<}
//cau 9
voidTBCsoLeDuong(List l){
int count=0;
int Sum=0;
Node *p=l.head;
do{
if(p->data%2>0){
count+=1; // tinh so cac so le duongtrongdanhsach
Sum+=p->data;
}
p=p->next;
}while(p!=l.head);
cout<<"Co tat ca "<cout<<"Trungbinhcongcac so le duongtrongdanhsach la "<}
void menu()
{
List l;
Init (l);
int index=0;
charlc;
while (1)
{
cout<<"\n______MENU______\n0.In danhsach\n1.Khoi taodanhsach.\n2.Kiem traphantu "
<<"bat ki la so nguyen to.\n3.Tinh tichcac so chan,duong, chia "
<<"het cho 5.\n4.Sap xepdanhsachtheothutu tang dan.\n5.Xoa "
<<"bonhungphantutrungnhautrongdanhsach.\n6.Dem so luongva "
<<"chi ra vi tri cac so hoanhao.\n7.Xoa tat ca so 0 trongdanhsach."
<<"\n8.Dem cac so bang k, va chi ra so luong la chan hay le.\n9."
<<"Tinhtrungbinhcac so le duongtrongdanhsach.\nBam ESC de thoatra\n"<fflush(stdin);
lc = getch();
if (lc=='0')
{
xuat(l);
getch();
}
else if (lc=='1')
{
khoiTao(l);
xuat(l);
}
else if (lc=='2')
{
cout<<"Nhap vi tri phantu can kiemtra:";
scanf("%d",&index);
checkSNTandDelete(l,index);
xuat(l);
getch();
}
else if (lc=='3')
{
chanDuongchia5(l);
xuat(l);
getch();
}
else if (lc=='4')
{
sapXep(l);
cout<<"Da sap xepdanhsach"<xuat(l);
getch();
}
else if (lc=='5')
{
onlyOne(l);
cout<<"Da xoacacphantutrungnhau"<xuat(l);
getch();
}
else if (lc=='6')
{
checkSoHoanHao(l);
getch();
}
else if (lc=='7')
{
xoaSo0(l);
cout<<"Da xoacac so 0!!"<xuat(l);
getch();
}
else if (lc=='8')
{
timSoBangK(l);
getch();
}
else if (lc=='9')
{
TBCsoLeDuong(l);
getch();
}
else if (lc==27)
{
break;
}
}
}
int main()
{
menu();
return 0;
}