static void main(string[] args) { var pq = new PriorityQueue(5); print("Testing Priority Queue\n"); print("Inserting 1 (2)\n"); pq.insert(1, 2); print("Inserting 2 (4)\n"); pq.insert(2, 4); print("Inserting 3 (1)\n"); pq.insert(3, 1); print("Inserting 4 (3)\n"); pq.insert(4, 3); print(@"$(pq.remove())\n"); print(@"$(pq.remove())\n"); print(@"$(pq.remove())\n"); print(@"$(pq.remove())\n"); } class PriorityQueue { class QueueItem { internal QueueItem(void* v, int p) { value = v; prio = p; } internal void* value; internal int prio; } QueueItem[] priority_queue; int size {public get; private set; } public PriorityQueue(int n) { priority_queue = new QueueItem[n]; size = 0; } public void insert(T v, int priority = 1) { if (size >= priority_queue.length-2) { print("Overflow!\n"); return; } QueueItem qi = new QueueItem((void*)v, priority); size++; priority_queue[size/*++ <- this would cause a sefault */] = qi; print("Added value with Prioirity %d\n".printf(priority_queue[size].prio)); } public T remove() { if (size <= 0) { print("Underflow!\n"); return null; } int max = 1; for (int i = 2; i < size; i++) { if (priority_queue[i].prio > priority_queue[max].prio) max = i; } var qi = priority_queue[max]; priority_queue[max] = priority_queue[size]; priority_queue[size] = null; size--; return (T) qi.value; } }