0

プライオリティ キューを作成していますが、PQ 配列を整数で埋めるのではなく、ポインタを構造体に割り当てています。PQ の初期化子と挿入関数の 2 つの構造体のコードを次に示します。

typedef struct HeapStruct *PriorityQueue;
typedef struct Customer *CustomerP;

struct HeapStruct {
    int Capacity;
    int Size;
    int *Elements;
};

struct Customer {
    float arrivalT;
    float waitT;
    float departureT;
    float currentT;
    CustomerP *siblingR; //Used for linked list
};

PriorityQueue Initialize() {
    PriorityQueue H = (PriorityQueue) malloc(sizeof (struct HeapStruct));
    CustomerP sentinal = malloc(sizeof (struct Customer));
    sentinal->currentT = MinData;
    H->Capacity = 101;
    H->Size = 0;
    H->Elements[0] = &sentinal; //Syntax Error
    return H;
}

void Insert(CustomerP X, PriorityQueue H) {
    int i;
    if (IsFull(H)) {
        printf("Priority queue is full");
        return;
    }
    //Syntax errors
    for (i = ++H->Size; H->Elements[i/2]->currentT > X->currentT; i /= 2)
    H->Elements[i] = H->Elements[i/2];
    H->Elements[i] = X;
}

そのため、Int 配列にポインターを配置して、H->Elements[i]->currentT などの比較を実行しようとしていますが、配列内の構造体へのポインターを処理し、構造体にアクセスする方法がわかりません。そこの。

誰かがこれの構文で私を助けてくれますか? 必要に応じて、より多くの情報を喜んで提供します。

4

3 に答える 3

2

あなたはEementsになりたい

CustomerP*

H->Elements 次に、すべてのポインタを保持できるように、メモリを割り当てる必要があり ます。

多分 :-

H->Elements =  malloc(sizeof (CustomerP) * H->Capacity);
于 2012-09-30T18:36:57.110 に答える
2

ヒープに保存したいものに基づいて、ニーズのElementsフィールドを適切に定義する必要があります。HeapStruct次に、使用する前にメモリを割り当てる必要があります。

最初の質問は、ヒープに何が必要ですか? あなたは顧客 (intあなたが持っている s ではありません) が欲しいと言いますが、構造体自体 ( Customer) または構造体へのポインター (Customer *またはCustomerP)が欲しいですか? 後者が必要であると仮定します:

struct HeapStruct {
    int Capacity;
    int Size;
    CustomerP *Elements;
};

次に、適切にスペースを割り当てる必要があります。

H = (PriorityQueue) malloc(sizeof (struct HeapStruct));
CustomerP sentinal = malloc(sizeof (struct Customer));
sentinal->currentT = MinData;
H->Capacity = 101;
H->Size = 0;
H->Elements = malloc(sizeof(CustomerP) * H->Capacity);
H->Elements[0] = sentinal;
于 2012-09-30T19:12:08.853 に答える
0
H->Elements = (int *)malloc(sizeof(int));
H->Elements[0] = &sentinal;

または H->Elements = &sental;

どちらも機能するはずです。

于 2012-09-30T18:30:36.860 に答える