質問編集履歴

5

ソースファイルの修正です

2021/11/01 05:23

投稿

Mellonn
Mellonn

スコア3

test CHANGED
File without changes
test CHANGED
@@ -26,82 +26,6 @@
26
26
 
27
27
  ```
28
28
 
29
- cc -c -o open_addressing.o open_addressing.c
30
-
31
- open_addressing.c: In function ‘display’:
32
-
33
- open_addressing.c:111:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
34
-
35
- 111 | printf(nchr);
36
-
37
- | ^~~~
38
-
39
- | |
40
-
41
- | unsigned char
42
-
43
- In file included from open_addressing.c:1:
44
-
45
- /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
46
-
47
- 332 | extern int printf (const char *__restrict __format, ...);
48
-
49
- | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
50
-
51
- open_addressing.c:111:5: warning: format not a string literal and no format arguments [-Wformat-security]
52
-
53
- 111 | printf(nchr);
54
-
55
- | ^~~~~~
56
-
57
- open_addressing.c:115:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
58
-
59
- 115 | printf(nchr);
60
-
61
- | ^~~~
62
-
63
- | |
64
-
65
- | unsigned char
66
-
67
- In file included from open_addressing.c:1:
68
-
69
- /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
70
-
71
- 332 | extern int printf (const char *__restrict __format, ...);
72
-
73
- | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
74
-
75
- open_addressing.c:115:5: warning: format not a string literal and no format arguments [-Wformat-security]
76
-
77
- 115 | printf(nchr);
78
-
79
- | ^~~~~~
80
-
81
- open_addressing.c:117:22: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
82
-
83
- 117 | printf(dict->H[i].name);
84
-
85
- | ~~~~~~~~~~^~~~~
86
-
87
- | |
88
-
89
- | int
90
-
91
- In file included from open_addressing.c:1:
92
-
93
- /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int’
94
-
95
- 332 | extern int printf (const char *__restrict __format, ...);
96
-
97
- | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
98
-
99
- open_addressing.c:117:5: warning: format not a string literal and no format arguments [-Wformat-security]
100
-
101
- 117 | printf(dict->H[i].name);
102
-
103
-
104
-
105
29
  $ ./a.out
106
30
 
107
31
  Breakpoint 1, main () at main_open_addressing.c:5
@@ -180,7 +104,7 @@
180
104
 
181
105
  printf("Dictionary created!\n");
182
106
 
183
-
107
+
184
108
 
185
109
  return dictopenaddr;
186
110
 
@@ -206,7 +130,9 @@
206
130
 
207
131
 
208
132
 
209
- void insert_hash(DictOpenAddr *dict, int d){
133
+ void insert_hash(DictOpenAddr *dict, int d)
134
+
135
+ {
210
136
 
211
137
  if (search_hash(dict,d) != -1 )
212
138
 
@@ -224,17 +150,73 @@
224
150
 
225
151
  int init_b = b;
226
152
 
153
+
154
+
155
+ while (b != init_b){
156
+
157
+ if (dict->H[b].state == EMPTY || dict->H[b].state == DELETED)
158
+
159
+ {
160
+
161
+ dict->H[b].name = d;
162
+
227
- DictData targetdata = (dict->H)[b];
163
+ dict->H[b].state = OCCUPIED;
164
+
165
+ }
166
+
167
+
168
+
169
+ count += 1;
170
+
171
+ b = h(dict,d,count);
172
+
173
+ }
228
174
 
229
175
 
230
176
 
231
- if (targetdata.state == EMPTY || targetdata.state == DELETED)
177
+ fprintf(stderr, "Cannot be inserted.\n");
178
+
232
-
179
+ }
180
+
181
+
182
+
183
+ int search_hash(DictOpenAddr *dict, int d)
184
+
233
- {
185
+ {
234
-
186
+
235
- targetdata.name = d;
187
+ int count = 0;
188
+
236
-
189
+ int b = h(dict,d,count);
190
+
191
+ int init_b = b;
192
+
193
+
194
+
195
+ while (b != init_b){
196
+
237
- targetdata.state = OCCUPIED;
197
+ if (dict->H[b].state == OCCUPIED)
198
+
199
+ {
200
+
201
+ if (dict->H[b].name == d)
202
+
203
+ {
204
+
205
+ return b;
206
+
207
+ }
208
+
209
+
210
+
211
+ }
212
+
213
+
214
+
215
+ else if ((dict->H)[b].state == EMPTY)
216
+
217
+ {
218
+
219
+ return -1;
238
220
 
239
221
  }
240
222
 
@@ -242,127 +224,65 @@
242
224
 
243
225
  count += 1;
244
226
 
227
+
228
+
245
229
  b = h(dict,d,count);
246
230
 
247
-
248
-
249
- while (b != init_b)
250
-
251
- {
252
-
253
- return ;
254
-
255
- }
256
-
257
- }
258
-
259
-
260
-
261
- int search_hash(DictOpenAddr *dict, int d){
262
-
263
- int count = 0;
264
-
265
- int b = h(dict,d,count);
266
-
267
- int init_b = b;
268
-
269
231
 
270
232
 
271
- if ((dict->H)[b].state == OCCUPIED)
272
-
273
- {
274
-
275
- if ((dict->H)[b].name == d)
276
-
277
- {
278
-
279
- return b;
280
-
281
- }
282
-
283
233
 
284
234
 
285
235
  }
286
236
 
287
-
288
-
289
- else if ((dict->H)[b].state == EMPTY)
290
-
291
- {
292
-
293
237
  return -1;
294
238
 
295
- }
296
-
297
-
298
-
299
- count = count + 1;
300
-
301
- b = h(dict,d,count);
302
-
303
-
304
-
305
- while (b != init_b)
306
-
307
- {
308
-
309
- return -1;
310
-
311
239
  }
312
240
 
313
- }
241
+
314
-
315
-
316
-
242
+
317
- void delete_hash(DictOpenAddr *dict, int d){
243
+ void delete_hash(DictOpenAddr *dict, int d)
244
+
245
+ {
318
246
 
319
247
  int b = search_hash(dict,d);
320
248
 
321
- DictData targetdata = (dict->H)[b];
322
-
323
249
  if (b != -1)
324
250
 
325
251
  {
326
252
 
327
- targetdata.state = DELETED;
253
+ (dict->H)[b].state = DELETED;
328
-
254
+
329
- }
255
+ }
330
-
256
+
331
- }
257
+ }
332
-
333
-
334
-
258
+
259
+
260
+
335
- void display(DictOpenAddr *dict){
261
+ void display(DictOpenAddr *dict)
336
-
262
+
337
- int len = dict->B;
263
+ {
338
264
 
339
265
  int i = 0;
340
266
 
341
- char *newDict;
267
+
342
-
343
-
344
-
345
- newDict = (char *)malloc(0*len);
268
+
346
-
347
-
348
-
349
- while (i<len){
269
+ while (i<dict->B){
350
-
270
+
351
- switch ((dict->H)[i].state)
271
+ switch (dict->H[i].state)
352
272
 
353
273
  {
354
274
 
355
275
  case EMPTY:
356
276
 
357
- newDict[i] = 'e';
277
+ printf("e");
358
278
 
359
279
  case DELETED:
360
280
 
361
- newDict[i] = 'd';
281
+ printf("d");
362
282
 
363
283
  case OCCUPIED:
364
284
 
365
- sprintf(newDict[i],"%d",(dict->H)[i].name);
285
+ printf("%d",dict->H[i].name);
366
286
 
367
287
  }
368
288
 
@@ -370,10 +290,6 @@
370
290
 
371
291
  }
372
292
 
373
- printf(newDict);
374
-
375
- free(newDict);
376
-
377
293
  }
378
294
 
379
295
 

4

実行時の結果の修正

2021/11/01 05:23

投稿

Mellonn
Mellonn

スコア3

test CHANGED
File without changes
test CHANGED
@@ -26,6 +26,84 @@
26
26
 
27
27
  ```
