Block list _Alpha
"block list.h"
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 | #ifndef _BLOCK_LIST_ #define _BLOCK_LIST_ struct Block_Node_Type { int Key; struct Block_Node_Type* Pointer_Next; // Next node or Next block struct Block_Node_Type* Pointer_Next_Block; // Next block or Previous node }; struct Linked_Block_Type { int Split_Range; int Number_Of_Current_Element; struct Block_Node_Type Header; }; struct Linked_Block_Type* Create_Linked_Block(int Split_Range); int Insert_Linked_Block_Element(struct Linked_Block_Type* Pointer_List, const struct Block_Node_Type element); int Remove_Linked_Block_Element(struct Linked_Block_Type* Pointer_List, const int key); void Delete_Linked_Block(struct Linked_Block_Type* Pointer_List); void Display_Linked_Block(struct Linked_Block_Type* Pointer_List); void Check_Tool(const struct Linked_Block_Type* Pointer_List); int Check_Infinity_For_Block_List(struct Linked_Block_Type* Pointer_List); int Check_The_Node_For_Block_List(struct Linked_Block_Type* Pointer_List); int Check_The_Block_For_Block_List(struct Linked_Block_Type* Pointer_List, const int Number_Of_Block); int Check_All_The_Linked_Block_List(struct Linked_Block_Type* Pointer_List, const int Number_Of_Block); int Get_Length_Of_The_Linked_Block(struct Linked_Block_Type* Pointer_List); #endif // !_BLOCK_LIST_ #ifndef _COMMON_DEFINITION_ #define _COMMON_DEFINITION_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_DEFINITION_ | cs |
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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "block list.h" struct Linked_Block_Type* Create_Linked_Block(int Split_Range) { if (Split_Range < 1) { printf("Error! Range must be greater then 1. Create_Linked_Block()\n"); return NULL; } struct Linked_Block_Type* Pointer_Return = (struct Linked_Block_Type*) malloc(1 * sizeof(struct Linked_Block_Type)); if (!(struct Linked_Block_Type*)Pointer_Return) { printf("Error! The linked block was null. Create_Linked_Block()\n"); return NULL; } (void*)memset((struct Linked_Block_Type*)Pointer_Return, 0, 1 * sizeof(struct Linked_Block_Type)); Pointer_Return->Header.Pointer_Next_Block = NULL; Pointer_Return->Header.Pointer_Next = NULL; Pointer_Return->Split_Range = Split_Range; Pointer_Return->Number_Of_Current_Element = 0; return (struct Linked_Block_Type*) Pointer_Return; } int Insert_Linked_Block_Element(struct Linked_Block_Type* Pointer_List, const struct Block_Node_Type element) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Insert_Linked_Block()\n"); return FALSE; } struct Block_Node_Type* Pointer_Header_Node = (struct Block_Node_Type*) &Pointer_List->Header; struct Block_Node_Type* Pointer_New_Node = (struct Block_Node_Type*) malloc(1 * sizeof(struct Block_Node_Type)); if (!(struct Block_Node_Type*)Pointer_New_Node) { printf("Error! The dynamic memory allocation failed. Insert_Linked_Block()\n"); return FALSE; } (void*)memset((struct Block_Node_Type*)Pointer_New_Node, 0, 1 * sizeof(struct Block_Node_Type)); *Pointer_New_Node = *(struct Block_Node_Type*) &element; int Length_Of_The_Block = Get_Length_Of_The_Linked_Block((struct Linked_Block_Type*)Pointer_List); // First insert if (Length_Of_The_Block == 0) { Pointer_Header_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; Pointer_Header_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_New_Node->Pointer_Next = NULL; Pointer_New_Node->Pointer_Next_Block = NULL; Pointer_List->Number_Of_Current_Element++; return TRUE; } struct Block_Node_Type* Pointer_Current_Node = (struct Block_Node_Type*) Pointer_Header_Node->Pointer_Next_Block; int Split_Of_New_Element_Key = (float)Pointer_New_Node->Key / (float)Pointer_List->Split_Range; int Split_Of_Current_Element_Key = (float)Pointer_Current_Node->Key / (float)Pointer_List->Split_Range; // Insert if smaller then the first block. if (Split_Of_New_Element_Key < Split_Of_Current_Element_Key) { Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*) Pointer_Current_Node; Pointer_Header_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; Pointer_Header_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_List->Number_Of_Current_Element++; return TRUE; } struct Block_Node_Type* Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Header_Node; while (Split_Of_New_Element_Key > Split_Of_Current_Element_Key && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block != NULL) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; Split_Of_Current_Element_Key = (float)Pointer_Current_Node->Key / (float)Pointer_List->Split_Range; } // Insert if bigger then the last block. if (Split_Of_New_Element_Key > Split_Of_Current_Element_Key) { Pointer_New_Node->Pointer_Next_Block = NULL; Pointer_New_Node->Pointer_Next = NULL; Pointer_Current_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != NULL) { Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; } Pointer_Current_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_List->Number_Of_Current_Element++; return TRUE; } // Insert block between blocks. else if (Split_Of_New_Element_Key < Split_Of_Current_Element_Key) { Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Previous_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; while ((struct Block_Node_Type*) Pointer_Previous_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Current_Node) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Previous_Node->Pointer_Next; } Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_List->Number_Of_Current_Element++; return TRUE; } // Insert if the same as a comparison block. (Insert node..) __Part 01 else if (Split_Of_New_Element_Key == Split_Of_Current_Element_Key && Pointer_New_Node->Key < Pointer_Current_Node->Key) { Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Previous_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; while ((struct Block_Node_Type*)Pointer_Previous_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Current_Node) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Previous_Node->Pointer_Next; } Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_Current_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; if ((struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next == NULL) Pointer_Current_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node->Pointer_Next_Block; Pointer_List->Number_Of_Current_Element++; return TRUE; } // Insert if the same as a comparison block. (Insert node..) __Part 02 else if (Split_Of_New_Element_Key == Split_Of_Current_Element_Key && Pointer_New_Node->Key >= Pointer_Current_Node->Key) { int Number_Of_Move = 0; struct Block_Node_Type* Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; while (Pointer_New_Node->Key > Pointer_Current_Node->Key && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; Number_Of_Move++; } // Insert the first node between the blocks if (Number_Of_Move == 0 && Pointer_New_Node->Key != Pointer_Current_Node->Key && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block) { Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_Current_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; } // Insert at the last node of the block else if ((Pointer_New_Node->Key >= Pointer_Current_Node->Key && (struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next == (struct Block_Node_Type*) Pointer_Next_Block && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block == (struct Block_Node_Type*)Pointer_Next_Block) || (Pointer_New_Node->Key >= Pointer_Current_Node->Key && (struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next == (struct Block_Node_Type*) Pointer_Next_Block && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block == (struct Block_Node_Type*)Pointer_Previous_Node)) { Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; Pointer_Current_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; } // Insert node between the nodes else if (Pointer_New_Node->Key >= Pointer_Current_Node->Key && (struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*) Pointer_Next_Block && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block == (struct Block_Node_Type*)Pointer_Previous_Node) { Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Previous_Node; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_Current_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; } // Troubleshooting duplicate value!!! else { if (Pointer_New_Node->Key >= Pointer_Current_Node->Key) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; } Pointer_New_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Previous_Node; Pointer_New_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Current_Node; Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_New_Node; Pointer_Current_Node->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_New_Node; } Pointer_List->Number_Of_Current_Element++; return TRUE; } return FALSE; } int Remove_Linked_Block_Element(struct Linked_Block_Type* Pointer_List, const int key) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Remove_Linked_Block()\n"); return FALSE; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Remove_Linked_Block_Element()\n"); return FALSE; } struct Block_Node_Type* Pointer_Delete_Node = (struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block; struct Block_Node_Type* Pointer_Previous_Node = (struct Block_Node_Type*) &Pointer_List->Header; int Split_Of_Current_Element_Key = 0, Split_Of_Target_Element_Key = (float)key / (float)Pointer_List->Split_Range; while ((struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next_Block != NULL) { Split_Of_Current_Element_Key = (float)Pointer_Delete_Node->Key / (float)Pointer_List->Split_Range; if (Split_Of_Current_Element_Key == Split_Of_Target_Element_Key) break; Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Delete_Node; Pointer_Delete_Node = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next_Block; } // Not found target... (Block) Split_Of_Current_Element_Key = (float)Pointer_Delete_Node->Key / (float)Pointer_List->Split_Range; if (Split_Of_Target_Element_Key != Split_Of_Current_Element_Key) { printf("[%2d] | Not found(block)... :(\n", key); return FALSE; } // Find a target in a block else if (Pointer_Delete_Node->Key == key) { struct Block_Node_Type* Pointer_Previous_Block = (struct Block_Node_Type*)Pointer_Previous_Node; while ((struct Block_Node_Type*)Pointer_Previous_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Delete_Node) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Previous_Node->Pointer_Next; } Pointer_Previous_Block->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next; Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next; if ((struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next_Block) Pointer_Delete_Node->Pointer_Next->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next_Block; free((struct Block_Node_Type*)Pointer_Delete_Node); Pointer_List->Number_Of_Current_Element--; return TRUE; } struct Block_Node_Type* Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next_Block; while ((struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next != NULL && (struct Block_Node_Type*) Pointer_Delete_Node->Pointer_Next != (struct Block_Node_Type*) Pointer_Next_Block) { Pointer_Previous_Node = (struct Block_Node_Type*)Pointer_Delete_Node; Pointer_Delete_Node = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next; if (Pointer_Delete_Node->Key == key) break; } // Not found target.. (Node) if (Pointer_Delete_Node->Key != key) { printf("[%2d] | Not found(node)... :(\n", key); return FALSE; } // Find a target in a node if ((struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next == NULL) { Pointer_Previous_Node->Pointer_Next = NULL; } else if ((struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block) { Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next; Pointer_Delete_Node->Pointer_Next->Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next_Block; } else if ((struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next == (struct Block_Node_Type*)Pointer_Next_Block) { Pointer_Previous_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next; } free((struct Block_Node_Type*)Pointer_Delete_Node); Pointer_List->Number_Of_Current_Element--; return TRUE; } void Delete_Linked_Block(struct Linked_Block_Type* Pointer_List) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Delete_Linked_Block()\n"); return; } struct Block_Node_Type* Pointer_Header_Node = (struct Block_Node_Type*) &Pointer_List->Header; int circuit = 0; for (circuit = Get_Length_Of_The_Linked_Block((struct Linked_Block_Type*)Pointer_List); circuit; circuit--) { struct Block_Node_Type* Pointer_Delete_Node = (struct Block_Node_Type*)Pointer_Header_Node->Pointer_Next; Pointer_Header_Node->Pointer_Next = (struct Block_Node_Type*)Pointer_Delete_Node->Pointer_Next; free((struct Block_Node_Type*)Pointer_Delete_Node); } free((struct Linked_Block_Type*)Pointer_List); Pointer_List = NULL; return; } int Check_Infinity_For_Block_List(struct Linked_Block_Type* Pointer_List) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Check_For_Block_List()\n"); return FALSE; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Check_Infinity_For_Block_List()\n"); return FALSE; } struct Block_Node_Type* Pointer_Current_Node = (struct Block_Node_Type*) &Pointer_List->Header; while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != NULL) Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; Pointer_Current_Node = (struct Block_Node_Type*) Pointer_List->Header.Pointer_Next_Block; while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block != NULL) Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; Pointer_Current_Node = (struct Block_Node_Type*) Pointer_List->Header.Pointer_Next; struct Block_Node_Type* Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; while (1) { while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block || ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == (struct Block_Node_Type*)Pointer_Next_Block && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == NULL && (struct Block_Node_Type*)Pointer_Next_Block == NULL)) { if ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block) Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; if ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == NULL) return TRUE; } while ((struct Block_Node_Type*)Pointer_Current_Node != (struct Block_Node_Type*)Pointer_Next_Block) { Pointer_Current_Node = (struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next_Block; } Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Next_Block->Pointer_Next_Block; } return FALSE; } int Check_The_Node_For_Block_List(struct Linked_Block_Type* Pointer_List) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Check_Horizontally_For_Block_List()\n"); return FALSE; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Check_The_Node_For_Block_List()\n"); return FALSE; } int Node_Count = 1, Number_Of_Block = 1; struct Block_Node_Type* Pointer_Current_Node = (struct Block_Node_Type*) Pointer_List->Header.Pointer_Next_Block; struct Block_Node_Type* Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != NULL) { if ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == (struct Block_Node_Type*)Pointer_Next_Block && (struct Block_Node_Type*)Pointer_Next_Block != NULL) { Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Next_Block->Pointer_Next_Block; Number_Of_Block++; } Node_Count++; Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; } if (Node_Count == Pointer_List->Number_Of_Current_Element) { return Number_Of_Block; // TRUE } return FALSE; } int Check_The_Block_For_Block_List(struct Linked_Block_Type* Pointer_List, const int Number_Of_Block) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Check_Vertically_For_Block_List()\n"); return FALSE; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Check_The_Block_For_Block_List()\n"); return FALSE; } int Block_Count = 0; struct Block_Node_Type* Pointer_Current_Node = (struct Block_Node_Type*) &Pointer_List->Header; while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block != NULL) { Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; Block_Count++; } if (Block_Count == Number_Of_Block) { return TRUE; } return FALSE; } int Check_All_The_Linked_Block_List(struct Linked_Block_Type* Pointer_List, const int Number_Of_Block) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Check_Back_And_Forth_For_Block_List()\n"); return FALSE; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Check_All_The_Linked_Block_List()\n"); return FALSE; } int Number_Of_Pass = 1; struct Block_Node_Type* Pointer_Current_Node = (struct Block_Node_Type*) Pointer_List->Header.Pointer_Next; struct Block_Node_Type* Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next_Block; while (1) { while ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block || ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == (struct Block_Node_Type*)Pointer_Next_Block && (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == NULL && (struct Block_Node_Type*)Pointer_Next_Block == NULL)) { if ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next != (struct Block_Node_Type*)Pointer_Next_Block) Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; if ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == NULL && Number_Of_Pass == Number_Of_Block) return TRUE; else if ((struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next == NULL && Number_Of_Pass != Number_Of_Block) return FALSE; } while ((struct Block_Node_Type*)Pointer_Current_Node != (struct Block_Node_Type*)Pointer_Next_Block) { Pointer_Current_Node = (struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next_Block; } Pointer_Next_Block = (struct Block_Node_Type*)Pointer_Next_Block->Pointer_Next_Block; Number_Of_Pass++; } return FALSE; } void Check_Tool(const struct Linked_Block_Type* Pointer_List) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Check_Tool()\n"); return; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("No problem[1]. :)\n"); return; } else if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block->Pointer_Next == NULL && (struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block->Pointer_Next_Block == NULL) { printf("No problem[1]. :)\n"); return; } int result = 0; result = Check_Infinity_For_Block_List((struct Linked_Block_Type*)Pointer_List); if (!result) { printf("Check_Infinity_For_Block_List(). :(\n"); return; } result = Check_The_Block_For_Block_List((struct Linked_Block_Type*)Pointer_List, Check_The_Node_For_Block_List((struct Linked_Block_Type*)Pointer_List)); if (!result) { printf("Check_The_Block_For_Block_List(). :(\n"); return; } result = Check_All_The_Linked_Block_List((struct Linked_Block_Type*)Pointer_List, Check_The_Node_For_Block_List((struct Linked_Block_Type*)Pointer_List)); if (!result) { printf("Check_All_The_Linked_Block_List(). :(\n"); return; } printf("No problem. :)\n"); return; } int Get_Length_Of_The_Linked_Block(struct Linked_Block_Type* Pointer_List) { if (!(struct Linked_Block_Type*)Pointer_List) { printf("Error! The linked block was null. Get_Length_Of_The_Linked_Block()\n"); return FALSE; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Get_Length_Of_The_Linked_Block()\n"); return FALSE; } return Pointer_List->Number_Of_Current_Element; } void Display_Linked_Block(struct Linked_Block_Type* Pointer_List) { if (!(struct Linked_Block_Type*) Pointer_List) { printf("Error! The linked block was NULL. Display_Linked_Block()\n"); return; } if ((struct Block_Node_Type*)Pointer_List->Header.Pointer_Next_Block == NULL) { printf("Error! The next node of the node doesn't exist. Display_Linked_Block()\n"); return; } struct Block_Node_Type* Pointer_Current_Node = (struct Block_Node_Type*) Pointer_List->Header.Pointer_Next; struct Block_Node_Type* Pointer_Next_Block_Node = (struct Block_Node_Type*) Pointer_Current_Node->Pointer_Next_Block; printf("\nTotal number of node : %d\nSplit Range : %d\n\n", Pointer_List->Number_Of_Current_Element, Pointer_List->Split_Range); int circuit = 0; for (circuit = Get_Length_Of_The_Linked_Block((struct Linked_Block_Type*)Pointer_List); circuit; circuit--) { if ((struct Block_Node_Type*)Pointer_Current_Node == (struct Block_Node_Type*)Pointer_Next_Block_Node) { if ((struct Block_Node_Type*)Pointer_Current_Node == NULL || (struct Block_Node_Type*)Pointer_Next_Block_Node == NULL); else { Pointer_Next_Block_Node = (struct Block_Node_Type*)Pointer_Next_Block_Node->Pointer_Next_Block; printf("\n"); } } if ((struct Block_Node_Type*)Pointer_Current_Node) { printf("[%d] ", Pointer_Current_Node->Key); Pointer_Current_Node = (struct Block_Node_Type*)Pointer_Current_Node->Pointer_Next; } } printf("\n"); return; } | cs |
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 | #include <stdio.h> #include <stdlib.h> #include <time.h> #include "block list.h" #define DIVISION 10 int main(int argc, char*argv[]) { typedef struct Linked_Block_Type Linked_Block; typedef struct Block_Node_Type Block_Node; Linked_Block* Pointer_List = (Linked_Block*)Create_Linked_Block(DIVISION); if (!(Linked_Block*)Pointer_List) { return -1; } Block_Node node = { 0, }; srand((unsigned)time(NULL)); int loop = 0, count = 0; for (loop = 0; loop < 100; loop++) { node.Key = rand() % 100; Insert_Linked_Block_Element((Linked_Block*)Pointer_List, node); } Display_Linked_Block((Linked_Block*)Pointer_List); //Check_Tool((Linked_Block*)Pointer_List); for (loop = 300; loop; loop--) { int Random_Number = rand() % 100; count += Remove_Linked_Block_Element((Linked_Block*)Pointer_List, Random_Number); //Check_Tool((Linked_Block*)Pointer_List); } Display_Linked_Block((Linked_Block*)Pointer_List); printf("\n\ncount : %d\n", count); Check_Tool((Linked_Block*)Pointer_List); Delete_Linked_Block((Linked_Block*)Pointer_List); system("pause"); return 0; } | cs |
0 개의 댓글:
댓글 쓰기