I am building a priority queue, but rather than filling the PQ array with integers I am assigning a pointer to a struct. Heres the code for the two structures, intializer and insertion function for PQ:
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;
}
So I am trying to place a pointer in the Int array and doing comparisons such as H->Elements[i]->currentT, but I don’t know how to handle a pointer to a struct in an array and access the struct from there.
Could anyone help me out with the syntax of this? I’ll gladly provide more info if needed.
The
Elementsfield in yourHeapStructneeds to be defined appropriately based on what you want to store in your heap. Then, you need to allocate memory for it before you use it.So the first question, what do want in the heap? You say you want customers (not
ints, which is what you have), but do you want the structs themselves (Customer) or pointers to the structs (Customer *orCustomerP). Assuming the latter you want:Then you need to allocate space for it properly: