Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static priority queue, which is an array heap[i], and for example i/2 is the father of i and so on..
the code of the static priority queue is this:
So I created a .h file:
in which "i" is like the "i" of the static priority queue.
Here is the code of the main file(only insert):
I know it's kind of hard to read the whole code, but please I would appreciate it if you did. My university is closed (due to strike) and we didn't have any lessons for the past 2 months. I thank anyone who will reply and give me at least some clues, if what I'm doing is right or totally wrong. Thanks in advance!
Kind regards, AlarV
the code of the static priority queue is this:
Code:
int i= ++CurrentSize;
while(i != 1 && X > heap[i/2]) {
// cannot put x in heap [i]
heap[i] = heap[i/2]; //move element down
i /= 2; /move to parent
}
heap[i] = x;
Code:
#ifndef queuedef
#define queuedef
class Queue {
public:
int a;
int i;
class Queue *next;
Queue();
void insert(int a);
void deletetop();
int gettop();
void destroy();
void show();
};
#endif
Here is the code of the main file(only insert):
Code:
class Queue * gotofather(class Queue *temp1)
{
class Queue *temp;
temp=top;
while((temp->i!=temp1->i/2)&&(temp->i!=1))
temp=temp->next;
return temp;
}
void Queue::insert(int data)
{
class Queue *temp,*temp1;
int temp2;
temp=top;
top=(class Queue *) malloc(sizeof(class Queue));
top->a=data;
top->next=temp;
top->i=++CurrentSize;
temp=top;
temp1=gotofather(temp);
while((temp1->i!=1)&&(data>temp1->a))
{
temp->a=temp1->a; //heap[i]=heap[i/2]
temp1=gotofather(temp1); //i=i/2
temp=gotofather(temp); //i=i/2
}
temp->a=data;//heap[i]=x
}
Kind regards, AlarV
Comment