こんにちは
現在、ハッシュテーブルを学んでいます。
Keyとvalueの値を並べて出力する時に、配列最後の要素だけコンマ", " をとって表示したい場合のコードですが、
このint flagの働きがよく理解できません。
出力結果:
{'Betty': 'Cool', 'python': 'awesome', 'Bob': 'and Kris love asm', '98': 'Battery Street', 'N': 'queens', 'c': 'fun', 'Asterix': 'Obelix'(←ここだけコンマがない)}
どなたか説明いただけないでしょうか?
void hash_table_print(const hash_table_t *ht) { unsigned long int index; unsigned long int flag; hash_node_t *temp; if (ht == NULL) return; printf("{"); for (index = 0; index < ht->size; index++) { temp = ht->array[index]; while (temp) { if (flag == 1) printf(", "); printf("'%s': '%s'", temp->key, temp->value); flag = 1; temp = temp->next; } } printf("}\n"); }
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ベストアンサー
コードをどう書くのかということであれば、実行できるコードをご提示頂いたほうが良いです。
最初に付けない場合でも最後に付けない場合でも、要するに if 文の条件をどうやって最初/最後と判定させるかということです。
フラグ変数を初期値 0 とし、一度でも表示したら 1 にすることで、最初か否かの判断が出来ます。
もし表示する件数が予め分かっていれば、表示した数をカウントして最後かどうかの判断が出来ます。
次のコードの hash_table_print2 では、"array のループの最後 かつ 次へのポインタが NULL " なら最後としています。
c
1#include <stdio.h> 2#include <stdlib.h> 3#include <string.h> 4 5typedef struct hash_node { 6 char *key; 7 char *value; 8 struct hash_node *next; 9} hash_node_t; 10 11typedef struct hash_table { 12 hash_node_t *table[256]; 13 int size; 14 hash_node_t **array; //有効データ 15} hash_table_t; 16 17int calcHash(char *key) { 18 return key[0]; //テキトウ 19} 20 21//最初の前に "," を付けない 22void hash_table_print(const hash_table_t *ht) { 23 if(ht == NULL) return; 24 25 printf("{"); 26 for(int i=0, flag=0; i<ht->size; i++) { 27 for(hash_node_t *node=ht->array[i]; node!=NULL; node=node->next) { 28 if(flag == 1) printf(", "); //最初だけ条件が偽になる 29 printf("'%s': '%s'", node->key, node->value); 30 flag = 1; 31 } 32 } 33 printf("}\n"); 34} 35 36//最後の後に "," を付けない 37void hash_table_print2(const hash_table_t *ht) { 38 if(ht == NULL) return; 39 40 printf("{"); 41 for(int i=0; i<ht->size; i++) { 42 for(hash_node_t *node=ht->array[i]; node!=NULL; node=node->next) { 43 printf("'%s': '%s'", node->key, node->value); 44 if(i < ht->size-1 || node->next != NULL) printf(", "); //最後だけ条件が偽になる 45 } 46 } 47 printf("}\n"); 48} 49 50int put(hash_table_t *ht, char *key, char *value) { 51 hash_node_t *node = malloc(sizeof(hash_node_t) + strlen(key)+1 + strlen(value)+1); 52 if(node == NULL) return -1; 53 node->key = (char*)node + sizeof(hash_node_t); 54 strcpy(node->key, key); 55 node->value = node->key + strlen(key)+1; 56 strcpy(node->value, value); 57 node->next = NULL; 58 59 int hash = calcHash(key); 60 if(ht->table[hash] == NULL) { 61 hash_node_t **a = realloc(ht->array, (ht->size+1) * sizeof(void*)); 62 if(a == NULL) return -1; 63 ht->array = a; 64 ht->array[ht->size++] = node; 65 66 ht->table[hash] = node; 67 return 0; 68 } 69 70 hash_node_t **pp = &ht->table[hash]; 71 while(*pp != NULL && strcmp((*pp)->key, node->key)) pp = &(*pp)->next; 72 if(*pp != NULL) { 73 node->next = (*pp)->next; 74 free(*pp); 75 } 76 *pp = node; 77 return 0; 78} 79 80int main(void) { 81 hash_table_t ht; 82 memset(&ht, 0, sizeof(hash_table_t)); 83 84 put(&ht, "Betty", "Cool"); 85 put(&ht, "python", "awesome"); 86 put(&ht, "Bob", "and Kris love asm"); 87 put(&ht, "98", "Battery Street"); 88 put(&ht, "N", "queens"); 89 put(&ht, "c", "fun"); 90 put(&ht, "Asterix", "Obelix"); 91 92 hash_table_print(&ht); 93 hash_table_print2(&ht); 94 95 return 0; 96}
実行結果 ( 実装が想像ですので並び順は無視してください。 )
plain
1{'Betty': 'Cool', 'Bob': 'and Kris love asm', 'python': 'awesome', '98': 'Battery Street', 'N': 'queens', 'c': 'fun', 'Asterix': 'Obelix'} 2{'Betty': 'Cool', 'Bob': 'and Kris love asm', 'python': 'awesome', '98': 'Battery Street', 'N': 'queens', 'c': 'fun', 'Asterix': 'Obelix'}
投稿2022/05/26 18:47
編集2022/05/26 18:53総合スコア13320
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

0
配列最後の要素だけコンマ", " をとって表示したい場合のコードですが
結果はそのようになりますが、コードの流れはそうではなく、「項目の前にコンマを打つけど、1回目だけはコンマを割愛」というようになっています。
投稿2022/05/26 14:02
総合スコア146550
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。