the code of the static priority queue is this:
Expand|Select|Wrap|Line Numbers
- 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;
Expand|Select|Wrap|Line Numbers
- #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):
Expand|Select|Wrap|Line Numbers
- 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