I am working on a Binary Heap implementation of a Priority Queue. So far, I have the queue working for a single element. Here is some of the code:
Now, I'm working on a discrete event simulation and I want my queue to store events with the following data:Code:[...headers and constants...] struct HeapStruct { int Capacity; int Size; ElementType *Elements; }; PriorityQueue Initialize(int MaxElements) { PriorityQueue H; if (MaxElements < MinPQSize) printf("Priority queue size is too small.\n"); H = malloc(sizeof ( struct HeapStruct)); if (H == NULL) printf("Out of space!\n"); /* Allocate the array plus one extra for sentinel */ H->Elements = malloc((MaxElements + 1) * sizeof ( ElementType)); if (H->Elements == NULL) printf("Out of space!\n"); H->Capacity = MaxElements; H->Size = 0; H->Elements[ 0 ] = MinData; return H; } void Insert(ElementType X, PriorityQueue H) { int i; if (IsFull(H)) { printf("Priority queue is full.\n"); return; } for (i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2) H->Elements[ i ] = H->Elements[ i / 2 ]; H->Elements[ i ] = X; } ElementType FindMin(PriorityQueue H) { if (!IsEmpty(H)) return H->Elements[ 1 ]; printf("Priority Queue is Empty.\n"); return H->Elements[ 0 ]; } [...the rest of the priority queue methods...]
The priorities would be based on the event_time variable, so I would always dequeue the event with the smallest time.Code:int id; //To store a process id. int event_type; //Could be 1 or 0. double event_time; //Stores the time an event occurs. double run_time; //Stores the run time of an event.
So, that's where I'm stuck. How should I go about modifying my original priority queue implementation to work with the above-mentioned variables and so that it prioritizes based on event_time?
I feel like it's something very simple to do, but I just don't have enough experience in C to do so. I don't know if I can create an array of objects or something like what I could do in Java or Python. I hope I was clear enough.
Help is much appreciated.
PS: BTW, I know that ANSI-C is not object oriented at all. I was just saying that if it were Java, I would simply create an array of objects, but I don't know how to proceed here.



LinkBack URL
About LinkBacks



