回答編集履歴

2

修正個所の表示を変更しました。

2021/01/29 04:24

投稿

tatsu99
tatsu99

スコア5438

test CHANGED
@@ -400,135 +400,179 @@
400
400
 
401
401
  ```diff
402
402
 
403
- 32c32,33
404
-
405
- < if(hash->table=calloc(n,sizeof(HashRecord))==NULL){
406
-
407
- ---
408
-
409
- > hash->table = calloc(n,sizeof(HashRecord));
410
-
411
- > if (hash->table == NULL){
412
-
413
- 36d36
414
-
415
- < hash->table->state=EMPTY;
416
-
417
- 37a38,41
418
-
419
- > int i;
420
-
421
- > for (i = 0; i < n; i++){
422
-
423
- > hash->table[i].state=EMPTY;
424
-
425
- > }
426
-
427
- 50,54c54,57
428
-
429
- < int len =0;
430
-
431
- < while(s[len]) len++;
432
-
433
- < return len;
434
-
435
- < }
436
-
437
- <
438
-
439
- ---
440
-
441
- > int len =0;
442
-
443
- > while(s[len]) len++;
444
-
445
- > return len;
446
-
447
- > }
448
-
449
- 57,62c60,68
450
-
451
- < if(strLen(key)==0){return 0;}
452
-
453
- < else if(strLen(key)==1){
454
-
455
- < return key[0] % hash->size -8;
456
-
457
- < }else{
458
-
459
- < int n = strLen(key);
460
-
461
- < int x = (key[0] - 65 + 26* (key[n/2 -1] - 65) + 26 * 26 * (key[n-2] - 65 )) % hash->size;
462
-
463
- ---
464
-
465
- > int x;
466
-
467
- > if(strLen(key)==0){return 0;}
468
-
469
- > else if(strLen(key)==1){
470
-
471
- > x = key[0] % hash->size -8;
472
-
473
- > if (x < 0) x = 0;
474
-
475
- > }else{
476
-
477
- > int n = strLen(key);
478
-
479
- > x = (key[0] - 65 + 26* (key[n/2 -1] - 65) + 26 * 26 * (key[n-2] - 65 )) % hash->size;
480
-
481
- > }
482
-
483
- 65d70
484
-
485
- < }
486
-
487
- 71c76,77
488
-
489
- < *p = hash->table[h];
490
-
491
- ---
492
-
493
- > printf("hash-code=%d\n",h);
494
-
495
- > p = &hash->table[h];
496
-
497
- 75,76c81,82
498
-
499
- < *p->key = key;
500
-
501
- < *p->value = value;
502
-
503
- ---
504
-
505
- > strcpy(p->key,key);
506
-
507
- > strcpy(p->value,value);
508
-
509
- 86,87c92,93
510
-
511
- < *p = hash->table[h];
512
-
513
- < if(p->state == EMPTY && p->key != key){
514
-
515
- ---
516
-
517
- > p = &hash->table[h];
518
-
519
- > if(p->state == EMPTY || strcmp(p->key,key) != 0){
520
-
521
- 90,91c96,97
522
-
523
- < value = p->value;
524
-
525
- < return FALSE;
526
-
527
- ---
528
-
529
- > strcpy(value,p->value);
530
-
531
- > return TRUE;
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

追記しました。

2021/01/29 04:24

投稿

tatsu99
tatsu99

スコア5438

test CHANGED
@@ -1,4 +1,4 @@
1
- とりあえず、動くようにしておきました。
1
+ とりあえず、動くようにしておきました。不明点があれば、質問してください。
2
2
 
3
3
  ```C
4
4