优先级队列是一种特殊类型的队列,其中每个元素都是与优先级相关联,并根据其优先级提供服务。如果元素有相同的优先级,那么根据它们在队列中的排列顺序。
元素本身的值可以用于分配优先级。例如:最高值的元素被视为最高的优先级元素。但是,在其他情况下,我们可以假设最小值的元素作为最高优先级元素。在其他情况下,我们可以根据我们的需要确定优先级。
在队列中,根据先进先出规则,而在优先级队列中,则根据优先级删除这些值。
#include <stdio.h>
#include <stdlib.h>
#define NULL ((void *)0)
struct node
{
int data;
struct node *next;
};
struct node *front, *rear;
/* 初始化队列*/
void createqueue() { front = rear = NULL; }
int empty()
{
if (front == NULL)
return 1;
else
return 0;
}
void insert(int x)
{
struct node *pnode;
pnode = (struct node *)malloc(sizeof(struct node));
if (pnode == NULL)
{
printf("Memory overflow. Unable to insert.\n");
exit(1);
}
pnode->data = x;
pnode->next = NULL; /*新增节点总是最后一个节点*/
if (empty())
front = rear = pnode;
else
{
rear->next = pnode;
rear = pnode;
}
}
int removes()
{
int min;
struct node *follow, *follow1, *p, *p1;
if (empty())
{
printf("\nQueue Underflow. Unable to remove.");
exit(1);
}
/* 在优先级队列中找到最小值的节点*/
p = p1 = front;
follow = follow1 = NULL;
min = front->data;
while (p != NULL)
{
if (p->data < min)
{
min = p->data;
follow1 = follow;
p1 = p;
}
follow = p;
p = p->next;
}
/* 根据最小值删除节点 */
if (p1 == front) /* 删除第一个节点.*/
{
front = front->next;
if (front == NULL) /* 删除唯一的一个节点 */
rear = NULL;
}
else if (p1 == rear) /* 删除最后一个节点 */
{
rear = follow1;
rear->next = NULL;
}
else /* 删除其它节点*/
follow1->next = p1->next;
free(p1);
return min;
}
void show()
{
struct node *p;
if (empty())
printf("Queue empty. No data to display \n");
else
{
printf("Queue from front to rear is as shown: \n");
p = front;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
void destroyqueue() { front = rear = NULL; }
int main()
{
int x, ch;
createqueue();
do
{
printf("\n******Menu******\n");
printf("1:Insert \n");
printf("2:Remove \n");
printf("3:exit \n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter element to be inserted: ");
scanf("%d", &x);
insert(x);
show();
break;
case 2:
x = removes();
printf("Element removed is: %d\n", x);
show();
break;
case 3:
break;
}
} while (ch != 3);
destroyqueue();
return 0;
}
运行结果:
插入元素
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 12
Queue from front to rear is as shown:
12
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 10
Queue from front to rear is as shown:
12 10
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 3
Queue from front to rear is as shown:
12 10 3
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 90
Queue from front to rear is as shown:
12 10 3 90
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 66
Queue from front to rear is as shown:
12 10 3 90 66
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 88
Queue from front to rear is as shown:
12 10 3 90 66 88
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 89
Queue from front to rear is as shown:
12 10 3 90 66 88 89
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 18
Queue from front to rear is as shown:
12 10 3 90 66 88 89 18
根据优先级删除,最小值具有高优先级被删除。
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 3
Queue from front to rear is as shown:
12 10 90 66 88 89 18
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 10
Queue from front to rear is as shown:
12 90 66 88 89 18
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 12
Queue from front to rear is as shown:
90 66 88 89 18
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 18
Queue from front to rear is as shown:
90 66 88 89
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 66
Queue from front to rear is as shown:
90 88 89
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 88
Queue from front to rear is as shown:
90 89
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 89
Queue from front to rear is as shown:
90
******Menu******
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 90
Queue empty. No data to display
大家好!我是编码小哥,欢迎关注,持续分享更多实用的编程经验和开发技巧,共同进步。