You are on page 1of 66

1 1. sau: Sp xp mng tng dn theo phng php chn trc tip.

Hy tm phn t d (nhp t bn phm) c trong mng c sp xp theo phng php tm kim nh phn. Tm phn t ln nht trong mng (theo phng php quy). 2. Chng trnh hon chnh: //=================================================== #include <stdio.h> //=================================================== // Nhp mng void Input(int a[], int n) { int i; for(i = 1; i <= n; i++) { printf("a[%d] = ", i); scanf("%d", &a[i]); } } //===================================================// Xut mng void Output(int a[], int n) { int i; for(i = 1; i <= n; i++) { printf("\t\na[%d] = %d", i, a[i]); } } //=================================================== //a. Sp xp mng tng dn theo phng php chn trc tip void Sort(int a[], int n) { int i, j, min, temp; for(i = 1; i <= n - 1; i++) { min = i; for(j = i + 1; j <= n; j++) Bi ton: Cho mt mng gm n phn t cha cc s nguyn. Vit chng trnh thc hin cc cng vic

if (a[j] < a[i]) min = j; if (min != i) { temp = a[i]; a[i] = a[min]; a[min] = temp; } } } //=================================================== //b. Tm kim nh phn int Search(int a[], int n, int x) { int i, j, p; i = 1; j = n; while (i <= j) { p = (i + j) / 2; if (x < a[p]) j = p - 1; else if (x > a[p]) i = p + 1; else return p; } return 0; // Khng tm thy phn t x trong mng a[] } //=================================================== //c. Tm phn t ln nht trong mng theo phng php quy int Max(int a[], int n) { if (n == 1) return a[1]; return a[n] > Max(a, n - 1) ? a[n] : Max(a, n - 1); } //===================================================

int main() { const int Nmax = 1000; int a[Nmax], n, x, t, c = -1; printf("\t\t\tMENU"); printf("\n\t1. Nhap mang moi"); printf("\n\t2. Sap xep mang"); printf("\n\t3. Tim kiem phan tu trong mang"); printf("\n\t4. Phan tu lon nhat mang"); printf("\n\t0. Thoat"); while (c != 0) { printf("\n\t\tBan chon: "); scanf("%d", &c); switch (c) { case 1: printf("Nhap n: "); scanf("%d", &n); Input(a, n); break; case 2: printf("Mang truoc khi sap xep la:"); Output(a, n); Sort(a, n); printf("\nMang sau khi sap xep la:"); Output(a, n); break; case 3: Sort(a, n); printf("Tim phan tu: "); scanf("%d", &x); t = Search(a, n, x); if (t == 0) printf("Khong tim thay phan tu %d.", x); else printf("Tim thay phan tu %d o vi tri dau tien la %d.", x, t); break; case 4: printf("Phan tu lon nhat mang la: %d", Max(a, n)); } } }

I. bi2: Cho mt mng gm n phn t cha cc s nguyn. Vit chng tnh thc hin cc cng vic sau: a. Sp xp mng gim dn theo phng php ni bt. b. Hy tm phn t x (nhp t bn phm) c trong mng theo phng php tm tuyn tnh. c. Tm phn t nh nht trong mng. (theo phng php khng qui)

Ci t chng trinh : #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> #define MAX 100 void CreateArray(int A[], int *n); void PrintArray(int A[], int n); void BubbleSort(int A[], int n); int LinearSearch(int A[], int n, int x); int FindMin(int A[], int n); void main() { int A[MAX], n, x, choice; clrscr(); CreateArray(A, &n); clrscr(); printf("Mang ban dau:\n"); PrintArray(A, n); printf("\n"); printf("1. Sap xep mang theo thu tu giam dan\n"); printf("2. Tim phan tu bat ky duoc nhap tu ban phim\n"); printf("3. Tim gia tri nho nhat trong mang"); printf("\n"); printf("Chon: "); scanf("%d", &choice); printf("\n"); switch(choice) { case 1: BubbleSort(A, n); printf("Mang sau khi sap xep giam dan:\n"); PrintArray(A, n); break; case 2: printf("Nhap mot so nguyen bat ky: "); scanf("%d", &x); fflush(stdin); if(LinearSearch(A, n, x)>-1) printf("%d nam o vi tri thu %d trong mang\n", x, LinearSearch(A, n, x)+1); else printf("%d khong nam trong mang\n", x); break; case 3: printf("Gia tri nho nhat tren mang la: %d\n", FindMin(A, n)); } getch(); }

// Tao mang gom n phan tu nguyen co gia tri ngau nhien tu [-100, 100] void CreateArray(int A[], int *n) { int i; do { clrscr(); printf("Nhap so phan tu cua mang (<%d): ", MAX); scanf("%d", n); fflush(stdin); } while(*n>MAX); srand(time(0));// Thiet dat diem bat dau (seed) cho rand for(i=0; i<*n; i++) A[i]=rand()%201-100;// Sinh so ngau nhien tu [-100, 100] } // In mang void PrintArray(int A[], int n) { int i; for(i=0; i<n; i++) printf("%d ", A[i]); printf("\n"); } // Sap xep giam dan bang phuong phap Noi bot void BubbleSort(int A[], int n) { int i, j, temp; for(i=1; i<n; i++) for(j=n-1; j>=i; j--) { if (A[j-1]<A[j]) { temp=A[j-1]; A[j-1]=A[j]; A[j]=temp; } } } // Tim kiem bang phuong phap tuyen tinh int LinearSearch(int A[], int n, int x) { int i=0; while(i<n) { if(x==A[i]) return i; i++; } return -1; } // Tim gia tri nho nhat bang phuong phap khong de qui int FindMin(int A[], int n) { int i, pos; pos=0;// Gia su gia tri nho nhat nam o phan tu dau tien for(i=1; i<n; i++)

{ if(A[pos]>A[i]) pos=i; } return A[pos]; } I. bi:3 Cho mt danh sch lin kt n gm nhiu, mi phn t gm c vng infor gm cc s nguyn v vng lin kt cha a ch ca phn t k tip. Vit chng trnh thc hin cc cng vic sau: a. m s phn t dng ca danh sch lin kt. b. Tch DSLK lm hai, mt danh sch cha cc s chn v mt danh sch cha cc s l. (Khng ph hy DSLK cho) c. o ngc danh sch lin kt cho. II. Ci t chng trinh : #include <conio.h> #include <stdio.h> struct element { int nb; element *next; element *next1; element *next2; }; typedef element *list; list f,f1,f2; int n; void get(list &f); void insert(list &f,int x); void count(list f); void tach_ds(list f,list &f1,list &f2); void dao_ds(list &f); void lietke_ds(list f); void lietke_dsc(list f); void lietke_dsl(list f); //=================================// main() { int cv; char c; f=NULL; printf("\n MENU "); printf("\n 1.Nhap danh sach lien ket"); printf("\n 2.liet ke danh sach"); printf("\n 3.Dem phan tu duong"); printf("\n 4.Tach danh sach lien ket"); printf("\n 5.Dao nguoc danh sach lien ket"); printf("\n 0.Thoat khoi chuong trinh"); do { printf("\nNhap thu tu cong viec "); scanf("%d",&cv); switch(cv) { case 1: get(f); break;

case 2: printf("\nDanh sach duoc liet ke "); lietke_ds(f); break; case 3: count(f);; break; case 4: tach_ds(f,f1,f2); printf("\nPhan tu chan cua danh sach "); lietke_dsc(f1); printf("\n\nPhan tu le cua danh sach "); lietke_dsl(f2); break; case 5: dao_ds(f); printf("\nDanh sach dao nguoc "); lietke_ds(f); break; } printf("\n"); } while(cv!=0); getch(); } //=================================// void get(list &f) { int x,i; f=NULL; printf ("nhap so phan tu ");scanf("%d",&n); for (i=0;i<n;i++) { printf("nhap phan tu thu %d = ",i+1); scanf("%d",&x); insert(f,x); } } void insert(list &f, int x) { list p,t; p=new element; (*p).nb=x; (*p).next=NULL; if (f==NULL) f=p; else { t=f; while ((*t).next!=NULL) t=(*t).next; (*t).next=p; } } void count(list f) { list p1; p1=f;int c=0; while (p1!=NULL) { if((*p1).nb>0) c++; p1=(*p1).next; } printf("\nSo phan tu duong trong danh sach la = %d",c);

} void tach_ds(list f,list &f1,list &f2) { list p1; p1=f;int c; f1=NULL; f2=NULL; while (p1!=NULL) { if (((*p1).nb)%2==0){(*p1).next1=f1;f1=p1;} else{(*p1).next2=f2;f2=p1;} p1=(*p1).next; } } void dao_ds(list &f) { if(f==NULL) return; list after=NULL; list before=NULL; while(f) { after=f; f=(*f).next; (*after).next=before; before=after; } f=after; } void lietke_ds(list f) { list p; p=f; while (p!=NULL) { printf (" %d ",(*p).nb); p=(*p).next; } } void lietke_dsc(list f) { list p; p=f; while (p!=NULL) { printf (" %5d ",(*p).nb); p=(*p).next1; } } void lietke_dsl(list f) { list p; p=f; while (p!=NULL) { printf (" %5d ",(*p).nb); p=(*p).next2;

} }

s 4: Cho mt danh sch lin kt n gm nhiu phn t, mi phn t gm c vng infor l s nguyn v vng lin kt cha a ch ca phn t k tip. Vit chng trnh thc hin cc cng vic sau: a. m cc s nguyn t trong danh sch lin kt. b. Tnh tng cc phn t m trong danh sch lin kt. c. Tch danh sch lin kt lm hai, mt danh sch cha cc s chn v mt danh sch cha cc s l (Khng ph hy danh sch lin kt cho).
CHNG TRNH: #include<stdio.h> #include<conio.h> #include<windows.h> //----------------------------------------------typedef int infor; struct element { infor info; element *next; }; typedef element *List; List F, C, L; //----------------------------------------------void printfMenu(); void taoDS(List &F); void demNgto(List &F); void tongAm(List &F); void tachDS(List &F, List &C, List &L); void hienthi(List &F); //----------------------------------------------main(){ int cv, tmp = 1; do{ system("cls"); printfMenu(); if(tmp) printf("\nNhap cong viec: "); else printf("Chi chon cong viec tu 0->5. Hay nhap lai: "); scanf("%d", &cv); switch(cv){ case 0: break; case 1: taoDS(F); printf("Cac phan tu trong DSLK ban vua nhap: \n"); hienthi(F); getch(); break; case 2: demNgto(F); break; case 3: tongAm(F); break; case 4: tachDS(F, C, L); break;

