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

26 2.3K 5
danh sách liên kết đơn

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

link list

[Tổng hợp ]Giải bài tập về Danh Sách Liên kết Thao tác trên danh sách liên kết đơn. bài 1 : với các yêu cầu sau. 1: Nhập dạnh sách.( Thêm đầu - Thêm cuối) 2: Xuất danh sách. 3: Liệt kê các phần tử mang phần tử chẵn. 4: Tìm phần tử có phần tử nhỏ nhất. 5: Đếm số lượng số nguyên tố trong danh sách. 6: Thêm phần tử X vào trước phần tử chẳn đầu tiên. 7: Thêm phần tử X vào sau phần tử lẽ cuối cũng. 8: Xoá phần tử nhỏ nhất trong danh sách. 9: Xoá phần tử đứng trước và sau X trong danh sách. 10: Tách danh sách hiện tại thành 2 danh sách sao cho danh sách 1 chứa các phần tử nguyên tố, danh sách 2 chứa các phần tử còn lại file dslk.h Code: #ifndef __DSLK__ #define __DSLK__ #include <iostream> #include <iomanip> #include <math.h> #include <conio.h> #include <windows.h> using namespace std; typedef struct node { int data; node* pNext; }NODE; typedef struct list { NODE* pHead; NODE* pTail; }LIST; void Init(LIST &l); NODE* GetNode(int x); void AddHead(LIST &l,NODE* new_ele); void AddTail(LIST &l,NODE* new_ele); void InPut(LIST &l); void OutPut(LIST l); void XuatChan(LIST l); NODE* TimMax(LIST l); int DemSNT(LIST l); void ThemXTruocChanDau(LIST &l,int x); void ThemXSauLeCuoi(LIST &l,int x); void XoaMin(LIST &l); void XoaPhanTuTruoc_SauX(LIST &l,int x); void TachDS(LIST &l,LIST &l1,LIST &l2); #endif file main.cpp Code: #include "dslk.h" int MeNu() { int c; cout<<endl<<endl<<endl<<" DANH SACH LIEN KET"<<endl; cout<<" "<<endl; cout<<" | 0 : Thoat. |"<<endl; cout<<" | 1 : Nhap danh sach. |"<<endl; cout<<" | 2 : Xuat danh sach. |"<<endl; cout<<" | 3 : Xuat Chan. |"<<endl; cout<<" | 4 : Tim max. |"<<endl; cout<<" | 5 : Dem so Nguyen To. |"<<endl; cout<<" | 6 : Them x truoc chan dau. |"<<endl; cout<<" | 7 : Them x sau le cuoi. |"<<endl; cout<<" | 8 : Xoa phan tu nho nhat trong danh sach. |"<<endl; cout<<" | 9 : Xoa phan tu truoc va sau x. |"<<endl; cout<<" | 10: Tach thanh 2 danh sach(nguyen to va k nguyen to). |"<<endl; cout<<" "<<endl; cout<<endl<<endl<<"ban chon:"; cin>>c; return c; } int main() { system("color 2C"); LIST l; int chon; Init(l); do { chon=MeNu(); switch(chon) { case 0: return 0; case 1: { InPut(l); }break; case 2: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else OutPut(l); }break; case 3: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else XuatChan(l); }break; case 4: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else cout<<"\n\nMax la:"<<TimMax(l)->data<<endl<<endl; }break; case 5: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else cout<<"so luong cac so nguyen to la"<<DemSNT(l); }break; case 6: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else { int x; cout<<"nhap x ="; cin>>x; ThemXTruocChanDau(l,x); } }break; case 7: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else { int x; cout<<"nhap x ="; cin>>x; ThemXSauLeCuoi(l,x); } }break; case 8: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else XoaMin(l); }break; case 9: { if(l.pHead==NULL) cout<<"danh sach rong - khong the thuc hien thao tac"; else { int x; cout<<"nhap x="; cin>>x; XoaPhanTuTruoc_SauX(l,x); } }break; case 10: { LIST l1,l2; if(l.pHead==NULL) cout<<"danh sach rong ko the tach dc"; else { TachDS(l,l1,l2); if(l1.pHead==NULL) { cout<<"khong co so nguyen to trong list 1"<<endl<<endl; cout<<"danh sach so k nguyen to trong list 2 la"<<endl; OutPut(l2); } else if(l2.pHead==NULL) { cout<<"danh sach so nguyen to trong list 1"<<endl; OutPut(l1); cout<<endl<<endl<<"danh sach list 2 khong co"<<endl; } else { cout<<"danh sach so nguyen to trong list 1"<<endl; OutPut(l1); cout<<endl<<endl<<"danh sach so k nguyen to trong list 2 la"<<endl; OutPut(l2); } } }break; default : cout<<"\n\nban chon ko co trong danh sach, xin nhap lai"<<endl<<endl;break; } }while(1); system("pause"); } file caidat.cpp Code: #include "dslk.h" int n; void Init(LIST &l) { l.pHead=l.pTail=NULL; } NODE* GetNode(int x) { NODE* p; p=new NODE; if(p==NULL) cout<<"cap phat bo nho khong du."; p->data=x; p->pNext=NULL; return p; } void AddHead(LIST &l,NODE* new_ele) { if(l.pHead==NULL) l.pHead=l.pTail=new_ele; else { new_ele->pNext=l.pHead; l.pHead=new_ele; } } void AddTail(LIST &l,NODE* new_ele) { if(l.pTail==NULL) l.pHead=l.pTail=new_ele; else { l.pTail->pNext=new_ele; l.pTail=new_ele; } } void InPut(LIST &l) { int x; cout<<"nhap so luong Node:"; cin>>n; Init(l); for(int i=1;i<=n;i++) { cout<<"nhap node x="; cin>>x; NODE* p=GetNode(x); AddHead(l,p); } } void OutPut(LIST l) { for(NODE* p=l.pHead;p;p=p->pNext) cout<<p->data<<" > "; cout<<"NULL"; } void XuatChan(LIST l) { for(NODE* p=l.pHead;p;p=p->pNext) if(p->data%2==0) cout<<p->data<<"\t"; } NODE* TimMax(LIST l) { NODE* max=l.pHead; for(NODE* p=l.pHead->pNext;p;p=p->pNext) if(p->data>max->data) max=p; return max; } int LaSNT(int x) { if(x<2) return 0; for(int i=2;i<=sqrtf(x);i++) if(x%2==0) return 0; return 1; } int DemSNT(LIST l) { int d=0; for(NODE* p=l.pHead;p;p=p->pNext) if(LaSNT(p->data)==1) d++; return d; } NODE* TimChanDau(LIST l) { for(NODE* p = l.pHead;p;p=p->pNext) if(p->data%2==0) return p; return NULL; } void ThempTruocq(LIST &l,NODE* p,NODE* q) { NODE*k=l.pHead; while(k->pNext!=q) k=k->pNext; p->pNext=q; k->pNext=p; } void ThemXTruocChanDau(LIST &l,int x) { NODE* p=GetNode(x); NODE* q=TimChanDau(l); if(q==l.pHead || q==NULL) AddHead(l,p); else ThempTruocq(l,p,q); } NODE* TimLeCuoi(LIST l) { NODE* p; NODE* k=NULL; for(p=l.pHead;p;p=p->pNext) if(p->data%2!=0) k=p; return k; } void ThempSauq(LIST &l,NODE* p,NODE* q) { p->pNext=q->pNext; q->pNext=p; } void ThemXSauLeCuoi(LIST &l,int x) { NODE* p=GetNode(x); NODE* q=TimLeCuoi(l); if(q==NULL || q==l.pTail) AddTail(l,p); else ThempSauq(l,p,q); } NODE* TimMin(LIST l) { NODE* min=l.pHead; for(NODE*p=l.pHead->pNext;p;p=p->pNext) if(p->data<min->data) min=p; return min; } void XoaDau(LIST &l) { NODE* h=l.pHead; l.pHead=l.pHead->pNext; delete(h); } void XoaCuoi(LIST &l) { NODE*p=l.pHead; while(p->pNext!=l.pTail) p=p->pNext; NODE* k=l.pTail; l.pTail=p; l.pTail->pNext=NULL; delete(k); } void Xoap(LIST &l,NODE* p) { NODE* k=l.pHead; while(k->pNext!=p) k=k->pNext; k->pNext=p->pNext; delete(p); } void XoaMin(LIST &l) { NODE* p=TimMin(l); if(p==l.pHead) XoaDau(l); else if(p==l.pTail) XoaCuoi(l); else Xoap(l,p); } NODE* TimX(LIST &l, int x) { NODE* p=l.pHead; for( ; p; p=p->pNext) if(p->data==x) return p; return NULL; } void XoapTruocq(LIST &l,NODE* p,NODE*q) { NODE*k=l.pHead; while(k->pNext!=p) k=k->pNext; k->pNext=q; delete(p); } void XoapSauq(LIST &l,NODE* p,NODE*q) { NODE* h=l.pHead; while(h->pNext!=l.pTail && h!=NULL) h=h->pNext; if(q==h) XoaCuoi(l); else { NODE*k=p; q->pNext=k->pNext; delete(p); } } void XoaPhanTuTruoc_SauX(LIST &l,int x) { NODE* q=TimX(l,x); if(q==NULL) cout<<"ko co gia tri x can tim"; else { if(q==l.pHead) cout<<""; else if(q==l.pHead->pNext) XoaDau(l); else { NODE*p=l.pHead; while(p->pNext!=q && p!=NULL) p=p->pNext; XoapTruocq(l,p,q); } if(q==l.pTail) cout<<""; else XoapSauq(l,q->pNext,q); } } void TachDS(LIST &l,LIST &l1,LIST &l2) { Init(l1); Init(l2); NODE* p=l.pHead,*pAdd; while(p) { int k=p->data; pAdd=GetNode(k); if(LaSNT(k)==1) AddHead(l1,pAdd); else AddHead(l2,pAdd); p=p->pNext; } Init(l); } Bài 2: cho 2 danh sách liên kết l1 và l2, gồm các phần tử là số nguyên, thực hiện các yêu cầu sau: 1: sắp xếp l1 và l2 tắng dần. 2: nối l1 và l2 thành l3 sao cho l3 tăng dần. file noidanhsachtang.h Code: #ifndef __NOIDS__ #define __NOIDS__ #include <iostream> #include <iomanip> #include <math.h> #include <conio.h> #include <windows.h> using namespace std; typedef struct node { int data; node* pNext; }NODE; typedef struct list { NODE* pHead; NODE* pTail; }LIST; void Init(LIST &l); NODE* GetNode(int x); void AddHead(LIST &l,NODE* new_ele); void AddTail(LIST &l,NODE* new_ele); void InPut(LIST &l); void OutPut(LIST l); void SapXep(LIST l); void NoiDS(LIST l1,LIST l2,LIST &l3); #endif file main.cpp Code: #include "noidanhsachtang.h" int MeNu() { int c; cout<<endl<<endl<<endl<<" DANH SACH LIEN KET"<<endl; cout<<" "<<endl; cout<<" | 0 : Thoat. |"<<endl; cout<<" | 1 : Nhap 2 danh sach. |"<<endl; cout<<" | 2 : Sap xep list 1 va list 2. |"<<endl; cout<<" | 3 : Xuat danh sach da nhap. |"<<endl; cout<<" "<<endl; [...]... AddTail(l3,Add); p1=p1->pNext; } else { Add=GetNode(p2->data); AddTail(l3,Add); p2=p2->pNext; } } } } Bài 3: cho danh sách sinh viên mỗi sinh viên gồm các thông tin: MSSV, họ tên, đ 1: Nhập danh sách sinh viên 2: Xuất danh sách sinh viên 3: Xoá 1 sinh viên với MSSV khỏi danh sách 4: Sắp xếp danh sách tăng dần theo điểm trung bình 5: Liệt kê các sinh viên có điểm trung bình >= 5 6: Đếm số lượng sinh viên... nhap" . vao dau danh sach void addTail(LIST &l, NODE* new_ele);//ham them node moi vao cuoi danh sach void input(LIST &l); // ham nhap danh sach void output(LIST l);// ham xuat danh sach long. &l3); #endif file main.cpp Code: #include "noidanhsachtang.h" int MeNu() { int c; cout<<endl<<endl<<endl<<" DANH SACH LIEN KET& quot;<<endl; cout<<". info;//kieu so nguyen cho danh sach } NODE; typedef struct list/ /danh sach chua pTail va pHead { NODE* pTail; NODE* pHead; }LIST; void init(LIST &l);//ham gan cho danh sach la NULL NODE* getnode(int

Ngày đăng: 23/05/2014, 13:05

Từ khóa liên quan

Mục lục

  • [Tổng hợp ]Giải bài tập về Danh Sách Liên kết

  • Thành viên dưới đây đã cám ơn bài viết này của: jony_vu

Tài liệu cùng người dùng

Tài liệu liên quan