I have an assignment that asks me to implement a priority queue with binary heap in c. I have a problem because basically, the input data that we get looks like this:
7 - number of occurences
3 4 0 5 8 0 0 - occurences, if the number is 0 that means the number with highest priority is removed (or if priorities are the same, the one thats been there the longest - i guess just basic priority queue)
however, whats important is that the output needs to be the numbers of the assignments, not the priorities, so in the example above it would be 2, 4, 3
im a beginner so the only way i can think of to do this is using structs for each of the assignments, with two ints - one for number of assignment, one for its priority
the problem im having is with the input, if all i get is numbers then how can i make each struct recognizable and be able to sort it into a heap later? i thought of maybe creating temporary structs and then adding them to a list, but how can i pass the values to the temporary structs?
this is the code i have by now, i didnt do all the functions yet because first i want to be able to store and access the values properly
#include <stdio.h>
struct zadanie
{
int nr;
int priorytet;
};
int main()
{
int events;
scanf("%d", &events);
//struct zadanie heap[events];
int n = 1;
for (int i=0; i<events; i++)
{
int x;
scanf("%d", &x);
if (x != 0)
{
struct zadanie tmp;
&tmp.nr = n;
&tmp.priorytet = x;
n += 1;
}
else
{
}
}
}
i tried using these questions as a guide: Using scanf for multiple data with structures How do i assign a value using scanf to a member of a Struct type variable whose array is created dynamically but i couldnt work out how to make the answers work for me
*edit: i got the code to work for inserting the values, right now im having a problem with passing the structs inside of the array to a heapify function i know its probably something to do with pointers, but i played around with it for a bit and all i got was a "request for member ‘priorytet’ in something not a structure or union" error code
my code so far:
#include <stdio.h>
int size = 0;
struct zadanie
{
int nr;
int priorytet;
};
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void kopcenie(int heap[], int size, int i)
{
if (size == 1){
return;
}
else
{
int max = i;
int l = 2*i;
int p = 2*i+1;
if (l < size && heap[l].priorytet > heap[max].priorytet)
max = l;
if (p < size && heap[p].priorytet > heap[max].priorytet)
max = p;
if (max != i)
{
swap(&heap[i], &heap[max]);
kopcenie(heap, size, max);
}
}
}
int main()
{
int events;
scanf("%d", &events);
struct zadanie heap[events];
int n = 1;
for (int i=0; i<events; i++)
{
int x;
scanf("%d", &x);
if (x != 0)
{
struct zadanie tmp;
tmp.nr = n;
tmp.priorytet = x;
heap[n] = tmp;
n += 1;
}
}
}
**another edit - i got it to work, Passing an array of structs in C helped me a lot
i got some things working, however the answer is not right on some data, for example for 10 1 1 1 1 2 0 0 0 0 0 the answer should be 5 1 2 3 4 but for me its 1 5 2 3 4, any idea what might be wrong? here's my finished code: