
c语言中优先级队列
Here you will get implementation of priority queue in C and C++ with program example.
在这里,您将通过程序示例在C和C ++中获得优先级队列的实现。
Priority Queue is an ordered list of homogeneous elements. In normal queue, service is provided on the basis of First-In-First-Out. In a priority queue service isn’t provided on the basis of First-In-First-Out service, but rather then each element has a priority based on the urgency of the need.
优先级队列是同类元素的有序列表。 在普通队列中,服务基于先进先出的原则提供。 在优先级队列服务中,不是基于先进先出服务来提供的,而是根据需求的紧迫性,每个元素都具有优先级。
- An element with higher priority is processed before other elements with lower priority. 优先级较高的元素将在其他优先级较低的元素之前处理。
- Elements with the same priority are processed on First-In-First-Out service basis. 具有相同优先级的元素将根据先进先出服务进行处理。
An example of priority queue is a hospital waiting room. A patient having a more fatal problem would be admitted before other patients.
优先队列的一个示例是医院候诊室。 具有更致命问题的患者将在其他患者之前入院。
Other applications of priority queues are found in long term scheduling of jobs processed in a computer. In practice, short processes are given a priority over long processes as it improves the average response of the system.
在对计算机中处理的作业进行长期调度时,可以找到优先级队列的其他应用程序。 实际上,短流程比长流程具有更高的优先级,因为它可以改善系统的平均响应。
Priority Queue can be implemented using a circular array.
优先队列可以使用循环数组来实现。
As the service must be provided to an element having highest priority, there could be a choice between:
由于必须将服务提供给具有最高优先级的元素,因此可以在以下选项之间进行选择:
- List is always maintained sorted on priority of elements with the highest priority element at the front. Here, deletion is trivial but insertion is complicated as the element must be inserted at the correct place depending on its priority. 列表始终按优先级最高的元素排在最前面。 在这里,删除是微不足道的,但是插入是复杂的,因为必须根据元素的优先级将其插入正确的位置。
- List is maintained in the FIFO form but the service is provided by selecting the element with the highest priority. Deletion is difficult as the entire queue must be traversed to locate the element with the highest priority. Here, insertion is trivial (at the rear end). 列表以FIFO形式维护,但是通过选择优先级最高的元素来提供服务。 删除是困难的,因为必须遍历整个队列才能找到具有最高优先级的元素。 在这里,插入很简单(在后端)。
C语言中的优先队列程序 (Program for Priority Queue in C)
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
typedef struct pqueue
{
int data[MAX];
int rear,front;
}pqueue;
void initialize(pqueue *p);
int empty(pqueue *p);
int full(pqueue *p);
void enqueue(pqueue *p, int x);
int dequeue(pqueue *p);
void print(pqueue *p);
void main()
{
int x,op,n,i;
pqueue q;
initialize(&q);
do
{
printf("\n1)Create \n2)Insert \n3)Delete \n4)Print \n5)EXIT");
printf("\nEnter Choice: ");
scanf("%d",&op);
switch (op) {
case 1: printf("\nEnter Number of Elements");
scanf("%d",&n );
initialize(&q);
printf("Enter the data");
for(i=0; i<n; i++)
{
scanf("%d",&x);
if(full(&q))
{
printf("\nQueue is Full..");
exit(0);
}
enqueue(&q,x);
}
break;
case 2: printf("\nEnter the element to be inserted");
scanf("%d\n",&x);
if(full(&q))
{
printf("\nQueue is Full");
exit(0);
}
enqueue(&q,x);
break;
case 3: if(empty(&q))
{
printf("\nQueue is empty..");
exit(0);
}
x=dequeue(&q);
printf("\nDeleted Element=%d",x);
break;
case 4: print(&q);
break;
default: break;
}
}while (op!=5);
}
void initialize(pqueue *p)
{
p->rear=-1;
p->front=-1;
}
int empty(pqueue *p)
{
if(p->rear==-1)
return(1);
return(0);
}
int full(pqueue *p)
{
if((p->rear+1)%MAX==p->front)
return(1);
return(0);
}
void enqueue(pqueue *p, int x)
{
int i;
if(full(p))
printf("\nOverflow");
else
{
if(empty(p))
{
p->rear=p->front=0;
p->data[0]=x;
}
else
{
i=p->rear;
while(x>p->data[i])
{
p->data[(i+1)%MAX]=p->data[i];
i=(i-1+MAX)%MAX; //anticlockwise movement inside the queue
if((i+1)%MAX==p->front)
break;
}
//insert x
i=(i+1)%MAX;
p->data[i]=x;
//re-adjust rear
p->rear=(p->rear+1)%MAX;
}
}
}
int dequeue(pqueue *p)
{
int x;
if(empty(p))
{
printf("\nUnderflow..");
}
else
{
x=p->data[p->front];
if(p->rear==p->front) //delete the last element
initialize(p);
else
p->front=(p->front +1)%MAX;
}
return(x);
}
void print(pqueue *p)
{
int i,x;
if(empty(p))
{
printf("\nQueue is empty..");
}
else
{
i=p->front;
while(i!=p->rear)
{
x=p->data[i];
printf("\n%d",x);
i=(i+1)%MAX;
}
//prints the last element
x=p->data[i];
printf("\n%d",x);
}
}
Output
输出量
1)Create 2)Insert 3)Delete 4)Print 5)EXIT Enter Choice: 1
1)创建 2)插入 3)删除 4)打印 5)退出 输入选择:1
Enter Number of Elements4 Enter the data9 12 4 6
输入元素数 4 输入数据 9 12 4 6
1)Create 2)Insert 3)Delete 4)Print 5)EXIT Enter Choice: 4
1)创建 2)插入 3)删除 4)打印 5)退出 输入选择:4
12 9 6 4 1)Create 2)Insert 3)Delete 4)Print 5)EXIT Enter Choice: 3
12 9 6 4 1)创建 2)插入 3)删除 4)打印 5)退出 输入选择:3
Deleted Element=12 1)Create 2)Insert 3)Delete 4)Print 5)EXIT Enter Choice: 5
删除的元素= 12 1)创建 2)插入 3)删除 4)打印 5)退出 输入选择:5
C ++中的优先队列程序 (Program for Priority Queue in C++)
#include <iostream>
#include <stdlib.h>
#define MAX 30
using namespace std;
typedef struct pqueue
{
int data[MAX];
int rear,front;
}pqueue;
void initialize(pqueue *p);
int empty(pqueue *p);
int full(pqueue *p);
void enqueue(pqueue *p, int x);
int dequeue(pqueue *p);
void print(pqueue *p);
int main()
{
int x,op,n,i;
pqueue q;
initialize(&q);
do
{
cout<<"\n1)Create \n2)Insert \n3)Delete \n4)Print \n5)EXIT";
cout<<"\nEnter Choice: ";
cin>>op;
switch (op) {
case 1: cout<<"\nEnter Number of Elements";
cin>>n;
initialize(&q);
cout<<"Enter the data";
for(i=0; i<n; i++)
{
cin>>x;
if(full(&q))
{
cout<<"\nQueue is Full..";
exit(0);
}
enqueue(&q,x);
}
break;
case 2: cout<<"\nEnter the element to be inserted";
cin>>x;
if(full(&q))
{
cout<<"\nQueue is Full";
exit(0);
}
enqueue(&q,x);
break;
case 3: if(empty(&q))
{
cout<<"\nQueue is empty..";
exit(0);
}
x=dequeue(&q);
cout<<"\nDeleted Element="<<x;
break;
case 4: print(&q);
break;
default: break;
}
}while (op!=5);
return 0;
}
void initialize(pqueue *p)
{
p->rear=-1;
p->front=-1;
}
int empty(pqueue *p)
{
if(p->rear==-1)
return(1);
return(0);
}
int full(pqueue *p)
{
if((p->rear+1)%MAX==p->front)
return(1);
return(0);
}
void enqueue(pqueue *p, int x)
{
int i;
if(full(p))
cout<<"\nOverflow";
else
{
if(empty(p))
{
p->rear=p->front=0;
p->data[0]=x;
}
else
{
i=p->rear;
while(x>p->data[i])
{
p->data[(i+1)%MAX]=p->data[i];
i=(i-1+MAX)%MAX; //anticlockwise movement inside the queue
if((i+1)%MAX==p->front)
break;
}
//insert x
i=(i+1)%MAX;
p->data[i]=x;
//re-adjust rear
p->rear=(p->rear+1)%MAX;
}
}
}
int dequeue(pqueue *p)
{
int x;
if(empty(p))
{
cout<<"\nUnderflow..";
}
else
{
x=p->data[p->front];
if(p->rear==p->front) //delete the last element
initialize(p);
else
p->front=(p->front +1)%MAX;
}
return(x);
}
void print(pqueue *p)
{
int i,x;
if(empty(p))
{
cout<<"\nQueue is empty..";
}
else
{
i=p->front;
while(i!=p->rear)
{
x=p->data[i];
cout<<"\n"<<x;
i=(i+1)%MAX;
}
//prints the last element
x=p->data[i];
cout<<"\n"<<x;
}
}
Comment below if you have queries or found any information incorrect in above tutorial for priority queue in C and C++.
如果您有疑问或在上面的教程中对C和C ++中的优先级队列有任何不正确的信息,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2017/06/priority-queue-c-c.html
c语言中优先级队列