質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

ハッシュ

ハッシュは、高速にデータ検索を行うアルゴリズムのことです。

Q&A

解決済

1回答

962閲覧

C言語でのチェイニング法によるハッシュの昇順にするには。

yukihirotahu

総合スコア8

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

プログラミング言語

プログラミング言語はパソコン上で実行することができるソースコードを記述する為に扱う言語の総称です。

ハッシュ

ハッシュは、高速にデータ検索を行うアルゴリズムのことです。

0グッド

0クリップ

投稿2020/07/09 11:38

C言語のチェイニング法でハッシュの問題なのですが、このコードを昇順に書き出すようにするにはどうすればよいのでしょうか?
ここのinsert部分を変えて昇順になるようにしたいのですがどうすればよいでしょうか?
いくつかやってみたのですが、できませんでした。どなたかお助けください、、

C言語

1#include <stdio.h> 2#include <stdlib.h> 3#define m 12 /* 配列サイズ*/ 4 5 6/* リストの基本操作 */ 7 8struct element { 9 int data; 10 struct element *next; 11}; 12 13struct element *newl() /* メモリー上にセルの領域を確保 */ 14{ 15 return((struct element *) malloc(sizeof(struct element))); 16} 17 18struct element *create() /* 空リストを生成 */ 19{ 20 struct element *p; 21 22 p = newl(); /* create(1) */ 23 p->next = NULL; /* create(2) */ 24 return(p); /* create(3) */ 25} 26 27void insert(struct element *list, int k, int x) /* リストのk番目にxを挿入 */ 28{ 29 struct element *p; 30 31 if (k > 1) /* insert(1) */ 32 insert(list->next, k-1, x); /* insert(2) */ 33 else { /* insert(3) */ 34 p = newl(); /* insert(4) */ 35 p->data = x; /* insert(5) */ 36 p->next = list->next; /* insert(6) */ 37 list->next = p; /* insert(7) */ 38 } 39} 40 41int member(struct element *list, int x) /* リスト中にxがあるかを判定 */ 42{ 43 if(list->next == NULL) /* member(1) */ 44 return 0; /* member(2) */ 45 if(list->next->data == x) /* member(3) */ 46 return 1; /* member(4) */ 47 else /* member(5) */ 48 member(list->next, x); /* member(6) */ 49} 50 51void deleteall(struct element *list, int x) /* リストから整数xをすべて削除する*/ 52{ 53 if (list->next == NULL) /* deleteall(1) */ 54 return; /* deleteall(2) */ 55 if (list->next->data == x){ /* deleteall(3) */ 56 list->next = list->next->next; /* deleteall(4) */ 57 deleteall(list, x); /* deleteall(5) */ 58 } 59 else /* deleteall(6) */ 60 deleteall(list->next, x); /* deleteall(7) */ 61} 62 63void printlist(struct element *list) /* リスト内のデータををすべて印刷する*/ 64{ 65 if(list->next != NULL){ /* printlist(1) */ 66 printf(" %d", list->next->data); /* printlist(2) */ 67 printlist(list->next); /* printlist(3) */ 68 } 69} 70 71 72 73/* ハッシュ表の基本操作 */ 74 75int hash(int x) /* ハッシュ関数 */ 76{ 77 return(x % m); /* hash(1) */ 78} 79 80void initializehash(struct element *a[]) /* ハッシュ表の初期化 */ 81{ 82 int i; /* initializehash(1) */ 83 84 for (i = 0;i <= m-1; i++) /* initializehash(2) */ 85 a[i] = create(); /* initializehash(3) */ 86} 87 88void inserthash(struct element *a[], int x) /* ハッシュ表にxを挿入 */ 89{ 90 insert(a[hash(x)],1,x); /* inserthash(1) */ 91} 92 93void deletehash(struct element *a[], int x) /* ハッシュ表からxを削除する */ 94{ 95 int i; 96 97 for (i = 0; i < m; i++) /* deletehash(1) */ 98 deleteall(a[i], x); /* deletehash(2) */ 99} 100 101int memberhash(struct element *a[], int x) /* ハッシュ表にxがあるかを判定 */ 102{ 103 return(member(a[hash(x)], x)); /* memberhash(1) */ 104} 105 106void printhash(struct element *a[]) /* ハッシュ表を印刷する */ 107{ 108 int i; 109 for (i = 0; i < m; i++){ /* printhash(1) */ 110 printf("H[%d",i); /* printhash(2) */ 111 printf("] = "); /* printhash(3) */ 112 printlist(a[i]); /* printhash(4) */ 113 printf("\n"); /* printhash(5) */ 114 } 115} 116 117 118int main() 119{ 120 int x; 121 struct element *a[m]; 122 123 initializehash(a); /* main(1) */ 124 printf("Next integer(0:end) = "); /* main(2) */ 125 scanf("%d", &x); /* main(3) コンソールから整数xを読みこむ */ 126 while (x != 0) { /* main(4) */ 127 inserthash(a, x); /* main(5) ハッシュ表に挿入する */ 128 printf("Next integer(0:end) = "); /* main(6) */ 129 scanf(" %d", &x); /* main(7) */ 130 } 131 printhash(a); /* main(8) */ 132 133 printf("Member integer(0:end) = "); /* main(9) */ 134 scanf("%d", &x); /* main(10) コンソールから整数xを読みこむ */ 135 while (x != 0) { /* main(11) */ 136 printf("%d", memberhash(a, x)); /* main(12) 1:ハッシュ表にxがある 0:ない */ 137 printf("\nMember integer(0:end) = "); /* main(13) */ 138 scanf(" %d", &x); /* main(14) */ 139 } 140 printhash(a); /* main(15) */ 141 printf("Delete integer(0:end) = "); /* main(16) */ 142 scanf("%d", &x); /* main(17) コンソールから整数xを読みこむ */ 143 while (x != 0) { /* main(18) */ 144 deletehash(a, x); /* main(19) ハッシュ表からxを削除する */ 145 printhash(a); /* main(20) */ 146 printf("Delete integer(0:end) = "); /* main(21) */ 147 scanf(" %d", &x); /* main(22) */ 148 } 149} 150

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ハッシュ部は省略。

