回答編集履歴
2
修正個所の表示を変更しました。
test
CHANGED
@@ -400,135 +400,179 @@
|
|
400
400
|
|
401
401
|
```diff
|
402
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
|
-
|
403
|
+
@@ -29,12 +29,16 @@
|
404
|
+
|
405
|
+
Hash* HashAlloc(int n){
|
406
|
+
|
407
|
+
Hash* hash=malloc(sizeof(Hash));
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
- if(hash->table=calloc(n,sizeof(HashRecord))==NULL){
|
412
|
+
|
413
|
+
+ hash->table = calloc(n,sizeof(HashRecord));
|
414
|
+
|
415
|
+
+ if (hash->table == NULL){
|
416
|
+
|
417
|
+
free(hash);
|
418
|
+
|
419
|
+
return NULL;
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
- hash->table->state=EMPTY;
|
424
|
+
|
425
|
+
hash->size=n;
|
426
|
+
|
427
|
+
+ int i;
|
428
|
+
|
429
|
+
+ for (i = 0; i < n; i++){
|
430
|
+
|
431
|
+
+ hash->table[i].state=EMPTY;
|
432
|
+
|
433
|
+
+ }
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
return hash;
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
@@ -47,33 +51,35 @@
|
442
|
+
|
443
|
+
int HashCode(Hash *hash,char *key){
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
int strLen(const char s[]){
|
448
|
+
|
449
|
+
- int len =0;
|
450
|
+
|
451
|
+
- while(s[len]) len++;
|
452
|
+
|
453
|
+
- return len;
|
454
|
+
|
455
|
+
-}
|
456
|
+
|
457
|
+
-
|
458
|
+
|
459
|
+
+ int len =0;
|
460
|
+
|
461
|
+
+ while(s[len]) len++;
|
462
|
+
|
463
|
+
+ return len;
|
464
|
+
|
465
|
+
+ }
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
-if(strLen(key)==0){return 0;}
|
472
|
+
|
473
|
+
-else if(strLen(key)==1){
|
474
|
+
|
475
|
+
- return key[0] % hash->size -8;
|
476
|
+
|
477
|
+
-}else{
|
478
|
+
|
479
|
+
- int n = strLen(key);
|
480
|
+
|
481
|
+
- int x = (key[0] - 65 + 26* (key[n/2 -1] - 65) + 26 * 26 * (key[n-2] - 65 )) % hash->size;
|
482
|
+
|
483
|
+
+ int x;
|
484
|
+
|
485
|
+
+ if(strLen(key)==0){return 0;}
|
486
|
+
|
487
|
+
+ else if(strLen(key)==1){
|
488
|
+
|
489
|
+
+ x = key[0] % hash->size -8;
|
490
|
+
|
491
|
+
+ if (x < 0) x = 0;
|
492
|
+
|
493
|
+
+ }else{
|
494
|
+
|
495
|
+
+ int n = strLen(key);
|
496
|
+
|
497
|
+
+ x = (key[0] - 65 + 26* (key[n/2 -1] - 65) + 26 * 26 * (key[n-2] - 65 )) % hash->size;
|
498
|
+
|
499
|
+
+ }
|
500
|
+
|
501
|
+
return x;
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
-}
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
int HashAdd(Hash *hash, char *key, char *value){
|
510
|
+
|
511
|
+
int h=0;
|
512
|
+
|
513
|
+
HashRecord *p;
|
514
|
+
|
515
|
+
h=HashCode(hash,key);
|
516
|
+
|
517
|
+
- *p = hash->table[h];
|
518
|
+
|
519
|
+
+ printf("hash-code=%d\n",h);
|
520
|
+
|
521
|
+
+ p = &hash->table[h];
|
522
|
+
|
523
|
+
if(p->state == FULL){
|
524
|
+
|
525
|
+
return FALSE;
|
526
|
+
|
527
|
+
}
|
528
|
+
|
529
|
+
- *p->key = key;
|
530
|
+
|
531
|
+
- *p->value = value;
|
532
|
+
|
533
|
+
+ strcpy(p->key,key);
|
534
|
+
|
535
|
+
+ strcpy(p->value,value);
|
536
|
+
|
537
|
+
p->state =FULL;
|
538
|
+
|
539
|
+
return TRUE;
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
@@ -83,12 +89,12 @@
|
544
|
+
|
545
|
+
int h;
|
546
|
+
|
547
|
+
HashRecord *p;
|
548
|
+
|
549
|
+
h=HashCode(hash,key);
|
550
|
+
|
551
|
+
- *p = hash->table[h];
|
552
|
+
|
553
|
+
- if(p->state == EMPTY && p->key != key){
|
554
|
+
|
555
|
+
+ p = &hash->table[h];
|
556
|
+
|
557
|
+
+ if(p->state == EMPTY || strcmp(p->key,key) != 0){
|
558
|
+
|
559
|
+
return FALSE;
|
560
|
+
|
561
|
+
}
|
562
|
+
|
563
|
+
- value = p->value;
|
564
|
+
|
565
|
+
- return FALSE;
|
566
|
+
|
567
|
+
+ strcpy(value,p->value);
|
568
|
+
|
569
|
+
+ return TRUE;
|
570
|
+
|
571
|
+
}
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
// ハッシュテーブルのレコードを全て表示する(ハッシュが空でも表示).
|
532
576
|
|
533
577
|
|
534
578
|
|
1
追記しました。
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
とりあえず、動くようにしておきました。
|
1
|
+
とりあえず、動くようにしておきました。不明点があれば、質問してください。
|
2
2
|
|
3
3
|
```C
|
4
4
|
|