前提
C言語でハッシュを用いた、プログラムを作っています。
発生している問題
コンパイラは通りますが、実行中に動作が停止します。
空のハッシュテーブルに連結リストを入れるときは、動作は停止しませんが。
連結リストが存在するハッシュテーブルに連結リストを入れようとすると、動作が停止します。
何が問題で、どうやって解決すればよろしいでしょうか?
該当のソースコード
C言語
#include <stdio.h> #include <stdlib.h> #include <string.h> #define HASHSIZE 17 struct match *hashtable[HASHSIZE]; struct match_score{ int year; int month; int day; int home_score; int away_score; struct match_score *next; } ; struct match{ char *home; char *away; struct match_score *r; struct match *next; } ; int hash(char *home, char *away); struct match *make_match(char *home, char *away, struct match_score *r, struct match *next); struct match_score *make_match_score(int year, int month, int day, int home_score, int away_score, struct match_score *next); void add(int year, int month, int day, char *home, int home_score, int away_score, char *away); struct match *find(char *home, char *away); int main(void) { for(int i=0; i < HASHSIZE; i++){ hashtable[i] = NULL; } printf("%d\n",hash("Kashima_Antler", "Yokohama_Flugels")); add(1992, 9, 5, "Kashima_Antler", 4, 2, "Yokohama_Flugels"); printf("%d\n",hashtable[2]->r->year); add(1996, 5, 12, "mashima_Antler", 3, 1, "Yokohama_Flugels"); printf("%d\n",hashtable[2]->r->year); return 0; } int hash(char *home, char *away) { int hashval = 0; int i = 0; while(home[i] != '\0') { hashval += home[i]; i++; } while(away[i] != '\0') { hashval += away[i]; i++; } return hashval % HASHSIZE; } struct match_score *make_match_score(int year, int month, int day, int home_score, int away_score, struct match_score *next) { struct match_score *newmsp = malloc(sizeof(struct match)); if (newmsp != NULL) { newmsp->year = year; newmsp->month = month; newmsp->day = day; newmsp->home_score = home_score; newmsp->away_score = away_score; newmsp->next = next; } return newmsp; } struct match *make_match(char *home, char *away, struct match_score *r, struct match *next) { struct match *newmp = malloc(sizeof(struct match)); if (newmp != NULL) { newmp->home = (char*)malloc(sizeof(char) * sizeof(home)); newmp->away = (char*)malloc(sizeof(char) * sizeof(away)); strcpy(newmp->home, home); strcpy(newmp->away, away); newmp->r = r; newmp->next = next; } return newmp; } struct match *find(char *home, char *away) { struct match *p; int hashval = hash(home, away); p = hashtable[hashval]; while (p != NULL) { if (strcmp(home, p->home) == 0 && strcmp(away, p->away) == 0) { return p; } p = p->next; } return NULL; } void add(int year, int month, int day, char *home, int home_score, int away_score, char *away) { struct match *newmp = NULL; struct match_score *newmsp = NULL; int hashval = hash(home,away); struct match *pmatch = NULL; struct match *prematch = NULL; if (hashtable[hashval] == NULL) { newmsp = make_match_score(year, month, day, home_score, away_score, NULL); newmp = make_match(home, away, newmsp, hashtable[hashval]); hashtable[hashval] = newmp; return ; } struct match *fmp = find(home, away); if (fmp != NULL) { newmsp = make_match_score(year, month, day, home_score, away_score, fmp->r); fmp->r = newmsp; return ; } newmsp = make_match_score(year, month, day, home_score, away_score, NULL); newmp = make_match(home, away, newmsp, hashtable[hashval]); hashtable[hashval] = newmp; return ; }
補足情報(FW/ツールのバージョンなど)
開発環境 : VS Code (バージョン1.45.1)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/06 13:01