28
28
 
29
+ cc -c -o open_addressing.o open_addressing.c
30
+
31
+ open_addressing.c: In function ‘display’:
32
+
33
+ open_addressing.c:111:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
34
+
35
+ 111 | printf(nchr);
36
+
37
+ | ^~~~
38
+
39
+ | |
40
+
41
+ | unsigned char
42
+
43
+ In file included from open_addressing.c:1:
44
+
45
+ /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
46
+
47
+ 332 | extern int printf (const char *__restrict __format, ...);
48
+
49
+ | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
50
+
51
+ open_addressing.c:111:5: warning: format not a string literal and no format arguments [-Wformat-security]
52
+
53
+ 111 | printf(nchr);
54
+
55
+ | ^~~~~~
56
+
57
+ open_addressing.c:115:12: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
58
+
59
+ 115 | printf(nchr);
60
+
61
+ | ^~~~
62
+
63
+ | |
64
+
65
+ | unsigned char
66
+
67
+ In file included from open_addressing.c:1:
68
+
69
+ /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘unsigned char’
70
+
71
+ 332 | extern int printf (const char *__restrict __format, ...);
72
+
73
+ | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
74
+
75
+ open_addressing.c:115:5: warning: format not a string literal and no format arguments [-Wformat-security]
76
+
77
+ 115 | printf(nchr);
78
+
79
+ | ^~~~~~
80
+
81
+ open_addressing.c:117:22: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [-Wint-conversion]
82
+
83
+ 117 | printf(dict->H[i].name);
84
+
85
+ | ~~~~~~~~~~^~~~~
86
+
87
+ | |
88
+
89
+ | int
90
+
91
+ In file included from open_addressing.c:1:
92
+
93
+ /usr/include/stdio.h:332:43: note: expected ‘const char * restrict’ but argument is of type ‘int’
94
+
95
+ 332 | extern int printf (const char *__restrict __format, ...);
96
+
97
+ | ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
98
+
99
+ open_addressing.c:117:5: warning: format not a string literal and no format arguments [-Wformat-security]
100
+
101
+ 117 | printf(dict->H[i].name);
102
+
103
+
104
+
105
+ $ ./a.out
106
+
29
107
  Breakpoint 1, main () at main_open_addressing.c:5
