TASK:
I am working on a homework for my Structure and Algorithms college class. The task is to implement Priority Queue in C using the heap and form the main so it can support operations such as:
INSERT s (inserts string s in a Priority Queue)
DELETE (deletes the element with the lowest priority)
NUMBER (returns number of elements in a Priority Queue)
We should also print out preorder of the Priority Queue every time we write an operation in the main. We can assume that the heap is implemented with the pointers. So I implemented it in structure that looks like this:
typedef char* elementtype;
typedef struct celltag {
elementtype zapis;
struct celltag *left, *right, *parent;
} celltype;
typedef celltype* node;
typedef celltype* PriorityQueue;
I also implemented following functions:
void PrMakeNull(PriorityQueue *Ap)
int PrEmpty(PriorityQueue A)
void PrInsert(elementtype x, PriorityQueue *Ap)
elementtype PrDeleteMin(PriorityQueue *Ap)
void Preorder(PriorityQueue A)
QUESTION:
I should implement "number function" with the header like this:
int Number(PriorityQueue P)
The function should not change the Priority Queue A after calling Number(A). Also, the function should be independent on Priority Queue implementation. (I think that means I should only use PrMakeNull, PrEmpty, PrInsert, PrDeleteMin). Is that even possible? How could I do it? How should it look like?
WHAT HAVE I TRIED:
1)
//This one is independent on Priority Queue implementation but destroys the Priority Queue.
int Number(PriorityQueue P){
PriorityQueue S;
PrMakeNull(&S);
int counter=0;
while(!PrEmpty(P))
{
PrInsert(PrDeleteMin(&P), &S);
counter++;
}
while(!PrEmpty(S))
{
PrInsert(PrDeleteMin(&S), &P);
}
return counter;
}
2)
//This one is dependent on Priority Queue implementation but doesn't destroy the Priority Queue.
int Number(PriorityQueue A){
int returning_number=1;
if (A == NULL)
return 0;
if(A->left!=NULL)
returning_number+=Number(A->left);
if(A->right!=NULL)
returning_number+=Number(A->right);
return returning_number;
}