case 5: printf("Liet ke cac phan tu trong DSLK: \n"); hienthi(F); getch(); break; default: break; } } while(cv); } //----------------------------------------------void printfMenu(){ printf("1. Nhap danh sach lien ket."); printf("\n2. Dem cac so nguyen to trong DSLK."); printf("\n3. Tinh tong cac phan tu am trong DSLK."); printf("\n4. Tach DSLK lam hai, mot chua so chan, mot chua so le(ko pha huy DS da cho)."); printf("\n5. Liet ke cac phan tu trong danh sach."); printf("\n0. Thoat chuong trinh."); } //----------------------------------------------void taoDS(List &F){ List p; int n, i; F = NULL; do{ printf("Nhap so phan tu cua DSLK: "); scanf("%d", &n); }while(!n); for(i=0;i<n;i++){ p = new element; printf("Nhap phan tu thu %d :",i+1); scanf("%d",&(*p).info); (*p).next = F; F = p; } } //----------------------------------------------int ngto(int x){ if(x<=1) return 0; if(x==2||x==3) return 1; for(int i=2;i<x-1;i++) if(x%i==0) return 0; return 1; } //----------------------------------------------void demNgto(List &F){ List p; p = F; int dem = 0; while(p!=NULL){ if(ngto((*p).info)) dem++; p = (*p).next; } if(dem){ printf("Co %d so nguyen to trong DSLK.\n", dem); printf("Cac so nguyen to trong DSLK: \n"); p = F; while(p!=NULL){ if(ngto((*p).info)) printf("%5d",(*p).info); p = (*p).next;

10

} }else printf("Trong DSLK khong co so nguyen to."); getch(); } //----------------------------------------------void tongAm(List &F){ int tong = 0; List p; p = F; while(p!=NULL){ if((*p).info<0) tong += (*p).info; p = (*p).next; } if(tong) printf("Tong cac so am trong DSLK: %d", tong); else printf("Trong DSLK khong co so am."); getch(); } //----------------------------------------------void tachDS(List &F, List &C, List &L){ List p, c, l; C = L = NULL; p = F; while(p!=NULL){ if((*p).info%2){ l = new element; (*l).info = (*p).info; (*l).next = L; L = l; }else{ c = new element; (*c).info = (*p).info; (*c).next = C; C = c; } p = (*p).next; } printf("DSLK chua cac so chan: \n"); hienthi(C); printf("\nDSLK chua cac so le:\n"); hienthi(L); getch(); } //----------------------------------------------void hienthi(List &F){ List p; p = F; while(p!=NULL){ printf("%5d",(*p).info); p = (*p).next; } } //---------------------------------------------- 5:

11

Cho mt danh sch lin kt n gm nhiu phn t, mi phn t gm c vng infor l s nguyn v vng lin kt cha a ch ca phn t k tip. Vit chng trnh thc hin cng vic sau: a. m s phn t m ca danh sch lin kt. b. Tch DSLK lm hai, mt danh sch cha cc s dng v mt danh sch cha cc s m (khng ph hy DSLK cho). c. Sp xp danh sch tng dn theo phng php chn trc tip. TON B CHNG TRNH: #include <stdio.h> struct element { int infor; element *next; }; typedef element *list; list f; void Create(list &f) { f = NULL; } void NhapDS(list &f) { int n, i; list p = new element; f = p; printf("Nhap n: "); scanf("%d", &n); for(i = 1; i <= n; i++) { printf("\nphan tu thu %d la: ", i); p->next = new element; p = p->next; scanf("%d", &p->infor); } f = f->next; p->next = NULL; } void DuyetDS(list f) { list p; p = f; while (p != NULL) { printf("%d\t", p->infor); p = p->next; } } int DemAm(list f) { list p = f;

12

int c = 0; while (p != NULL) { if (p->infor < 0) c++; p = p->next; } return c; } void TachDS(list f) { int i; list p = f, fd, fa, pd, pa, td, ta; pa = new element; pd = new element; fa = pa; fd = pd; while (p != NULL) { if (p->infor < 0) { ta = new element; ta->infor = p->infor; pa->next = ta; pa = ta; } if (p->infor > 0) { td = new element; td->infor = p->infor; pd->next = td; pd = td; } p = p->next; } pa->next = NULL; pd->next = NULL; fd = fd->next; fa = fa->next; printf("\nDanh sach duong la: "); DuyetDS(fd); printf("\nDanh sach am la: "); DuyetDS(fa); Printf(\ndanh sach ban goc la:); DuyetDS(f); } void SapXep(list f) { int tg; list p,q,min; p=f; while (p!=NULL) { min=p; q=p->next;

13

while(q!=NULL) { if(q->infor<min->infor) min=q; q=q->next; } { tg=min->infor; min->infor=p->infor; p->infor=tg; } p=p->next; } } int main() { int n, i, c = -1; list p = f; printf("\t\tTHUC HANH CAU TRUC DU LIEU"); printf("\t\n1. Nhap danh sach lien ket"); printf("\t\n2. Duyet danh sach"); printf("\t\n3. Dem so phan tu am trong danh sach"); printf("\t\n4. Tach DSLK"); printf("\t\n5. Sap xep danh sach tang dan"); printf("\t\n0. Thoat chuong trinh"); while (c) { printf("\t\n\n Lua chon: "); scanf("%d", &c); switch (c) { case 1: NhapDS(f); break; case 2: DuyetDS(f); break; case 3: printf("\nSo phan tu am trong danh sach la: %d", DemAm(f)); break; case 4: TachDS(f); break; case 5: printf("\nDanh sach sap xep tang dan:\n"); SapXep(f); DuyetDS(f); } } } I. bi:6 Cho mt danh sch lin kt n, gm nhiu phn t, mi phn t c mt vng info l s nguyn v vng lin kt cha a ch ca phn t k tip. Vit chng trnh thc hin cc cng vic sau: a. Tnh tch cc phn t dng trong danh sch lin kt.

14

b. Tch DSLK lm hai, mt danh sch cha cc s dng v mt danh sch cha cc s m (khng ph hu DSLK cho). c. Sp xp danh sch gim dn theo phng php ni bt. III. Chng trnh hon chnh. //Thuc hanh Cau truc du lieu - Khoa 09 //De so 6 #include <stdio.h> #include <conio.h> #include <stdlib.h> //----------------typedef int infor; struct element { infor a; element *next; }; typedef element *List; List F,AM,DG; infor x; //----------------void Menu(); void Create(List &F); void InsertFirst(List &F,infor x); void Display(List F); void TichDuong(List F); void SplitList(List F,infor a,List &T,List &L); List BubbleSort(List &F); //----------------main() { int cv=0; do{ Menu(); printf("\nAn STT cong viec can thuc hien: "); scanf("%d",&cv); switch(cv) { case 0:exit(0); case 1:Create(F);break; case 2:printf("\nDanh sach danhap:");Display(F);break; case 3:TichDuong(F);break; case 4:SplitList(F,x,AM,DG); printf("\nDanh sach duong:"); Display(DG); printf("\nDanh sach am:"); Display(AM); printf("\nDanh sach ban dau:"); Display(F); break; case 5:printf("\nDanh sach sau khi da sap xep:\n\t"); BubbleSort(F); Display(F); break; default:printf("\nSTT khong hop le! Moi nhap lai!");

15

getch(); break; } }while(cv!=0); getch(); } //----------------void Menu() { printf("\n=============================================="); printf("\n1.Nhap danh sach lien ket"); printf("\n2.Liet ke danh sach da nhap"); printf("\n3.Tich cac phan tu duong trong danh sach"); printf("\n4.Tach danh sach thanh 2 danh sach am va duong"); printf("\n5.Sap xep danh sach giam dan theo phuong phap noi bot"); printf("\n0.Ket thuc"); printf("\n=============================================="); } //----------------void Create(List &F) { int i,n; F=NULL; printf("\nNhap so phan tu cua danh sach: "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nPhan tu thu %d: ",i); scanf("%d",&x); InsertFirst(F,x); } } //----------------void InsertFirst(List &F,infor x) { List p; p=new element; (*p).a=x; (*p).next=F; F=p; } //----------------void Display(List F) { List p; p=F; if (p==NULL) printf("rong!"); while(p!=NULL) { printf("%4d",(*p).a); p=(*p).next; } } //----------------void TichDuong(List F) {

16

int N=1; List p; p=F; if (p==NULL) N=0; while(p!=NULL) { if((*p).a>0) N*=(*p).a; p=(*p).next; } printf("\nTich cac phan tu duong trong danh sach: %d",N); } //----------------void SplitList(List F,infor a,List &AM,List &DG) { List p; p=F; while(p!=NULL) { if((*p).a>0) InsertFirst(DG,(*p).a); if((*p).a<0) InsertFirst(AM,(*p).a); p=(*p).next; } } //----------------List BubbleSort(List &F) { List i,j; int tam; for(i=F;(*i).next!=NULL;i=(*i).next) for(j=(*i).next;j!=NULL;j=(*j).next) if((*i).a<(*j).a) { tam=(*i).a; (*i).a=(*j).a; (*j).a=tam; } } bi:7 Cho mt danh sch lin kt n gm nhiu phn t, mi phn t gm c vng info l mt k t A..Z v vng lin kt cha a ch ca phn t k tip. Vit chng trnh thc hin cc cng vic sau: a. m s k t khng trng nhau ca DSLK. b. Sp xp danh sch tng dn theo phng php ni bt c. B sung vo danh sch sp xp sao cho danh sch s cha y k t t A n Z #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> typedef char infor;

17

struct element { infor data; element *next; }; typedef element *list; list F; //Ham chen mot phan tu vao danh sach void insert(list &F, infor x) { list p; p=new element; p->data=x; p->next=F; F=p; } //Nhap danh sach tu ban phim void typing(list &F) { infor x; printf("Nhap cac ki tu A..Z, ket thuc bang phim khong(0)\n"); do { scanf("%c", &x); if (x>='A' && x<= 'Z') insert(F, x); } while (x!='0'); } //nhap ngau nhien void random(list &F) { int n; infor x; printf("Chon so ki tu muon nhap ngau nhien: "); scanf("%d", &n); srand(time(NULL));

18

for (int i=1;i<=n;i++) { x = 65 + rand()%26; insert (F, x); } } //xuat danh sach void display(list F) { list p; p=F; while (p) { printf("%2c", p->data ); p=p->next; } printf("\n"); } //Tao danh sach lien ket void create(list &F) { int i; F=NULL; printf("KHOI TAO DANH SACH MOI\n\n"); printf("1. Nhap tu ban phim\n"); printf("2. Nhap ngau nhien\n"); printf(".......\n"); printf("Chon thao tac 1..2 de tao danh sach: "); do { scanf("%d", &i); } while (i<1 || i>3); system("cls"); switch(i) { case 1: typing(F); break;