30
108
 
31
109
  5 int main(void) {
@@ -36,11 +114,11 @@
36
114
 
37
115
  (gdb) step
38
116
 
39
- create_dict (len=32767) at open_addressing.c:6
117
+ create_dict (len=0) at open_addressing.c:7
40
118
 
41
119
  warning: Source file is more recent than executable.
42
120
 
43
- 6 DictOpenAddr *create_dict(int len)
121
+ 7 {
44
122
 
45
123
  (gdb) step
46
124
 
@@ -48,24 +126,10 @@
48
126
 
49
127
  (gdb) step
50
128
 
51
- 9
52
-
53
- (gdb) step
129
+ Dictionary created!
54
-
55
- 13 for (int i=0; i < len; i++)
56
-
57
- (gdb) step
58
-
59
-
60
130
 
61
131
  Program received signal SIGSEGV, Segmentation fault.
62
132
 
63
- 0x00007fffff6452b4 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
64
-
65
- f=<optimized out>) at genops.c:394
66
-
67
- 394 genops.c: No such file or directory.
68
-
69
133
  ```
70
134
 
71
135
 

3

コードを全て載せました

2021/11/01 03:03

投稿

Mellonn
Mellonn

スコア3

test CHANGED
File without changes
test CHANGED
@@ -122,7 +122,205 @@
122
122
 
123
123
  }
124
124
 
125
+
126
+
127
+ int h(DictOpenAddr *dict, int d, int count){
128
+
129
+
130
+
131
+ int hash = d % (dict->B);
132
+
133
+
134
+
135
+ hash = (hash + count) % (dict->B);
136
+
137
+
138
+
139
+ return hash;
140
+
141
+ }
142
+
143
+
144
+
145
+ void insert_hash(DictOpenAddr *dict, int d){
146
+
147
+ if (search_hash(dict,d) != -1 )
148
+
149
+ {
150
+
151
+ return ;
152
+
153
+ }
154
+
155
+
156
+
157
+ int count = 0;
158
+
159
+ int b = h(dict,d,count);
160
+
161
+ int init_b = b;
162
+
163
+ DictData targetdata = (dict->H)[b];
164
+
165
+
166
+
167
+ if (targetdata.state == EMPTY || targetdata.state == DELETED)
168
+
169
+ {
170
+
171
+ targetdata.name = d;
172
+
173
+ targetdata.state = OCCUPIED;
174
+
175
+ }
176
+
177
+
178
+
179
+ count += 1;
180
+
181
+ b = h(dict,d,count);
182
+
183
+
184
+
185
+ while (b != init_b)
186
+
187
+ {
188
+
189
+ return ;
190
+
191
+ }
192
+
193
+ }
194
+
195
+
196
+
197
+ int search_hash(DictOpenAddr *dict, int d){
198
+
199
+ int count = 0;
200
+
201
+ int b = h(dict,d,count);
202
+
203
+ int init_b = b;
204
+
205
+
206
+
207
+ if ((dict->H)[b].state == OCCUPIED)
208
+
209
+ {
210
+
211
+ if ((dict->H)[b].name == d)
212
+
213
+ {
214
+
215
+ return b;
216
+
217
+ }
218
+
219
+
220
+
221
+ }
222
+
223
+
224
+
225
+ else if ((dict->H)[b].state == EMPTY)
226
+
227
+ {
228
+
229
+ return -1;
230
+
231
+ }
232
+
233
+
234
+
235
+ count = count + 1;
236
+
237
+ b = h(dict,d,count);
238
+
239
+
240
+
241
+ while (b != init_b)
242
+
243
+ {
244
+
245
+ return -1;
246
+
247
+ }
248
+
249
+ }
250
+
251
+
252
+
253
+ void delete_hash(DictOpenAddr *dict, int d){
254
+
255
+ int b = search_hash(dict,d);
256
+
257
+ DictData targetdata = (dict->H)[b];
258
+
259
+ if (b != -1)
260
+
261
+ {
262
+
263
+ targetdata.state = DELETED;
264
+
265
+ }
266
+
267
+ }
268
+
269
+
270
+
271
+ void display(DictOpenAddr *dict){
272
+
273
+ int len = dict->B;
274
+
275
+ int i = 0;
276
+
277
+ char *newDict;
278
+
279
+
280
+
281
+ newDict = (char *)malloc(0*len);
282
+
283
+
284
+
285
+ while (i<len){
286
+
287
+ switch ((dict->H)[i].state)
288
+
289
+ {
290
+
291
+ case EMPTY:
292
+
293
+ newDict[i] = 'e';
294
+
295
+ case DELETED:
296
+
297
+ newDict[i] = 'd';
298
+
299
+ case OCCUPIED:
300
+
301
+ sprintf(newDict[i],"%d",(dict->H)[i].name);
302
+
303
+ }
304
+
125
- (略)
305
+ i++;
306
+
307
+ }
308
+
309
+ printf(newDict);
310
+
311
+ free(newDict);
312
+
313
+ }
314
+
315
+
316
+
317
+ void delete_dict(DictOpenAddr *dict){
318
+
319
+ free(dict);
320
+
321
+ printf("Dictionary deleted!");
322
+
323
+ }
126
324
 
127
325
  ```
128
326
 
@@ -226,9 +424,35 @@
226
424
 
227
425
  display(test_dict);
228
426
 
427
+
428
+
229
-
429
+ printf("Search 1 ...\t%d\n", search_hash(test_dict, 1));
430
+
230
-
431
+ printf("Search 2 ...\t%d\n", search_hash(test_dict, 2));
432
+
433
+ printf("Search 21 ...\t%d\n", search_hash(test_dict, 21));
434
+
435
+ printf("Search 5 ...\t%d\n", search_hash(test_dict, 5));
436
+
437
+
438
+
439
+ delete_hash(test_dict, 3);
440
+
231
-
441
+ display(test_dict);
442
+
443
+
444
+
445
+ delete_hash(test_dict, 11);
446
+
447
+ display(test_dict);
448
+
449
+
450
+
451
+ delete_dict(test_dict);
452
+
453
+
454
+
455
+ return EXIT_SUCCESS;
232
456
 
233
457
  }