C

1#include <stdio.h> 2#include <stdlib.h> 3 4/* リストの基本操作 */ 5 6struct element { 7 int data; 8 struct element* next; 9}; 10 11/* メモリー上にセルの領域を確保 */ 12struct element* create_element(int value, struct element* nxt) { 13 struct element* p = (struct element*)malloc(sizeof(struct element)); 14 if ( p != NULL ) { 15 p->data = value; 16 p->next = nxt; 17 } 18 return p; 19} 20 21/* 昇順となる位置に elementを挿入し、 22 新たな先頭elementを返す */ 23struct element* insert(struct element* list, int x) { 24 struct element* prev = NULL; 25 struct element* curr = list; 26 27 // 挿入位置を探す 28 while ( curr != NULL && curr->data < x ) { 29 prev = curr; 30 curr = curr->next; 31 } 32 // elementを挿入する 33 if ( prev == NULL ) { 34 return create_element(x, list); 35 } else { 36 prev->next = create_element(x, curr); 37 return list; 38 } 39} 40 41/* リスト内のデータををすべて印刷する*/ 42void printlist(struct element* list) { 43 while ( list != NULL ) { 44 printf(" %d", list->data); 45 list = list->next; 46 } 47 printf("\n"); 48} 49 50int main() { 51 struct element* list = NULL; 52 int values[] = { 1, 3, 5, 7, 9, 8, 6, 4, 2, 0, 0, 1, 2, 3, 4 }; 53 int i; 54 for ( i = 0; i < 15; ++i ) { 55 list = insert(list, values[i]); 56 printf("%d : ", values[i]); 57 printlist(list); 58 } 59 return 0; 60}

投稿2020/07/10 02:01

episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問