19

case 2: random(F); }

break;

printf("\nDanh sach vua khoi tao la:\n"); display(F); } //tim kiem list search(list F, infor x) { list p; p=F; while (p && (p->data != x)) p=p->next; return p; } //dem so ki tu khong trung nhau int count(list F) { list p, q; int n=0; p=F; q=NULL; infor x; while (p!=NULL) { x=p->data; if (search(q, x)==NULL) { insert(q, x); n++; } p=p->next; } return n; } //sap xep tang dan void sort(list &F) {

20

list p, q, g; infor x; g=F; while (g->next) { p=F; while (p->next) { q=p->next; if (q->data < p->data) { x=q->data; q->data=p->data; p->data=x; } p=p->next; } g=g->next; } } //Chen cac ki tu con thieu void complete(list &F) { list p=F, q; infor x; sort(F); for (x=p->data; x>64; x--) insert(F, x); while(p->next) { q=p->next; for (x=(p->data)+1;x<q->data;x++) { insert(q, x); p->next=q; } p=q; }

21

q=NULL; for(x='Z'; x>p->data; x--) insert(q, x); p->next=q; } //====================================== main() { int i; while(1) { printf("\n*******************************************\n"); printf("1. Tao moi danh sach\n"); printf("2. Hien thi danh sach\n"); printf("3. Dem so ki tu khong trung nhau\n"); printf("4. Sap xep danh sach tang dan\n"); printf("5. Them ki tu de hoan thanh bang chu cai\n"); printf("0. thoat\n"); printf("\n*******************************************\n\n"); printf("chon thao tac 0..5 de thuc hien: "); do { scanf("%d", &i); } while (i<0 || i>5); system("cls"); switch(i) { case 0: return 1; case 1: create(F); break; case 2: printf("Danh sach cua ban la:\n\n"); display(F); break; case 3: printf("\nso ki tu khong trung nhau trong danh sach la: %d", count(F)); break; case 4: sort(F); printf("Danh sach sau khi sap xep: \n\n"); display(F);

22

break; case 5: complete(F); printf("Da hoan thanh, xem lai danh sach: \n\n"); display(F); break; } // printf("\n\nNhan phim bat ky de tiep tuc\n"); getch(); system("cls"); } } BI 8: Cho mt danh sch lin kt n gm nhiu phn t, mi phn t gm c vng kt gm 1 k t (A..Z) v vng lin kt cha a ch ca phn k tip. Vit chng trnh thc hin cc cng vic sau: a. Loi khi danh sch cc phn t trng nhau b. To mt danh sch lin kt n mi cha ccc k t khng c trong danh sch cho. c. B sung vo danh sch sp xp ny sao cho c y cc k t t A n Z. II. Ton b chng trnh: #include <conio.h> #include <stdio.h> #include <string.h> typedef char infor; struct element { infor kt; element *next; }; typedef element *List; List F; int n; void khoitao(List &F) { F=NULL; } void hienthi(List F) { List p; p=F; printf("\n"); while (p != NULL) { printf("%3c", (*p).kt ); p=(*p).next; } printf("\n\n"); }

23

void themfirst(List &F, infor x) { List p; p=new element; (*p).kt=x; (*p).next=F; F=p; } void themsort(List &F, infor x) { List p, before, after; p=new element; (*p).kt=x; after=F; while ( (after!=NULL) && ( (*after).kt<x )) { before=after; after=(*after).next; }; (*p).next=after; if (F==after) F=p; else (*before).next=p; } void nhapmoi(List &F) { infor x; int i; printf ("Nhap so phan tu: "); scanf("%d",&n); for (i=1;i<=n;i++) { printf("Nhap phan tu thu %d: ",i); fflush(stdin); scanf("%c",&x); themsort(F,x); } }

// 1 // 2' // 2

void xoa(List &F, List t) { List before, after; after=F; while ( ( after!=NULL) && (after!=t) ) { before = after; after=(*after).next; } if (after!=NULL) { if (F==t) F=(*t).next; else (*before).next=(*t).next; delete t; } }

// 1' // 1

24

main() { List F,hoa,k,p; infor x,b,chu[30] ={'A','B','C','D','E','F','G','H','I','K','L','M','N','O' ,'P','Q','R','S','T','U','V','W','X','Y','Z'}; int cv,n,i; do{//1 printf("\nNHAP VAO SO THU TU CUA CONG VIEC BAN MUON THUC HIEN: "); printf("\n----------------------------------------------------"); printf("\n1. Nhap moi ds."); printf("\n2. Liet ke ds."); printf("\n3. Xoa phan tu trung nhau."); printf("\n4. Tao ds moi ko chua phan tu cua cac ds cu."); printf("\n5. Bo sung vao ds moi du ki tu tu A->Z."); printf("\n0. Thoat khoi chuong trinh.\n\n"); scanf("%d",&cv); switch(cv) {case 1: khoitao(F); nhapmoi(F); break;//2 case 2: hienthi(F); break; case 3: k=p=F; while(p!=NULL) {x=(*p).kt; k=(*p).next; {while ( (k!=NULL) && ((*k).kt!=x) ) k= (*k).next; } if(k!=NULL)xoa(F,k ); else p=(*p).next; }delete p; delete k; hienthi(F); break; case 4: khoitao(hoa); for (i=26;i>=0;i--) {b=chu[i]; themfirst(hoa,b); } p=F; k=hoa; while(p!=NULL) {x=(*p).kt; {while ( (k!=NULL) && ((*k).kt!=x) ) k= (*k).next;

25

} if(k!=NULL)xoa(hoa,k ); k=hoa; p=(*p).next; } hienthi(hoa); delete p; break; case 5: while(F!=NULL) {b=(*F).kt; themsort(hoa,b); F=(*F).next; } hienthi(hoa); break; default : break; }//2 }while(cv!=0);//1 getch(); }

Bi 9: Cho mt chui vn bn gm cc chui k t ,khong trng v du chm. Chui gm mt cu kt thc bng du chm. Hy xy dng danh sch lin kt cha vn bn trn v vit chng trnh thc hin cc cng vic sau : a. m s t trong cu. b. Tm kim v thay th mt t bng mt t khc. c. Cho bit cc t c chiu di n k t cho trc. Bi Lm A.Chng Trnh: #include<conio.h> #include<stdio.h> #include<string.h> #include<malloc.h> typedef struct vanBan { char tu[1000]; char khoangtrang[1]; struct vanBan *next; }vb; vb *temp,*L,*last; char vanBanNhap[1000]; void kiemtracau(); void menu(); void nhapVanBan() { int i,j; printf("\n Nhap van ban muon xu ly: "); fflush(stdin); gets(vanBanNhap);

26

kiemtracau(); } void kiemtracau() { int i=0,j=0,k=0,dem=0,l=0; char st[1000]; i=strlen(vanBanNhap); if(vanBanNhap[i-1]!='.') { printf("\n Cau ban nhap chua dung cu phap. Nhap lai"); nhapVanBan(); } else { for(j=0;j<strlen(vanBanNhap);j++) if(vanBanNhap[j]=='.') dem++; if(dem!=1) { printf("\n Ban da nhap nhieu hon 2 cau. Nhap lai"); nhapVanBan(); } else { printf("\n Van ban vua nhap: "); for(k=0;k<i;) if((vanBanNhap[k]==' ')&&(vanBanNhap[k+1]==' ')) { for(j=k;j<i-1;j++) { vanBanNhap[j]=vanBanNhap[j+1];} i--;} else { st[l]=vanBanNhap[k]; l++; k++;} fflush(stdin); strcpy(vanBanNhap,st); printf("%s",vanBanNhap); } } } void luutu() { char tam[1000]; temp=last=L=NULL; int i=0,j=0; for(i=0;i<strlen(vanBanNhap);i++) { if((i-1<0||vanBanNhap[i-1]==' ')&&vanBanNhap[i]!=' '&&vanBanNhap[i]!='.') { while(vanBanNhap[i]!=' '&&vanBanNhap[i]!='.'&&i<strlen(vanBanNhap)) { tam[j]=vanBanNhap[i]; i++;j++;} tam[j]='\0'; j=0; fflush(stdin); temp=(vb*)malloc(sizeof(vb)); strcpy((*temp).tu,tam); strcpy(temp->khoangtrang," "); if(L==NULL) L=temp; else (*last).next=temp; last=temp; last->next=NULL; }} }

27

void xuatVanBan() { temp=L; puts("\n Van ban vua nhap: "); while(temp!=NULL) { printf("%s%s",temp->tu,temp->khoangtrang); temp=temp->next; } puts("."); } void sotu() { int i=0; temp=L; while(temp!=NULL) { i++; temp=temp->next; } printf("so tu la %d",i); } void timkiem() { int i=0; char tuCanTim[1000]; char tuThayThe[1000]; puts("\n Nhap tu can tim kiem: "); fflush(stdin); gets(tuCanTim); temp=L; puts("\n Ket qua tim kiem"); while(temp!=NULL) { if(strcmp((*temp).tu,tuCanTim)==0) i++; temp=temp->next; } if(i==0) printf("\n Khong tim thay tu"); else { printf("\nTim thay %d ket qua giong tu vua nhap",i); printf("\nNhap tu muon thay the: "); fflush(stdin); gets(tuThayThe); temp=L; while(temp!=NULL) { if(strcmp((*temp).tu,tuCanTim)==0) { strcpy((*temp).tu,tuThayThe);} temp=temp->next; } printf("\nSau khi thay the ta duoc: "); xuatVanBan(); } } void xuatn(int n) { int i=1; temp=L; while(temp!=NULL) { if(strlen((*temp).tu)==n) printf("\n%s",(*temp).tu); temp=temp->next;

28

} } main() { int i,n; do { printf("\n"); printf("\n-------------Menu-------------"); printf("\n *1.Nhap 1 cau ket thuc bang dau cham "); printf("\n *2.So tu trong chuoi"); printf("\n------------------------------"); printf("\n *3.Tim va thay the mot tu trong chuoi neu co"); printf("\n *4.Tim tu co do dai n"); printf("\n------------------------------"); printf("\n *5.Sinh vien thuc hien"); printf("\n *0.Thoat"); printf("\n------------------------------") ; printf("\n Chon cong viec ban can lam : "); scanf("%d",&i); switch(i) { case 1: nhapVanBan(); luutu();break; case 2: sotu();break; case 3: timkiem();break; case 4: printf("nhap do dai n"); scanf("%d",&n); xuatn(n); break; case 5: printf("Nhom 9"); printf("\nSv1: Le Quang Dao 09T2"); printf("\nSv2: Phan Ba An 09T2 "); break; } }while(i!=0); } Bi 10: Cho mt chui vn bn gm cc k t ch, khong trng v du chm. Chui gm c mt cu kt thc bng du chm. Hy xy dng danh sch lin kt lu tr vn bn trn v vit chng trnh thc hin cc cng vic sau: a. Cho bit t di nht trong cu v tnh s k t ca t di nht. b. Tm kim v thay th mt t bng mt t khc. c. Cho bit cc t bt u bng k t H. I. Code chng trnh chnh. #include<conio.h> #include<stdio.h> #include<string.h> #include<malloc.h> typedef struct vanBan { char tu[1000]; char khoangtrang[1];

