我不明白优先级队列的以下两个定义是否正确:
1
- 升序优先级队列 - 任意插入元素,但删除后删除最小元素(假设数据为整数)。
- 降序优先级队列 - 任意插入元素,但删除后,删除最大的元素(假设数据是整数)。
每个例子:
5 15 10 -> after dequeue() -> 15 10
15 5 10 -> after dequeue() -> 5 10
2
优先级队列的每个元素都有优先级,通过该优先级进行删除。 可能有两种情况。首先,删除具有最高优先级的元素。其次,删除最低优先级的元素。
显然,这与第一个定义不同。如果我们将优先级6,3,12
分配给数字15, 10, 5
,那么在dequeue()
操作之后会有两种情况。如果删除了最低优先级的元素,则队列为15,5 (10 is removed)
。如果删除了最高优先级的元素,则队列为15,10 (5 is removed)
。
另外,如果队列的元素不是数字(例如字符串),那么第一个定义就没用了。
这是对的吗?
问题:这两个定义都正确吗?在我看来,第一个只适用于数字,但即使这样,它也违反了第二个定义中的优先级。有人可以解释一下吗?
以下是C中两个定义的两个实现:
//1. DEFINITION//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int itemCount = 0;
int peek(){
return intArray[itemCount - 1];
}
bool isEmpty(){
return itemCount == 0;
}
bool isFull(){
return itemCount == MAX;
}
int size(){
return itemCount;
}
void insert(int data){
int i = 0;
if(!isFull()){
// if queue is empty, insert the data
if(itemCount == 0){
intArray[itemCount++] = data;
}else{
// start from the right end of the queue
for(i = itemCount - 1; i >= 0; i-- ){
// if data is larger, shift existing item to right end
if(data > intArray[i]){
intArray[i+1] = intArray[i];
}else{
break;
}
}
// insert the data
intArray[i+1] = data;
itemCount++;
}
}
}
int removeData(){
return intArray[--itemCount];
}
int main() {
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
int num = removeData();
printf("Element removed: %d\n",num);
return 0;
}
//2. DEFINITION//
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5 /* Size of Queue */
int f=0,r=-1; /* Global declarations */
typedef struct PRQ
{
int ele;
int pr;
}PriorityQ;
PriorityQ PQ[SIZE];
PQinsert(int elem, int pre)
{
int i; /* Function for Insert operation */
if( Qfull()) printf("\n\n Overflow!!!!\n\n");
else
{
i=r;
++r;
while(PQ[i].pr >= pre && i >= 0) /* Find location for new elem */
{
PQ[i+1]=PQ[i];
i--;
}
PQ[i+1].ele=elem;
PQ[i+1].pr=pre;
}
}
PriorityQ PQdelete()
{ /* Function for Delete operation */
PriorityQ p;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
p.ele=-1;p.pr=-1;
return(p); }
else
{
p=PQ[f];
f=f+1;
return(p);
}
}
int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}
int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}
display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("[%d,%d] ",PQ[i].ele,PQ[i].pr);
printf("<-Rear");
}
}
main()
{ /* Main Program */
int opn;
PriorityQ p;
do
{
printf("\n ### Priority Queue Operations(DSC order) ### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element and its Priority?");
scanf("%d%d",&p.ele,&p.pr);
PQinsert(p.ele,p.pr); break;
case 2: p=PQdelete();
if( p.ele != -1)
printf("\n\nDeleted Element is %d \n",p.ele);
break;
case 3: printf("\n\nStatus of Queue\n\n");
display(); break;
case 4: printf("\n\n Terminating \n\n"); break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}
答案 0 :(得分:2)
priority queue是一个包含元素(与任何数据结构一样)的数据结构及其优先级。这是你的第二个定义。
但是,在某些情况下,元素实际上代表了他们自己的优先级。这是您的第一个定义:有时,您只需要存储一堆无序数字并按顺序检索它们。注意,在这种情况下,元素不一定是数字。其他数据类型可能具有可用作优先级的属性。