キーと要素を持つ辞書を作成しました。それはキーフィールドによって順序付けられ、ディクショナリに割り当てられたスペースは、それが含む要素の数と常に等しくなければなりません。だから、これは私がやったことです:配列struct(Dictionary)をC言語で挿入
//Creating a Dictionary structure
typedef struct dict{
int elem;
int key;
}Dictionary;
int main(){
Dictionary * d = NULL;
int dim = 0;
insert(&d, 5, 3, &dim);
insert(&d, 10, 2, &dim);
insert(&d, 6, 1, &dim);
//insert(&d, 9, 6, &dim);
//insert(&d, 55, 2, &dim);
//insert(&d, 11, 5, &dim);
return 0;
}
void insert(Dictionary **d, int elem, int key, int *dim){
(*d) =(Dictionary *)realloc((*d),(*dim)++);//adding space for another element
int i = 0, j = 0;
while(i < (*dim) && (*d)[i].key < key)//searching for the corect position to insert
i++;
//sliding all the lements to the right
for(j = (*dim); j > i; j--){
(*d)[j] = (*d)[j - 1];
}
//iserting the element in the correct position
(*d)[i].elem = elem;
(*d)[i].key = key;
}
それが問題を挿入する最初の3つの要素のために、私は前後を挿入しようとwhenerver私はinfinteループおよび5のような失速を得るかということである必要がありますようにコードが動作しますより多くの挿入がプログラムをクラッシュさせます。だから誰かが私に何が行方不明か、何が間違っているのかを私に説明することができましたか?
'(* dim)++' ++は意味をなさない。それとは別に、_hash tables_を見てみましょう。これは、辞書に使用するのに適したADTであるためです。 – Lundin