29

struct vanBan *next; }vb; vb *temp,*L,*last; char vanBanNhap[1000]; void kiemtracau(); void menu(); void nhapVanBan() { int i,j; printf("\n Nhap van ban muon xu ly: "); fflush(stdin); gets(vanBanNhap); kiemtracau(); } void kiemtracau() { int i=0,j=0,k=0,dem=0,l=0; char st[1000]; i=strlen(vanBanNhap); if(vanBanNhap[i-1]!='.') { printf("\n Cau ban nhap chua dung cu phap. Nhap lai"); nhapVanBan(); } else { for(j=0;j<strlen(vanBanNhap);j++) if(vanBanNhap[j]=='.') dem++; if(dem!=1) { printf("\n Ban da nhap nhieu hon 1 cau. Nhap lai"); nhapVanBan(); } else { printf("\n Van ban vua nhap: "); for(k=0;k<i;) if((vanBanNhap[k]==' ')&&(vanBanNhap[k+1]==' ')) { for(j=k;j<i-1;j++) { vanBanNhap[j]=vanBanNhap[j+1];} i--;} else { st[l]=vanBanNhap[k]; l++; k++;} fflush(stdin); strcpy(vanBanNhap,st); printf("%s",vanBanNhap); } } } void luutu() { char tam[1000]; temp=last=L=NULL; int i=0,j=0; for(i=0;i<strlen(vanBanNhap);i++) { if((i-1<0||vanBanNhap[i-1]==' ')&&vanBanNhap[i]!=' '&&vanBanNhap[i]!='.') { while(vanBanNhap[i]!=' '&&vanBanNhap[i]!='.'&&i<strlen(vanBanNhap)) { tam[j]=vanBanNhap[i]; i++;j++;} tam[j]='\0'; j=0;

30

fflush(stdin); temp=(vb*)malloc(sizeof(vb)); strcpy((*temp).tu,tam); strcpy(temp->khoangtrang," "); if(L==NULL) L=temp; else (*last).next=temp; last=temp; last->next=NULL; }} } void xuatVanBan() { temp=L; puts("\n Van ban vua nhap: "); while(temp!=NULL) { printf("%s%s",temp->tu,temp->khoangtrang); temp=temp->next; } puts("."); } void tuDaiNhat() { int i=1; temp=L; char Max[1000]; strcpy(Max,(*temp).tu); while(temp!=NULL) { if(strlen(Max)<strlen((*temp).tu)) { strcpy(Max,(*temp).tu); temp=temp->next; } else temp=temp->next; } temp=L; printf("\n Tu dai nhat trong chuoi co so ki tu la %d.Gom nhung tu sau: ",strlen(Max)); while(temp!=NULL) { if(strlen((*temp).tu)==strlen(Max)) { printf("\n*%d.%s",i,(*temp).tu); i++; } temp=temp->next; } } void timkiem() { int i=0; char tuCanTim[1000]; char tuThayThe[1000]; puts("\n Nhap tu can tim kiem: "); fflush(stdin); gets(tuCanTim); temp=L; puts("\n Ket qua tim kiem"); while(temp!=NULL) { if(strcmp((*temp).tu,tuCanTim)==0) i++; temp=temp->next; }

31

if(i==0) printf("\n Khong tim thay tu"); else { printf("\nTim thay %d ket qua giong tu vua nhap",i); printf("\nNhap tu muon thay the: "); fflush(stdin); gets(tuThayThe); temp=L; while(temp!=NULL) { if(strcmp((*temp).tu,tuCanTim)==0) { strcpy((*temp).tu,tuThayThe);} temp=temp->next; } printf("\nSau khi thay the ta duoc: "); xuatVanBan(); } } void timH() { char tam[1000]; int i=1; temp=L; while(temp!=NULL) { strcpy(tam,(*temp).tu); if(tam[0]=='H'||tam[0]=='h') {printf("\n*%d.%s",i,tam); i++; } temp=temp->next; } } void svthuchien() { printf("\nNhom sv thuc hien"); printf("\nDang Quoc Huy Lop:09T2"); printf("\nHua Van Trung Hieu Lop:09T2"); } main() { int i; do { printf("\n"); printf("\n-------------Menu-------------"); printf("\n *1.Nhap 1 cau ket thuc bang dau cham "); printf("\n *2.Tim tu dai nhat trong chuoi"); printf("\n------------------------------"); printf("\n *3.Tim va thay the mot tu trong chuoi neu co"); printf("\n *4.Tim tu bat dau bang chu 'H'"); printf("\n------------------------------"); printf("\n *5.Sinh vien thuc hien"); printf("\n *0.Thoat"); printf("\n------------------------------") ; printf("\n Chon cong viec ban can lam : "); scanf("%d",&i); switch(i) { case 1: nhapVanBan();

32

luutu();break; case 2: tuDaiNhat();break; case 3: timkiem();break; case 4: timH();break; case 5: svthuchien();break; } }while(i!=0); }

ti 11 Cho 1 chui vn bn gm cc k t ch , khong trng v du chm.Chui gm c nhiu cu cch nhau bi du chm, cc cu gm c nhiu t,cc t cch nhau bi khong trng.Hy xy dng danh sch lin kt lu tr chui vn bn trn v vit chng trnh thc hin cc cng vic sau: a. m s cu c trong vn bn b. Xut ra mn hnh cu di nht c. Cho bit cc cu c chiu di ln hn hoc bng n t cho trc 1. Chng trnh hon chnh. #include<conio.h> #include<stdio.h> #include<string.h> #include<malloc.h> typedef struct vanBan { char cau[1000]; char chamcau[1]; struct vanBan *next; } vb; vb *temp,*L,*last; char vanBanNhap[1000]; void menu() { printf("\n"); printf("\n Chuong trinh: "); printf("\n 1.Nhap mot van ban ket thuc bang dau cham"); printf("\n (cac cau cach nhau bang dau cham)"); printf("\n 2.Dem so cau cua van ban"); printf("\n 3.Tim cau dai nhat trong van ban"); printf("\n 4.Tim cau lon hon hoac bang n tu cho truoc"); printf("\n 0.Thoat"); printf("\n Chon cong viec ban can thuc thi : "); } void kiemtravanban(); void nhapVanBan() { int i,j; printf("\n Nhap van ban : "); fflush(stdin); gets(vanBanNhap);

I.

33

kiemtravanban(); } void kiemtravanban() { int i=0,j=0,k=0,dem=0,l=0; char st[1000]; i=strlen(vanBanNhap); if(vanBanNhap[i-1]!='.') { printf("\n Van ban nhap sai cu phap. Xin moi nhap lai"); nhapVanBan(); } } void luucau() { char tam[1000]; temp=last=L=NULL; int i=0,j=0; for(i=0;i<strlen(vanBanNhap);i++) { if((i-1<0||vanBanNhap[i-1]=='.')) { while(vanBanNhap[i]!='.'&&i<strlen(vanBanNhap)) { tam[j]=vanBanNhap[i]; i++;j++;} tam[j]='\0'; j=0; fflush(stdin); temp=(vb*)malloc(sizeof(vb)); strcpy((*temp).cau,tam); strcpy(temp->chamcau,"."); if(L==NULL) L=temp; else (*last).next=temp; last=temp; last->next=NULL; } } } void socau() { int i=0; temp=L; while(temp!=NULL) { i++; temp=temp->next; } printf(" So cau trong van ban la %d",i); } void cauDaiNhat() { int i=1; temp=L; char Max[1000]; strcpy(Max,(*temp).cau); while(temp!=NULL) { if(strlen(Max)<strlen((*temp).cau))

34

{ strcpy(Max,(*temp).cau); temp=temp->next; } else temp=temp->next; } temp=L; printf("\n Cau dai nhat trong chuoi gom nhung cau sau: "); while(temp!=NULL) { if(strlen((*temp).cau)==strlen(Max)) { printf("\n %d.%s",i,(*temp).cau); i++; } temp=temp->next; } } int demtu(char *s) { int i=0,dem=1; for(i=0;i<=strlen(s);i++) if(s[i]==' ') dem++; return dem; } void timcaulonhonbangntu() { int n,d,i=0; char tam[1000]; printf("nhap n ="); scanf("%d",&n); temp=L; printf("\n Cac cau co do dai lon hon hoac bang %d tu la:",n); while(temp!=NULL) { d=demtu((*temp).cau); if(d>=n) { i++; printf("\n%d.%s",i,(*temp).cau); } temp=(*temp).next; } if (i==0) printf("\n Khong co cau nao lon hon hoac bang %d tu",n); } main() { int i; do { menu(); scanf("%d",&i); switch(i) { case 1: nhapVanBan();luucau();break; case 2: socau();break;

35

case 3: cauDaiNhat();break; case 4: timcaulonhonbangntu();break; } } while(i!=0); } BI 12 Cho mt chui vn bn gm cc k t ch, khong trng v du chm . Chui gm c nhiu cu cch nhau bi du chm . Cc cu c nhiu t, cc t cch nhau bi khong trng . Hy xc nh danh sch lin kt lu tr chui vn bn trn v vit chng trnh thc hin cc cng vic sau : a) m s t c trong vn bn. b) Xut ra mn hnh cu ngn nht. c) Cho bit cc t c chiu di n k t cho trc. #include <stdio.h> #include <conio.h> #include <string.h> #include <ctype.h> typedef char infor1[1000]; struct text { infor1 c; text *next; }; typedef text *List; List F; //nhap van ban void nhapvb(List &F) { char vb[1000]; long max,j,n,d,i=0; List tam,p; p=new text; F=p; tam=p; d=0; printf("\nThuc hien viec nhap van ban (Chu y: Ket thuc van ban bang dau cham).\n"); printf("Go van ban: "); fflush(stdin); gets(vb); while (vb[i]!='\0') { if (vb[i]=='.') { strncpy((*p).c,vb,i); (*p).c[i]='\0'; strcpy((*tam).c,(*p).c); if (d==0) F=tam; p=new text; if (vb[i+1]=='\0') p=NULL; (*tam).next=p; tam=p; n=i; max=strlen(vb)-i-1; for(j=0;j<=max;j++) { if (vb[i+1]==' ') vb[j]=vb[n+2]; else vb[j]=vb[n+1]; n++; } vb[n+1]='\0';

