1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
/* ----------------------------------------------------------------------------- - 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
Xem thêm Code C minh họa danh sách liên kết sử dụng con trỏ