C言語の課題で悩んでいます。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
kkk

C言語の課題で悩んでいます。

#1

投稿記事 by kkk » 9年前

ハッシュ表に2次元平面上の1点を表現した構造体をデータとして登録する関数を作り、0~9999までの一様乱数を発生させ、それを構造体に代入し、得られた構造体を上記の関数を用いてハッシュ表に登録したいのですが、セグメンテーションフォルトが出てうまくいきません。いろいろと考えて試してみたのですが、解決できませんでした。
自分で考えたプログラムを載せておきます。
[code=c]#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define B 10000
#define A 100

typedef struct _point{
int x;
int y;
}POINT;

typedef struct h_cell{
POINT data;
struct h_cell *next;
}CELL;

CELL H;
CELL *head;


CELL insert_cell_top(POINT p,int a){
CELL *new_cell;

new_cell=(CELL*)malloc(sizeof(CELL));
new_cell->data=p;
new_cell->next=head[a];
head[a]=new_cell;

return *new_cell;
}

int h(POINT p){
return (p.x)*(p.y)%B;
}

int insert_hash(POINT p){
CELL *a;
int b=h(p);
H=*head;
a=&H;
while(a->next!=NULL){
if(a->data.x!=p.x && a->data.y!=p.y){
H=insert_cell_top(p,b);
a=a->next;
return 1;
}
else return 0;
}
return 0;
}

int search_hash(POINT p){
int b=h(p);
CELL *a;
a=&H;
while(a->next!=NULL){
if(a->data.x==p.x && a->data.y==p.y){
return 1;
a=a->next;
}else return 0;
}
return 0;
}

void hash_status(){
int i,j=0;
int max,min;
CELL *a;
for(i=0;i<B;i++){
a=&H;
while(a->next!=NULL)

printf("Number of H[%d]'s data=%d\n",i,j);
printf("H[%d]'smax=%d,min=%d\n",i,max,min);
}
}

int main(void){
int i,j,X;
srand((unsigned)time(NULL));
POINT a[A];
for(j=0;j<A;j++){
a[j].x=0;
a[j].y=0;
}
for(i=0;i<A;i++){
a.x=rand()%9999;
a.y=rand()%9999;
insert_hash(a);
}



return 0;
}





[/code]

kkk

Re: C言語の課題で悩んでいます。

#2

投稿記事 by kkk » 9年前

すいません。コード選択がうまくいっていなかったようなので再掲します。

コード:

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define B 10000
#define A 100

typedef struct _point{
  int x;
  int y;
}POINT;

typedef struct h_cell{
  POINT data;
  struct h_cell *next;
}CELL;

CELL H[B];
CELL *head[B];


CELL insert_cell_top(POINT p,int a){
  CELL *new_cell;

  new_cell=(CELL*)malloc(sizeof(CELL));
  new_cell->data=p;
  new_cell->next=head[a];
  head[a]=new_cell;

  return *new_cell;
}

int h(POINT p){  
  return (p.x)*(p.y)%B;
}

int insert_hash(POINT p){
  CELL *a;
  int b=h(p);
  H[b]=*head[b];
  a=&H[b];
  while(a->next!=NULL){
    if(a->data.x!=p.x && a->data.y!=p.y){
      H[b]=insert_cell_top(p,b);
      a=a->next;
      return 1;
    }
    else return 0;
  }
  return 0;
}
    
int search_hash(POINT p){
  int b=h(p);
  CELL *a;
  a=&H[b];
  while(a->next!=NULL){
    if(a->data.x==p.x && a->data.y==p.y){
      return 1;
      a=a->next;
    }else return 0;
  }
  return 0;
}

void hash_status(){
  int i,j=0;
  int max,min;
  CELL *a;
  for(i=0;i<B;i++){
    a=&H[i];
    while(a->next!=NULL)

    printf("Number of H[%d]'s data=%d\n",i,j);
    printf("H[%d]'smax=%d,min=%d\n",i,max,min);
  }
}

int main(void){
  int i,j,X;
  srand((unsigned)time(NULL));
  POINT a[A];
  for(j=0;j<A;j++){
    a[j].x=0;
    a[j].y=0;
  }
  for(i=0;i<A;i++){
    a[i].x=rand()%9999;
    a[i].y=rand()%9999;
    insert_hash(a[i]);
  }
  
 
  
  return 0;
}

閉鎖

“C言語何でも質問掲示板” へ戻る