36

i=-1; d++; } i++; } } //in ra cau ngan nhat; void ngan(List F) { List p; char x[1000]; int min; p=F; min=strlen((*F).c); while (p!=NULL) { if (strlen((*p).c)<=min) min=strlen((*p).c); p=(*p).next; } p=F; while(p!=NULL) { if(strlen((*p).c)==min) {strcpy(x,(*p).c); printf("%s.\n",x);} p=(*p).next; } } //dem so tu cua vanban int demvb(List F) { List p; int i,dem=0; p=F; while (p!=NULL) { for(i=0;i<=strlen((*p).c);i++) if(((*p).c[i]==' ')||(*p).c[i]=='\0') dem++; p=(*p).next; } return(dem); } //in ra cac tu co n ki tu void xuatnkt(List F,int y) { List p; int kt=0,k,i,j=-1; p=F; while (p!=NULL) { j=-1; for(i=0;i<(strlen((*p).c));i++) { if (((*p).c[i]==' ')||((*p).c[i+1]=='\0')) { if (i-j-1==y) { for(k=j;k<i;k++)printf("%c",(*p).c[k]); kt++;} if((*p).c[i+1]=='\0') {if (i-j==y) { for(k=j;k<=i;k++)printf("%c",(*p).c[k]); kt++; } } j=i; } }

37

p=(*p).next; } if(kt==0) printf("Khong co tu nao co chieu dai bang %d.",y); } ///////////////////////////////////////////////////////////////////////////// main() { F=NULL; int n,tt; // thu tu do { printf("\nDanh sach cong viec:\n\n"); printf("\t 1.Khoi tao van ban\n"); printf("\t 2.Dem so tu co trong van ban\n"); printf("\t 3.Xuat ra cau ngan nhat cua van ban\n"); printf("\t 4.Nhap n va tim cau co n ky tu trong van ban\n"); printf("\t 0.Ket thuc chuong trinh\n"); printf("\nNhap so thu tu de chon cong viec ban muon thuc hien: "); scanf("%d",&tt); switch(tt) { case 1: nhapvb(F); break; case 2: printf("\nSo tu co trong doan van ban: %d\n",demvb(F)); break; case 3: ngan(F); break; case 4: { printf("\nNhap chieu dai cua tu can in ra: "); scanf("%d",&n); xuatnkt(F,n); } break; }; }while (tt!=0); getch(); } 13: #include<stdio.h> #include<conio.h> #include<string.h> typedef char infor1[15]; typedef char infor2[20]; typedef float infor3; struct Svien { //khai bao cac truong : infor1 masv; //ma sinh vien infor2 tensv //ten sinh vien ,ho; //Ho va ten dem cuar sinh vien infor3 dtoan //diem toan ,dly //diem ly ,dhoa //diem hoa ,dtb; //diem trung binh Svien *next; }; typedef Svien *list;

38

list P,F; int cv;//cong viec infor1 code;//ma sinh vien infor2 name,fname;//ten sinh vien infor3 t,l,h,tb;//Diem toan, ly, hoa, va diem trung binh //---KHAI BAO CAC NGUYEN MAU HAM--//--Nhap moi danh sach tu ban phim void nhap(list &F); //--in danh sach sinh vien len man hinh--void xuat(list F); //--in danh sach sinh vien co diem trung binh >=7 len man hinh void xuat2(list F); //--Cap nhap ly lich cho sinh vien them - sua- xoa void them(list &F,infor1 code,infor2 fname,infor2 name,infor3 t,infor3 l,infor3 h,infor3 tb);//them mot sinh vien vao danh sach void sua(list ct);//sua phan tu chi boi con tro ct void xoa(list &F,list t);//xoa sinh vien duoc chi boi con tro t //--Tim thong tin sinh vien qua viec nhap ma sinh vien void tim(list F,infor1 code); //---XAY DUNG CAC CHUONG TRINH CON----void nhap(list &F) { F = NULL; do { printf("\n Nhap ho va ten lot : "); gets(fname);gets(fname); printf("\n Nhap ten (bo trong de ket thuc) : "); gets(name); if(strlen(name)>0) { printf("\n Ma sinh vien : "); gets(code); printf("\n Diem toan : "); scanf("%f",&t); printf("\n Diem ly : "); scanf("%f",&l); printf("\n Diem hoa :"); scanf("%f",&h); tb = (t*3 + l*2 + h)/6; them(F,code,fname,name,t,l,h,tb); } } while(strlen(name)>0); } void xuat(list F) { list P; P = F; if(P==NULL) printf("\n Danh sach rong !"); else { printf("\n DANH SACH SINH VIEN"); printf("\n");

39

printf("\n Ma sv Ho Ten Toan Ly Hoa Diem TB"); } while(P!=NULL) { printf("\n %10s%13s%7s%10.1f%10.1f%10.1f%10.1f",(*P).masv,(*P).ho,(*P).tensv,(*P).dtoan, (*P).dly,(*P).dhoa,(*P).dtb); P= (*P).next; } printf("\n +An Enter de ve Menu chinh !"); } void them(list &F,infor1 code,infor2 fname,infor2 name,infor3 t,infor3 l,infor3 h,infor3 tb) { list P,before,after; P=new Svien; strcpy((*P).ho,fname); strcpy((*P).tensv,name); strcpy((*P).masv,code); (*P).dtoan = t; (*P).dly = l; (*P).dhoa = h; (*P).dtb = tb; after = F; while((after!=NULL)&&(strcmp((*P).tensv,(*after).tensv)>0)) { before = after; after = (*after).next; } (*P).next = after; if(F==after) F=P; else (*before).next = P; } void xoa(list &F,list t) { list after,before; after = F; while((after!=NULL)&&(after!=t)) { before = after; after = (*after).next; } if(after!=NULL) { if(F==t) F = (*t).next; else (*before).next = (*t).next; delete t; } } list timkiem(list F, infor1 code) { list P; P=F; while ( (P!=NULL) && strcmp((*P).masv,code) ) P= (*P).next; return P; } void xuat2(list F)

40