234
458
 

2

create_dictの修正とエラーメッセージを更新しました

2021/10/31 23:42

投稿

Mellonn
Mellonn

スコア3

test CHANGED
File without changes
test CHANGED
@@ -40,19 +40,19 @@
40
40
 
41
41
  warning: Source file is more recent than executable.
42
42
 
43
- 6 DictOpenAddr *create_dict(int len){
43
+ 6 DictOpenAddr *create_dict(int len)
44
-
44
+
45
- (gdb) step
45
+ (gdb) step
46
-
46
+
47
- 8 DictElement.name = 0;
47
+ 8 DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr));
48
-
48
+
49
- (gdb) step
49
+ (gdb) step
50
-
50
+
51
- 9 DictElement.state = EMPTY;
51
+ 9
52
-
52
+
53
- (gdb) step
53
+ (gdb) step
54
-
54
+
55
- 13 DDp = (DictData *)malloc(len * sizeof(DictData));
55
+ 13 for (int i=0; i < len; i++)
56
56
 
57
57
  (gdb) step
58
58
 
@@ -88,49 +88,37 @@
88
88
 
89
89
 
90
90
 
91
- DictOpenAddr *create_dict(int len){
91
+ DictOpenAddr *create_dict(int len)
92
+
92
-
93
+ {
94
+
95
+ DictOpenAddr *dictopenaddr = (DictOpenAddr *)malloc(sizeof(DictOpenAddr));
96
+
97
+
98
+
93
- DictData DictElement;//DictData型のDictElementの定義
99
+ dictopenaddr->H = (DictData *)malloc(sizeof(DictData) * len);
94
-
95
- DictElement.name = 0;//格納するデータ部の初期化
100
+
96
-
97
- DictElement.state = EMPTY;//空の状態
101
+ dictopenaddr->B = len;
98
-
99
-
100
-
101
- DictData *DDp; //DictData型の配列を表すポインタ変数
102
+
102
-
103
-
104
-
105
- DDp = (DictData *)malloc(len * sizeof(DictData));//データの要素分の(len個)DictDataの主記憶領域の動的確保
103
+
106
-
107
-
108
-
104
+
109
- for (int i=0;i<len;i++)
105
+ for (int i=0; i < len; i++)
110
106
 
111
107
  {
112
108
 
109
+ dictopenaddr->H[i].name = -1;
110
+
113
- DDp[i] = DictElement;//DDpの配列にDictElementをlen個分格納
111
+ dictopenaddr->H[i].state = EMPTY;
114
112
 
115
113
  }
116
114
 
117
115
 
118
116
 
119
- DictOpenAddr *dictopenaddr;//DictOpenAddr型の変数の宣言
120
-
121
-
122
-
123
- dictopenaddr->H = DDp;//メンバHにDDpを格納
124
-
125
- dictopenaddr->B = len;//メンバBにlenを格納
126
-
127
-
128
-
129
- printf("Dictionary created!");
117
+ printf("Dictionary created!\n");
130
-
131
-
132
-
118
+
119
+
120
+
133
- return dictopenaddr;DictOpenAddr型のポインタを返す
121
+ return dictopenaddr;
134
122
 
135
123
  }
136
124
 

1

関数名の訂正

2021/10/31 23:35

投稿

Mellonn
Mellonn

スコア3

test CHANGED
File without changes
test CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
 
14
14
 
15
- open_addressing.cのDictOpenAddr関数に問題があることはわかるのですが,具体的な箇所がわからず困っています.
15
+ open_addressing.cのcreate_dict関数に問題があることはわかるのですが,具体的な箇所がわからず困っています.
16
16
 
17
17
 
18
18