/* ----------------------------------------------------------------------------- - Su Dung Con Tro / ----------------------------------------------- a) nhap ds so nguyen b) Sap xep giam c) Tong cac so nguyen la so 9 phuong d) in ket qua vua tinh e) Sap xep theo phuong phap noi bot f) Tong cac so nguyen to trong danh sach g) Tong cac so nguyen la so chan/le h) In ra day con co phan tu tang dan nieu nhat i) dem so phan tu trong danh sach j) Tim gia tri lon nhat/nho nhat trong danh sach k) Tinh trung binh cac phan tu trong danh sach l) In ra nhung phan tu co gia tri lon hon/ nho hon gia tri trung binh m) Viet ham kiem tra danh sach tang dan neu chua tang thi sap xep cho tang n) Chen 1 phan tu vao danh sach da sap xep tang dan o) Viet ham nhap vao 2 danh sach --------------------------------------------------------------------------------*/ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <stdlib.h> #include <math.h> #include <dos.h> typedef int ElementType; struct Node { ElementType Element; Node *Next; }; typedef Node *List; typedef Node *Position; void MakeNullList(List L) { L->Next=NULL; } //a)NHAP DANH SACH void InsertList(List L) { clrscr(); Position P; int sophantu=1; //Nhap phan tu thu 1 Node *newnode=new Node; newnode->Next=NULL; printf("Nhap phan tu thu %d: ",sophantu); scanf("%d",&newnode->Element); L->Next=newnode; P=newnode; sophantu+=1; //Nhap cac phan tu tiep theo khi nhap 0 thi ket thuc viec nhap while(1) { Node *newnode=new Node; printf("Phan tu thu %d :",sophantu); scanf("%d",&newnode->Element); if(newnode->Element==0) { free(newnode); break; } newnode->Next=NULL; P->Next=newnode; P=newnode; sophantu+=1; } } //In DS void ShowList(List L) { Position P=L->Next; while(P!=NULL) { printf("|%d|",P->Element); P=P->Next; } } //Tra vi dia chi con tro truoc P trong DS L Position TruocP(List L,Position P) { Position PtruocP=L; while(PtruocP->Next!=P&&PtruocP!=NULL) { PtruocP=PtruocP->Next; } return PtruocP; } void Swap(List L, Position P1,Position P2) { Node *newnode1=new Node; Node *newnode2=new Node; newnode1->Element=P1->Element; newnode2->Element=P2->Element; newnode1->Next=P2->Next; TruocP(L,P2)->Next=newnode1; newnode2->Next=P1->Next; TruocP(L,P1)->Next=newnode2; free(P1); free(P2); } // Dem So Phan Tu trong danh sach int LengthList(List L) { int len=0; Position P=L->Next; while(P!=NULL) { len+=1; P=P->Next; } return len; } Position ConvertXToP(List L,int stt) { Position P=L; for(int i=1;i<=stt;i++) { P=P->Next; } return P; } //b) SAP XEP GIAM void DesendingSort(List L) { for(int i=1;i<=LengthList(L)-1;i++) for(int j=i+1;j<=LengthList(L);j++) { if(ConvertXToP(L,j)->Element>ConvertXToP(L,i)->Element) { Swap(L,ConvertXToP(L,i),ConvertXToP(L,j)); } } } // La so 9 phuong thi tra ve 1 nguoc lai tra ve 0 int SoChinhPhuong(int number) { if(number<0) return 0; if(sqrt(number)-(int)sqrt(number)==0) return 1; else return 0; } //c) Tong cac so nguyen la so chinh phuong int TongChinhPhuong(List L) { int tong=0; Position P=L->Next; while(P!=NULL) { if(SoChinhPhuong(P->Element)==1) tong+=P->Element; P=P->Next; } return tong; } //d)Bubble Sort (Sap xep noi bot) void BubbleSort(List L,int tanggiam) //1 thi sap xep tang nguoc lai thi sap xep giam { for(int index1=1;index1<=LengthList(L)-1;index1++) { for(int index2=LengthList(L);index2>index1;index2--) { if(tanggiam==1) // Sap xep tang { if(ConvertXToP(L,index2-1)->Element>ConvertXToP(L,index2)->Element) { Swap(L,ConvertXToP(L,index2-1),ConvertXToP(L,index2)); } } else //sap xep giam { if(ConvertXToP(L,index2-1)->Element<ConvertXToP(L,index2)->Element) { Swap(L,ConvertXToP(L,index2-1),ConvertXToP(L,index2)); } } } } } // Neu la so nguyen to tra ve 1 khong phai tra ve 0 int NguyenTo(int number) { if(number<=1) return 0; int count=1; for(int i=2;i<=number/2;i++) { if(number%i==0) { count=0; break; } } return count; } int TongNguyenTo(List L) { int tong=0; Position P=L->Next; while(P!=NULL) { if(NguyenTo(P->Element)==1) { tong+=P->Element; } P=P->Next; } return tong; } //Tong cac phan tu trong danh sach int TongCong(List L) { int tong=0; Position P=L->Next; while(P!=NULL) { tong+=P->Element; P=P->Next; } return tong; } //Tong cac so chan cac so le int Tong(List L,int chanle) // changle=1 thi tinh tong cac so le 0 thi nguoc lai { int tongchan=0,tongle=0; Position P=L->Next; while(P!=NULL) { if(P->Element%2==0) { tongchan+=P->Element; } else { tongle+=P->Element; } P=P->Next; } if(chanle==0) return tongchan; else return tongle; } //In ra Day con tang co nhieu phan tu nhat void InDayTangNhieuNhat(List L) { Position P=L->Next; List LVTDD; //LVTDD: List Vi Tri Dau Day. Dung Luu cac vi tri phan tu dau cua cac dau con Position PLVTDD; List LLen; //Dung de luu chieu dai cua cac day con tuong ung MakeNullList(LVTDD); Node *newnode=new Node; newnode->Element=1; newnode->Next=NULL; LVTDD->Next=newnode; PLVTDD=newnode; int stt=2; while(P->Next!=NULL) { if(P->Next->Element>P->Element) { Node *newnode=new Node; newnode->Element=stt; newnode->Next=NULL; PLVTDD->Next=newnode; PLVTDD=newnode; } P=P->Next; stt+=1; } Position Pchay=LVTDD->Next; while(Pchay!=NULL) { printf("|%d|",P->Element); Pchay=Pchay->Next; } } // Gia tri lon nhat nho nhat trong DS int MaxMin(List L,int maxmin) //maxmin=0 tra ve min, maxmin=1 tra ve max { int max=0, min=1000000; Position P=L->Next; while(P!=NULL) { if(max<P->Element) { max=P->Element; } if(min>P->Element) { min=P->Element; } P=P->Next; } if(maxmin==1) return max; else return min; } // Gia tri trung binh cua cac phan tu trong DS float Avegare(List L) { return (Tong(L,1)+Tong(L,0))/LengthList(L); } // In ds Lon/nho hon tb void LonNhoHonTB(List L,int lonnho)//lonnho=1 thi in cac phan tu lon hon TB { //lonhon=0 thi in cac phan tu nho hon TB if(lonnho==1) { Position P=L->Next; while(P!=NULL) { if(P->Element>Avegare(L)) { printf("|%d|",P->Element); } P=P->Next; } } else { Position P1=L->Next; while(P1!=NULL) { if(P1->Element<Avegare(L)) { printf("|%d|",P1->Element); } P1=P1->Next; } } } // Kiem tra ds tang dan chua neu chua thi sap xep lai void CheckAscendingList(List L) { Position P=L->Next; while(1) { if(P->Element>P->Next->Element||P!=NULL) { break; } P=P->Next; } if(P!=NULL) { BubbleSort(L,1); } } //Chen 1 Phan tu vao ds da sap xep tang dan void InsertIntoAscendingList(List L,int value) { Node *insertnode=new Node; insertnode->Element=value; Position P=L; while(P!=NULL) { if(P->Next->Element>=value) { break; } P=P->Next; } if(P!=NULL) //P khong phai la phan tu cuoi { insertnode->Next=P->Next; P->Next=insertnode; } else { P=L->Next; while(P->Next!=NULL) { P=P->Next; } P->Next=insertnode; insertnode->Next=NULL; } } // Nhap 2 DS void Insert2List(List L1,List L2) { InsertList(L1); InsertList(L2); } void FreeList(List L) { Position P=L; while(P->Next!=NULL) { P=P->Next; } free(P); FreeList(L); } void main() { clrscr(); List L1; MakeNullList(L1); InsertList(L1); printf("Danh sach vua nhap: "); ShowList(L1); getch(); DesendingSort(L1); printf("\nDanh sach da sap xep giam dan: "); ShowList(L1); getch(); printf("\nTong Cac so chinh phuong: |%d|",TongChinhPhuong(L1)); getch(); BubbleSort(L1,1); printf("\nDanh Sach Bubble Sort tang dan: "); ShowList(L1); getch(); printf("\nTong cac so nguyen to: |%d|",TongNguyenTo(L1)); getch(); printf("\nTong cac so trong danh sach: |%d|",TongCong(L1)); getch(); printf("\nTong cac so le: |%d|",Tong(L1,1)); getch(); printf("\nTong cac so chan: |%d|",Tong(L1,0)); //InDayTangNhieuNhat(L1); getch(); printf("\nGia tri lon nhat: |%d|",MaxMin(L1,1)); getch(); printf("\nGia tri nho nhat: |%d|",MaxMin(L1,0)); getch(); printf("\nGia tri trung binh: |%f|",Avegare(L1)); getch(); printf("\nLon hon TB: "); LonNhoHonTB(L1,1); getch(); printf("\nNho hon TB: "); LonNhoHonTB(L1,0); getch(); int socanchen; printf("\nNhap so can chen vao DS: "); scanf("%d",&socanchen); printf("\nDS sau khi chen %d: ",socanchen); InsertIntoAscendingList(L1,socanchen); ShowList(L1); printf("\n\nAn Phim Bat Ky De Thoat..."); getch(); }
nosomovo