{ list P; P = F; printf("\n DANH SACH SINH VIEN CO DIEM TRUNG BINH LON HON 7"); printf("\n"); printf("\n Ma sv Ho Ten Toan Ly Hoa Diem TB"); while(P!=NULL) { if((*P).dtb>=7) { printf("\n %10s%13s%7s%10.1f%10.1f%10.1f%10.1f",(*P).masv,(*P).ho,(*P).tensv,(*P).dtoan, (*P).dly,(*P).dhoa,(*P).dtb); } P= (*P).next; } printf("\n +An Enter de ve Menu chinh !"); } void sua(list ct) { printf("\n Ho va ten lot : "); gets(fname);gets(fname); strcpy((*ct).ho,fname);//thay ho va ten lot moi printf("\n Ten : "); gets(name); //lay chuoi tu ban phim strcpy((*ct).tensv,name);//thay the chuoi cu bang chuoi moi tt: printf("\n Ma sv : "); gets(code);//lay ma so sinh vien tu ban phim P = timkiem(F,code);//tim kiem xem co bi trung ma voi sinh vien khac khong if((P!= NULL)&&(strcmp((*ct).masv,code)!=0)) //neu trung bao loi va cho nhap lai ma sinh vien { printf("\n Sinh vien nay da co trong danh sach. Vui long nhap lai !"); goto tt; } strcpy((*ct).masv,code);//thay the ma so cu bang ma so moi printf("\n Diem Toan : "); scanf("%f",&t); (*ct).dtoan = t; printf("\n Diem Ly : "); scanf("%f",&l); (*ct).dly = l; printf("\n Diem Hoa : "); scanf("%f",&h); (*ct).dhoa = h; tb = (t*3 + l*2 + h)/6; (*ct).dtb = tb; printf("\n Sua thanh cong !"); } //======== HAM MAIN()======= void main() { clrscr(); F = NULL; char k; int tc; do {

41

clrscr(); printf("\n"); printf("\n-------------Menu-------------"); printf("\n-----------MSTUDENT-----------"); printf("\n *1.Nhap moi danh sach "); printf("\n *2.Liet ke danh sach "); printf("\n------------------------------"); printf("\n Cap nhat ly lich cho sv"); printf("\n *3.Them "); printf("\n *4.Sua "); printf("\n *5.Xoa "); printf("\n------------------------------"); printf("\n *6.Tim sinh vien "); printf("\n------------------------------"); printf("\n *7.In ra nhung sv hoc luc kha "); printf("\n------------------------------"); printf("\n *8.About MStudent"); printf("\n------------------------------"); printf("\n *0.Thoat"); printf("\n------------------------------") ; printf("\n *Chon cong viec ban can lam : "); scanf("%d",&cv); switch(cv) { case 1: nhap(F);break; case 2: xuat(F); getch(); break; case 3: printf("\n Nhap ho va ten lot : "); gets(fname);gets(fname); printf("\n Nhap ten (bo trong de ket thuc) : "); gets(name); tt : printf("\n Ma so sinh vien : "); gets(code); P = timkiem(F,code); if(P!= NULL) { printf("\n Sinh vien nay da co trong danh sach. Vui long nhap lai !"); goto tt; } printf("\n Diem toan : "); scanf("%f",&t); printf("\n Diem ly : "); scanf("%f",&l); printf("\n Diem hoa : "); scanf("%f",&h); tb = (t*3 + l*2 + h)/6; them(F,code,fname,name,t,l,h,tb); printf("\n Them thanh cong !");getch();break; case 4: printf("\n Nhap ma so sinh vien cua sinh vien can sua : "); gets(code);gets(code); P = timkiem(F,code); if(P!=NULL)

42

{ printf("\n Thong tin sinh vien can sua : "); printf("\n Ma sv Ho Ten Toan Ly Hoa Diem TB"); printf("\n %10s%13s%7s%10.1f%10.1f%10.1f%10.1f",(*P).masv,(*P).ho,(*P).tensv,(*P).dtoan, (*P).dly,(*P).dhoa,(*P).dtb); printf("\n Co chac la ban muon sua thong tin ve sv nay : C/K ?"); scanf("%c",&k); if(k=='c'||k=='C') sua(P); else printf("\n Lenh sua duoc huy !"); } else printf("\n Khong tim thay sinh vien co ma so %s trong danh sach !",code);getch(); break; case 5: printf("\n Nhap ma so sinh vien cua sinh vien can xoa : "); gets(code);gets(code); P = timkiem(F,code); if(P!=NULL) { printf("\n Thong tin sinh vien can xoa : "); printf("\n Ma sv Ho Ten Toan Ly Hoa Diem TB"); printf("\n %10s%13s%7s%10.1f%10.1f%10.1f%10.1f",(*P).masv,(*P).ho,(*P).tensv,(*P).dtoan, (*P).dly,(*P).dhoa,(*P).dtb); printf("\n Co chac la ban muon xoa sinh vien nay : C/K ?"); scanf("%c",&k); if(k=='c'||k=='C') { xoa(F,P); printf("\n Xoa thanh cong !"); } else printf("\n Lenh xoa duoc huy !"); } else printf("\n Khong co sinh vien co ma sv %s trong danh sach !",code);getch(); break; case 6: printf("\n Nhap ma so sinh vien can tim : "); gets(code);gets(code); P = timkiem(F,code); if(P!=NULL) { printf("\n Thong tin sinh vien co ma sinh vien %s la : ",code); printf("\n Ma sv Ho Ten Toan Ly Hoa Diem TB"); printf("\n %10s%13s%7s%10.1f%10.1f%10.1f%10.1f",(*P).masv,(*P).ho,(*P).tensv,(*P).dtoan, (*P).dly,(*P).dhoa,(*P).dtb); printf("\n\n Cac tuy chon : 1.Sua 2.Xoa 3.Ve Menu chinh "); printf("\n Ban chon : "); scanf("%d",&tc); switch(tc) { case 1 : sua(P);break; case 2 : xoa(F,P); printf("\n Xoa thanh cong !");break; } } else printf("\n Khong tim thay sinh vien co ma sv %s trong danh sach !",code);getch(); break; case 7: xuat2(F); getch();

43

break; case 8: printf("\n # THUC HANH CAU TRUC DU LIEU #"); printf("\n # -------@@@------- #"); printf("\n # CHUONG TRINH #"); printf("\n # QUAN LY THONG TIN SINH VIEN MSTUDENT #"); printf("\n # (De 13) #"); printf("\n # Nhom 10. Sinh vien thuc hien : #"); printf("\n # 1.Ngo Van Nang - Lop 06T1 #"); printf("\n # 2.Nguyen Thanh Huyen - Lop 06T1 #"); printf("\n # 3.Nguyen Le Phu Long - Lop 06T1 #"); printf("\n # copyright@ Nhom 10 #"); printf("\n # update 14.5.2008 #"); printf("\n printf("\n +An Enter de ve Menu chinh !"); getch(); break; } } while(cv!=0); } bi 16: Qun l thng tin cn bn hng gm: tn hng, s lng, n gi.T chc cu trc d liu thch hp biu din cc thng tin trn v ci t cc chc nng: a) Cp nhp thng tin bn hng(thm, sa, xo) b) Tnh thnh tin = s lng * n gi c) Tm thng tin ca mt ln bn hng(nhp tn hng) d) In ra nhng ln bn hng vi thnh tin >= 5 000 000 II. Code chng trnh hon chnh. #include<conio.h> #include<stdio.h> #include<string.h> #include<math.h> typedef char tenhang[20]; typedef int soluong; typedef float dongia; typedef float thanhtien; struct hang { tenhang th; soluong sl; dongia dg; thanhtien tt; hang *next; }; typedef hang *DS; DS F,p; int cv; tenhang x; soluong y; dongia z; thanhtien k; void HienThi(DS F);//Dinh nghia ham duyet danh sach. Liet ke tat ca cac phan tu trong danh sach void ChenSX(DS &F,tenhang x,soluong y,dongia z,thanhtien k);//Dinh nghia ham them mot phan tu

44

//vao danh sach co sap xep theo thu tu tang dan void TaoDS(DS &F);//Dinh nghia ham tao danh sach void SuaDS(DS &p,tenhang x,soluong y,dongia z,thanhtien k);//Dinh nghia ham sua thong tin mat hang void Xoa(DS &F,DS k);//Xoa ban ghi k ra khoi danh sach DS Tim(DS F,tenhang x);//Dinh nghia ham tim kiem mot phan tu trong danh sach theo ten main() { F=NULL;p=NULL; do { printf("*****************************************\n"); printf("* 1.Nhap Danh Sach. *\n"); printf("* 2.In Danh Sach. *\n"); printf("* 3.Them Vao Danh Sach. *\n"); printf("* 4.Sua Danh Sach. *\n"); printf("* 5.Xoa Phan Tu Trong Danh Sach. *\n"); printf("* 6.Tim Phan Tu Trong Danh Sach. *\n"); printf("* 7.In Danh Sach Lan Ban Hang>=5000000. *\n"); printf("* 0.Ket Thuc. *\n"); printf("******************************************\n"); printf("\nChon Cong Viec:"); scanf("%d",&cv); switch(cv) { case 1: TaoDS(F); break; case 2: HienThi(F); break; case 3: printf("\nNhap ten hang:"); fflush(stdin); gets(x); if(strcmp(x,"")) { printf("\nNhap so luong:"); scanf("%d",&y); printf("\nNhap don gia:"); scanf("%f",&z); k=y*z; ChenSX(F,x,y,z,k); } break; case 4: printf("\nNhap ten hang can sua:"); fflush(stdin); gets(x); printf("\nNhap so luong:"); scanf("%d",&y); printf("\nNhap don gia:"); scanf("%f",&z); k=y*z; p=Tim(F,x);

45

SuaDS(p,x,y,z,k); break; case 5: printf("\nNhap ten can xoa:"); fflush(stdin); gets(x); p=Tim(F,x); if(p!=NULL) Xoa(F,p); else printf("\nKhong co mat hang nay trong danh sach.\n"); break; case 6: printf("\nNhap ten can tim:"); fflush(stdin); gets(x); p=Tim(F,x); if(p!=NULL) printf("\nTen hang: %20s \nSo luong: %d \nDon gia: %7.2f \nThanh tien: %7.2f",(*p).th, (*p).sl,(*p).dg,(*p).tt); else printf("\nKhong co mat hang nay trong danh sach.\n"); break; case 7: p=F; int ok=0; while(p!=NULL) { if((*p).tt>5000000) { printf("\nTen hang: %20s \nSo luong: %0.f \nDon gia: %0.f \nThanh tien: %0.2f",(*p).th, (*p).sl,(*p).dg,(*p).tt); ok=1; } p=(*p).next; } if(ok==0) printf("\nKhong co mat hang nao ban tren 5 000 000.\n"); break; } }while(cv!=0); return 0; } void HienThi(DS F) { DS p=F; int i= 1; while(p!=NULL) { printf("%d. Ten hang:%15s \n\tSo luong:%d \n\tDon gia: %7.2f \n\tThanh tien: %7.2f\n",i, (*p).th,(*p).sl,(*p).dg,(*p).tt); p=(*p).next; i++; } }

46

void ChenSX(DS &F,tenhang x,soluong y,dongia z,thanhtien k) { DS before,after,p; p=new hang; strcpy((*p).th,x); (*p).sl=y; (*p).dg=z; (*p).tt=k; after=F; while((after!=NULL) && (strcmp((*after).th,x)<0)) { before=after; after=(*after).next; } (*p).next=after; if(F ==after) F=p; else (*before).next=p; } void TaoDS(DS &F) { do { printf("\nNhap ten hang:"); fflush(stdin); gets(x); if(strcmp(x,"")) { printf("\nNhap so luong:"); scanf("%d",&y); printf("\nNhap don gia:"); scanf("%f",&z); k=y*z; ChenSX(F,x,y,z,k); } }while(strcmp(x,"")); } void SuaDS(DS &p,tenhang x,soluong y,dongia z,thanhtien k) { strcpy((*p).th,x); (*p).sl=y; (*p).dg=z; (*p).tt=k; } void Xoa(DS &F,DS k) { DS before,after; after=F; while((after!=NULL)&&(after!=k)) { before=after; after=(*after).next;

47

} if(after!=NULL) { if(F==k) F=(*k).next; else (*before).next=(*k).next; delete k; } HienThi(F); } DS Tim(DS F,tenhang x) { DS p=F; while((p!=NULL)&& strcmp((*p).th,x)) p=(*p).next; return p; } 17 #include<stdio.h> #include<conio.h> #include<stdlib.h> /*********************************** */ /* THUC HANH CAU TRUC DU LIEU */ /* BAI SO 17 */ /************************************/ struct element{ int infor ; element *left,*right; }; typedef element *Tree; void create(Tree &T){T=NULL;}//Khoi tao cay nhi phan tim kiem! void insert(Tree p,Tree &T)//Ham co chuc nang chen la p vao cay T { if(!T)T=p;//Neu goc la NULL hoac la la thi chen la p vao. else { if(p->infor>T->infor)//Neu la can chen > nut truoc(cha)hoac goc insert(p,T->right);//Thi chen vao khoa phai(Do cay dang xet:NPTK) else insert(p,T->left);//Nguoc lai thi chen vao khoa trai ! } } void nhap(Tree &T) { Tree p; int x; char ch; printf("An q hoac Q sau khi nhap gia tri de thoat!\n"); do { p=new element; printf("Nhap gia tri: "); scanf("%d",&x);

48

p->infor=x; p->left=p->right=NULL; insert(p,T);//Chen la p vao cay T fflush(stdin);scanf("%c",&ch); }while(ch!='q'&& ch!='Q'); } void NLR(Tree T) { if(T) { printf("%3d",T->infor); NLR(T->left); NLR(T->right); } } void LNR(Tree T) { if(T) { LNR(T->left); printf("%3d",T->infor); LNR(T->right); } } void LRN(Tree T) { if(T) { LRN(T->left); LRN(T->right); printf("%3d",T->infor); } } int dem(Tree T,int &nut,int &la,int &nhanh) { if(T) { nut++; If((!T->left)&&(!T->right)) la++; else nhanh++;//nut nhanh la nut ma khong phai nut la dem(T->left,nut,la,nhanh); dem(T->right,nut,la,nhanh); } return nhanh; } void greater(Tree T,Tree p,int x) { if(!T)return ; else { if(T->infor>x)printf("%3d",T->infor); if(T->infor==x) p=T; greater(T->left,p,x); greater(T->right,p,x); } }

49

main() { Tree T,p; int la,nut,nhanh,x,a=0; nhanh=la=nut=0; char ch; create(T); printf("\t\t*******************************************\n"); printf("\t\t* *\n"); printf("\t\t* THUC HANH CU TRUC DU LIEU *\n"); printf("\t\t* DE SO 17 *\n"); printf("\t\t* *\n"); printf("\t\t*******************************************\n"); do{ printf("\n"); printf("\t1. NHAP CAY NHI PHAN TIM KIEM\n"); printf("\t2. DUYET BANG PHUONG PHAP NLR\n"); printf("\t3. DUYET BANG PHUONG PHAP LNR\n"); printf("\t4. DUYET BANG PHUONG PHAP LRN\n"); printf("\t5. DEM SO NUT-SO NUT LA-SO NUT NHANH\n"); printf("\t6. TIM KIEM NUT X TREN CAY\n"); printf("\t7. Thoat\n"); printf("\t\t Chon cong viec: ");fflush(stdin);scanf("%c",&ch); printf("\n\n"); switch(ch){ case '1': nhap(T);break; case '2': printf("Duyet NLR:\n");NLR(T);break; case '3': printf("\nDuyet LNR:\n");LNR(T);break; case '4': printf("\nDuyet LRN:\n");LRN(T);break; case '5': printf("\n So la :%d\tSo nut %d\tSo nut nhanh: %d",la,nut,dem(T,nut,la,nhanh));break; case '6': printf("\nNhap gia tri can tim x= ");scanf("%d",&x); printf("\nCac nut lon hon %d la :",x);greater(T,p,x); if(p) printf("\n %d thuoc but o dia chi %d",x,p); else printf("\n %d khong thuoc nut nao tren cay !\n",x);break; case '7': exit(1); } }while(ch!='7'); getch(); } 18 Cho mt cy nh phn tim kim c gc l root.Mi node trn cy c info l mt s nguyn v lin kt ch n cy con tri v cy con phi.Vit chng trnh thc hin cc cng vic sau: a. Duyt cy nh phn theo cc phng php NLR,LNR,LRN. b. Tnh di ca cy v tng gi tr cc node trn cy. c. Tm x (x nhp t bn phm ) trong cy.Nu tm thy hy in ra mn hnh gi tr ca cc node nh hn x. #include <stdio.h> #include <conio.h> #include <stdlib.h> typedef struct _node{

50

int info; _node *left,*right; } *node; node root; void init(node& root);//Khoi tao void nhap();//Nhap node moi void insert(node p,node& root); void NLR(node root);//Duyet cay theo NLR void LNR(node root);//Duyet cay theo LNR void LRN(node root); int tim(int x,node root); void nhohon(int,node);//in ra cac phan tu lon hon x int max(int a,int b);//Tim max 2 so int chieucao(node root); //Tinh chieu cao = de quy int sumnode(node root);//tong gia tri cac nut tren cay main(){ int x; init(root); nhap(); printf("\nDuyet NLR: ");NLR(root); printf("\nDuyet LNR: ");LNR(root); printf("\nDuyet LRN: ");LRN(root); printf("\nChieu cao: %d",chieucao(root)); printf("\nTong gia tri cac nut tren cay: %d ",sumnode(root)); printf("\nNhap x: ");scanf("%d",&x); if (tim(x,root)){ printf("\nCac node nho hon %d la: ",x);nhohon(x,root); } else printf("\nKhong tim thay %d !",x); getch(); } int sumnode(node root){ if (root==NULL) return 0; else return (root->info + sumnode(root->left) + sumnode(root->right)); } int chieucao(node root){ if (root==NULL) return 0; else return max(chieucao(root->left),chieucao(root->right))+1; } int max(int a,int b){ return (a>b ? a:b); } void nhohon(int x,node root){ if (root!=NULL){ if (root->info<x) printf("%4d",root->info); nhohon(x,root->left); nhohon(x,root->right); } } int tim(int x,node root){ if (root!=NULL){ if (root->info==x) return 1; if ((x>root->info)) tim(x,root->right); else tim(x,root->left);

51

} else return 0; } void init(node& root){ root=NULL; } void nhap(){ int info; node p; do{ p=new _node; printf("(Nhap 0 de ket thuc) Nhap gia tri:"); scanf("%d",&info); p->info=info; p->left=NULL; p->right=NULL; if (info) insert(p,root); }while (info); } void insert(node p,node& root){ if (root==NULL) root=p; else{ if(p->info>root->info) insert(p,root->right); else insert(p,root->left); } } void NLR(node root){ if (root!=NULL){ printf("%-4d",root->info); NLR(root->left); NLR(root->right); } } void LNR(node root){ if (root!=NULL){ LNR(root->left); printf("%-4d",root->info); LNR(root->right); } } void LRN(node root){ if (root!=NULL){ LRN(root->left); LRN(root->right); printf("%-4d",root->info); } } A. ti:( 19) Cho cy nh phn c gc l root, mi nt trn cy bao gm c info l mt s nguyn (khng trng nhau), lin kt tri v lin kt phi. Vit chng trnh thc hin cc cng vic sau: a. Duyt cy nh phn theo cc phng php NLR, LNR, LRN. b. m s nt bc mt ca cy. c. Tm x (x nhp t bn phm) trn cy v m c bao nhiu node c gi tr x trn cy.

52

I.

Ton b code chng trnh. #include <conio.h> #include <stdio.h> #include <stdlib.h> struct element { int info; element *left,*right; }; typedef element *Tree; Tree T; //=================================== void Create(Tree &T) { T=NULL; } //=================================== void Insert(Tree p,Tree &T) { if(T==NULL) T = p; else { if(p->info<T->info) Insert(p,T->left); else Insert(p,T->right); } } //=================================== void NLR(Tree T) { if(T!=NULL) { printf("%-4d",T->info); NLR(T->left); NLR(T->right); } } //=================================== void DemBac1(Tree T,int &NotBac1) { if(T!=NULL) { if((T->left==NULL && T->right!=NULL) || (T->left!=NULL && T>right==NULL)) NotBac1 ++; DemBac1(T->left,NotBac1); DemBac1(T->right,NotBac1); } } //=================================== void LNR(Tree T) { if(T!=NULL) { LNR(T->left); printf("%-4d",T->info); LNR(T->right);

53

} } //=================================== void LRN(Tree T) { if(T!=NULL) { LRN(T->left); LRN(T->right); printf("%-4d",T->info); } } //=================================== int TimX(Tree T,int x) { if(T==NULL) return 0; else { if(T->info==x) return (TimX(T->left,x)+TimX(T->right,x)+1); else return (TimX(T->left,x)+TimX(T->right,x)); } } //=================================== void Nhap() { int info; Tree p; do { p= new element; printf("Nhap gia tri nut: (0 = ket thuc) "); scanf("%d",&info); p->info = info; p->left = NULL; p->right = NULL; if(info) Insert(p,T); } while(info); } //=================================== main() { int NotBac1=0; int x=0; Create(T); Nhap(); printf("\nDuyet NLR:");NLR(T); printf("\nDuyet LNR:");LNR(T); printf("\nDuyet LRN:");LRN(T); DemBac1(T,NotBac1); printf("\nDem so not bac 1: %d",NotBac1); printf("\nNhap x:");scanf("%d",&x); printf("Co %d Node bang %d",TimX(T,x),x);

54

getch(); } 20: Cho cy nh phn c gc l root, mi nt trn cy bao gm info l mt s nguyn (khng trng nhau), lin kt tri v lin kt phi. Vit chng trnh thc hin cc cng vic sau: a. Duyt cy nh phn theo cc phng php NLR, LNR, LRN. b. Tnh chiu cao ca cy. c. Tnh tng gi tr cc node mc x ( x nhp t bn phm) trn cy. //De 20 #include<conio.h> #include<stdio.h> #include<stdlib.h> #include<dos.h> // Khai bao cac kieu du lieu moi typedef int KeyType; typedef struct tagNode { KeyType key; struct tagNode *Left; struct tagNode *Right; }Node; typedef Node *BTree; // Khai bao cac ham dung trong chuong trinh void MakeNullBTree(BTree &Root); // Tao cay rong int EmptyBTree(BTree Root); // Kiem tra cay rong void InsertNode(KeyType x,BTree &Root); // Them mot node co khoa x vao cay NPTK Node *CreateNode(KeyType x); // Tao mot node co khoa x va tra ve dia chi cua Node do void InputBTree(BTree &Root); // Nhap cay NPTK; void PreOrder(BTree Root); // Duyet cay NPTK Tien tu(Node-Left-Right); void InOrder(BTree Root); // Duyet cay NPTK Trung tu(Left-Node-Right); void PostOrder(BTree Root); // Duyet cay NPTK Hau tu(Left-Right-Node); int Max(int a,int b); // Ham so sanh max int HightBTree(BTree Root); // Ham tinh chieu cao cua cay Node *SearchNode(BTree Root,KeyType x); // Tim kiem mot node tra ve dia chi node do void Error(int i); // Ham thong bao loi int Sum (BTree Root, int level); // Ham dem tong gia tri cua cac node o muc x int Menu(); // Xay dung menu cua chuong trinh chinh /*---------------------Main Program--------------------------*/ main() { BTree Root; Node *p; int Router,x,t;

55

MakeNullBTree(Root); do { Router=Menu(); switch(Router) { case 1: printf("\n------------------------------------------------------"); InputBTree(Root); break; case 2: printf("\n------------------------------------------------------"); if(EmptyBTree(Root)) Error(4); else { printf("\nDuyet NLR (Node-Left-Right): ");PreOrder(Root); printf("\nDuyet LNR (Left-Node-Right): ");InOrder(Root); printf("\nDuyet LRN (Left-Right-Node): ");PostOrder(Root); printf("\n"); } printf("\n------------------------------------------------------"); break; case 3: printf("\n------------------------------------------------------"); printf("\n Chieu cao cua cay la : %d",HightBTree(Root)); printf("\n------------------------------------------------------"); break; case 4: printf("\n------------------------------------------------------"); printf("\n Nhap x: ");scanf("%d",&x); printf("\n Tong gia tri cua cac node o muc x la : %d ",Sum(Root, x)Sum(Root, x-1)); printf("\n------------------------------------------------------"); break; default: printf("\n------------------------------------------------------"); Error(2); printf("\n------------------------------------------------------"); } }while(Router!=0); } /*----------------------Functions--------------------------*/ // Dinh nghia ham tao cay NPTK rong void MakeNullBTree(BTree &Root) { Root=NULL; } // Dinh nghia ham kiem tra cay rong int EmptyBTree(BTree Root) { return (Root==NULL); } // Dinh nghia ham tao mot Node co khoa x tra ve dia chi nut vua tao Node *CreateNode(KeyType x)

56

{ Node *p; p=new Node; if(p==NULL) Error(1); else { (*p).key=x; (*p).Left=NULL; (*p).Right=NULL; } return p; } // Dinh nghia ham chen mot node co khoa x vao cay Nhi Phan void InsertNode(KeyType x,BTree &Root) { if(Root==NULL)// Neu cay nay dang rong { Root=CreateNode(x); } else { if(x<(*Root).key) InsertNode(x,(*Root).Left); if(x>(*Root).key) InsertNode(x,(*Root).Right); } } // Dinh nghia ham tim mot node co key x va tra ve dia chi cua node do Node *SearchNode(BTree Root,KeyType x) { if(EmptyBTree(Root)) return NULL; else { if((*Root).key==x) return Root; if((*Root).key<x) return SearchNode((*Root).Right,x); else return SearchNode((*Root).Left,x); } } // Dinh nghia ham nhap cay NPTK void InputBTree(BTree &Root) { Node *p; KeyType key; int n,i; printf("\n Ban muon nhap bao nhieu node:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n Moi ban nhap Info cho node thu %d :",i); printf("\n Moi ban nhap mot so nguyen :"); scanf("%d",&key);

57

p=SearchNode(Root,key); if(p==NULL) InsertNode(key,Root); else { Error(3); i--; } } } // Dinh nghia ham duyet cay Tien tu (Node-Left-Right) void PreOrder(BTree Root) { if(Root!=NULL) { printf("%3d",(*Root).key); PreOrder((*Root).Left); PreOrder((*Root).Right); } } // Dinh nghia ham duyet cay Trung tu (Left-Node-Right) void InOrder(BTree Root) { if(Root!=NULL) { InOrder((*Root).Left); printf("%3d",(*Root).key); InOrder((*Root).Right); } } // Dinh nghia ham duyet cay Hau tu (Left-Right-Node) void PostOrder(BTree Root) { if(Root!=NULL) { PostOrder((*Root).Left); PostOrder((*Root).Right); printf("%3d",(*Root).key); } } // Dinh nghia ham Max cua a va b int Max(int a,int b) { return (a>b ? a:b); } // Dinh nghia ham tinh chieu cao cua cay int HightBTree(BTree Root) { if (Root==NULL) return 0; else return (1+Max(HightBTree(Root->Left),HightBTree(Root->Right))); } // Tinh tong gia tri cac node tu goc den muc x // Tinh tong gia tri cac node tai muc x : Sum(Root, x)- Sum(Root, x-1) int Sum (BTree Root, int level)

58

{ if (Root == NULL || level == 0) return 0; if (level == 1) return (*Root).key; else return (*Root).key + Sum (Root->Left, level-1) + Sum(Root->Right, level-1); } // Dinh nghia ham thong bao loi void Error(int i) { switch(i) { case 1: printf("\n Khong du bo nho."); break; case 2: printf("\n Khong co chuc nang nay.Moi ban thu lai."); break; case 3: printf("\n"); printf("\n So nay da duoc nhap.Moi ban chon so khac."); break; case 4: printf("\n Cay hien dang rong.Khong the thuc hien thao tac."); break; } } // Xay dung menu cho chuong trinh chinh int Menu() { int Router; printf("\n Moi ban chon chuc nang:"); printf("\n 1.Nhap cay nhi phan tim kiem."); printf("\n 2.Duyet cay NPTK."); printf("\n 3.Tinh chieu cao cua cay."); printf("\n 4.Tinh tong gia tri cua cac node o muc x tren cay"); printf("\n 0.Thoat chuong trinh."); printf("\n --->Ban chon cong viec nao: "); scanf("%d",&Router); printf("\n"); return Router; }

23: Cho danh sch lin kt n gm nhiu phn t, mi phn t gm c vng infor l s nguyn v vng lin kt cha a ch ca phn t k tip. Vit chng trnh thc hin cc cng vic sau: a. m s phn t dng ca danh sch lin kt. b.Tch danh sch lin kt lm hai, mt danh sch cha cc s chn v mt danh sch cha cc s l (khng lm ph hu danh sch cho). c. o ngc danh sch lin kt. V ni dung ton b chng trnh #include <stdio.h>

59

#include <conio.h> #include <string.h> #include <stdlib.h> typedef int infor; //--------------------------------------------------// //PHAN KHAI BAO struct element { infor so; element *next; }; typedef element *list; list ds,chan=NULL,le=NULL; int count; //--------------------------------------------------// //HAM CHINH void input(list &ds); void dem_duong(list ds); void tachds(list ds,list &chan,list &le); void dao_ds(list &ds); void sapxep(list &ds); //--------------------------------------------------// //HAM PHU void create(list &ds); void print(list ds); void insertlast(list &r,int a,list &temp); void inle(list le); void inchan(list chan); void swap(infor &a,infor &b); //--------------------------------------------------// //Ham chinh int main() { int cv; do{ system("cls"); printf("\n************************************"); printf("\n* Danh sach cac cong viec: *"); printf("\n* 0.Hien thi danh sach *"); printf("\n* 1.Nhap danh sach *"); printf("\n* 2.Dem so phan tu duong *"); printf("\n* 3.Tach danh sach *" ); printf("\n* 4.Dao nguoc danh sach *"); printf("\n* 5.Sap xep danh sach *"); printf("\n************************************"); printf("\nHay nhap cong viec can lam:"); scanf("%d",&cv); switch(cv) { case 0: print(ds);break; case 1: input(ds);print(ds);break; case 2: dem_duong(ds);break; case 3: tachds(ds,chan,le);inchan(chan); inle(le);break; case 4: dao_ds(ds); print(ds);break;

60

case 5: sapxep(ds); print(ds);break; default : return 1; } getch(); } while(1); } //--------------------------------------------------// //Chuong trinh ham void create(list &ds) { ds=NULL; } void input(list &ds) { int i=0,n,a; list p; do{ printf("Nhap so luong phan tu: "); scanf("%d",&n); } while (n<=0); do { printf("Nhap so thu %d :",i); scanf ("%d",&a); if(ds==NULL)//Kiem tra rong hay khong, neu rong thi cap bo nho tao moi { ds = new element; p = ds; goto l1; }; p->next=new element; p=p->next; l1: p->so = a; p->next=NULL; i++; n--; } while (n);

} void insertlast(list &r,int a,list &temp) //bien temp de luu vi tri cuoi cua DS so can chen { if( r == NULL) { r = new element; temp = r; }

61

else { temp->next = new element; temp =temp->next; } temp->so = a; temp->next = NULL; ; } void print(list ds) { int i=0; list p; p = ds; while(p!=NULL) { printf("\nSo thu %d :",i); printf("%d",p->so); p=p->next; i++; } } void dem_duong(list ds) { int count=0; list p; p = ds; while(p!=0) { if(p->so > 0) count++; p=p->next; } if(count) printf("\nCo %d phan tu duong ",count); else printf("\nKhong co phan tu duong nao ca."); } void tachds(list ds,list &chan,list &le) { if(ds==NULL) { printf("Danh sach khong ton tai");return;} list p,odd,even;// bien odd va even de nho vi tri cuoi de chen phan tu chan le vao DS moi p = ds; odd=le; even=chan; infor a; do { a = p->so; if(a >= 0) if ( a%2==1 ) insertlast(le,a,odd); else insertlast(chan,a,even);

62

p = p->next; } while (p!=NULL); } void inchan(list chan) { list p; p=chan; printf("\nDanh sach cac so chan : "); while(p!=NULL) { printf ("%3d ",p->so); p=p->next; }; } void inle(list le) { list p; p=le; printf("\nDanh sach cac so le : "); while(p!=NULL) { printf ("%3d ",p->so); p=p->next; } } void dao_ds(list &ds) { printf("\nDanh sach sau khi dao nguoc"); if(ds==NULL) return; list hientai=NULL; list truoc=NULL; while(ds) { hientai=ds; ds=ds->next; hientai->next=truoc; truoc=hientai; } ds=hientai; } void sapxep(list &ds) { if(ds==NULL) { printf("Danh sach khong ton tai");return;} printf("\nDanh sach sau khi sap xep"); list index1,index2;//index1 chi vi tri sau khi sap xep, index2 chi vi tri dang tim kiem index1=ds; while (index1->next!=NULL) { index2=index1->next; ; while(index2!=NULL) {

63

if(index2->so < index1->so) swap(index2->so,index1->so); index2=index2->next; } index1=index1->next; } } void swap(infor &a,infor &b) { infor t; t=a; a=b; b=t; } 24 #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> typedef int infor1; struct element { infor1 data; element *next; }; typedef element *List; List F,F1,F2; /*F danh sach nhap, F1 danh sach so chan, F2 danh sach so le*/ int act; /*Bien act de nhan thao tac thuc hien*/ infor1 x; /*Chen dau tien*/ void InsertFirst(List &F,infor1 x) { List p; p=new element; (*p).data=x; (*p).next=F; F=p; } /*Kiem tra co phai la so nguyen to*/ int PrimeNumber(infor1 x) { int i=2; if (x<0) return 0; /*Khong co dinh nghia so nguyen to cho so am */ if (x==1) return 1; while ((i<x)&&(x%i)) { i++; } if (i==x) return 1; else return 0; } /*Tinh tong cac so am*/ void SumNegativeNumber(List F)

64

{ List p; p=F; int sum=0; while ((p!=NULL)) { if ((*p).data<0) sum= sum + (*p).data; p=(*p).next;; } printf("\nThe sum of negative numbers is: %d",sum); } /*In cac so nguyen to trong DSLK ra man hinh*/ void PrintPrimeNumber(List F) { List p; int count=0; p=F; printf("\nYour list of prime numbers is: "); while(p!=NULL) { if (PrimeNumber((*p).data)) { printf("%d ",(*p).data); count++; } p=(*p).next; } if (count==0) printf(" Empty list."); printf("\nThe amount's prime number is: %d",count); } void Creat(List &F) { F=NULL; char y[1]; printf("\n Input code Y to continue."); do { printf("\n Input your data: "); scanf("%d",&x); InsertFirst(F,x); printf("\n Do you want to continue: ); gets(y);gets(y); } while (strcmp(y,"Y")==0); } void Display(List F) { List p; p=F; if (p==NULL) printf (" Empty list."); else while(p!=NULL) { printf("%d ",(*p).data); p=(*p).next; } } /*Chi DSLK ra lam hai danh sach */ void DivideList(List F) { List p;

65

p=F; while(p!=NULL) { x=(*p).data; if (((x%2)==0)&&(x>0)) InsertFirst(F1,x); if (((x%2)!=0)&&(x>0)) InsertFirst(F2,x); p=(*p).next; } } main() { F=NULL; do { printf("\n\n =============================MENU=========================== ==="); printf("\n 1.Input your list."); printf("\n 2.View prime numbers in your list."); printf("\n 3.View sum of negative numbers in your list."); printf("\n 4.Divide your list in to Even number list and Odd number list."); printf("\n 0.Exit."); printf("\n ============================================================ ==="); printf("\nYour act: "); scanf("%d",&act); switch(act) { case 0: break; case 1: Creat(F); break; case 2: PrintPrimeNumber(F); break; case 3: SumNegativeNumber(F); break; case 4: DivideList(F); printf("\nYour list: "); Display(F); printf("\nYour even list: "); Display(F1); printf("\nYour odd list : "); Display(F2); break; } } while (act); }

66

You might also like