今天给各位分享c语言优先级队列的知识,其中也会对编程实现一个优先级队列进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、C语言实现一个优先队列2、c语言中优先级队列如何实现优先级相同的元素先进先出??3、C语言/C++,基于优先级队列的拓扑排序。4、怎么用C语言实现多级反馈队列调度算法?5、c语言中优先级队列如何实现优先级相同的元素先进先出?
C语言实现一个优先队列
# include “stdio.h”
# include “malloc.h”
# include “stdlib.h”
typedef struct Queue
{
int data;
int Priority;
Queue * Next;
}* PQUEUE;
bool insert(PQUEUE p,int i, int j);
bool pop(PQUEUE p);
void sort(PQUEUE p);
int length = 0;
PQUEUE pT;
int main(void)
{
PQUEUE pH = (PQUEUE)malloc(sizeof(Queue));
insert(pH, 75, 8);
insert(pH, 54, 4);
insert(pH, 75, 6);
insert(pH, 23, 5);
insert(pH, 81, 4);
insert(pH, 65, 3);
insert(pH, 43, 4);
insert(pH, 34, 2);
sort(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
pop(pH);
return 0;
}
bool insert(PQUEUE p,int i, int j)
{
if(i= 0 i= 100 j=0 j=100)
{
PQUEUE pNew = (PQUEUE)malloc(sizeof(Queue));
if(length == 0)
{
pT = NULL;
}
if(pT == NULL)
{
pT = p;
}
if(NULL == pNew)
{
printf(“动态内存分配失败~!”);
exit(-1);
}
pNew-data = i;
pNew-Priority = j;
pT-Next = pNew;
pNew-Next = NULL;
pT = pNew;
length++;
return true;
}
return false;
}
PQUEUE p2;
bool pop(PQUEUE p)
{
if(length != 0)
{
p2 = p;
p = p-Next;
printf(“%d,”, p-data);
printf(“%d\n”, p-Priority);
p2-Next = p-Next;
length–;
return true;
}
return false;
}
void sort(PQUEUE p)
{
if(length != 0)
{
PQUEUE w,q;
int i, j, t1,t2;
for(i=0,w=p-Next; i length-1; ++i,w = w-Next)
{
for(j=i+1,q=w-Next; j length; ++j,q = q-Next)
{
if(w-Priority q-Priority)
{
t1 = w-data;
w-data = q-data;
q-data = t1;
t2 = w-Priority;
w-Priority = q-Priority;
q-Priority = t2;
}
}
}
}
return;
}
/*
都满足你的要求了,以上是使用链表结构的队列
*/
c语言中优先级队列如何实现优先级相同的元素先进先出??
每次插入的时候进行排序
比较的时候,相同情况下,把后进的放在后面
然后从前面出队就可以了
C语言/C++,基于优先级队列的拓扑排序。
#include iostream
#include vector
#include queue
#include string.h
using namespace std;
#define SIZE 100010
struct less_cmp
{
bool operator()(const int a, const int b)
{
return a b;
}
};
vectorint Vec[SIZE];
priority_queue int, vectorint, less_cmp Que;
int n, k, degree[SIZE];
void clear()
{
memset(degree, 0, sizeof(degree));
}
int main(int argc, char *argv[])
{
int i, beg, end;
while (1)
{
scanf(“%d %d”, n, k);
if (0 == n 0 == k)
break;
for (i=0 ; ik ; i++)
{
scanf(“%d %d”, beg, end);
Vec[beg].push_back(end);
degree
c语言优先级队列的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编程实现一个优先级队列、c语言优先级队列的信息别忘了在本站进行查找喔。
++;
}
/* 初始化,将所有入度为0的点加入优先级队列 */
for (i=1 ; i=n ; i++)
{
if (0 == degree[i])
Que.push(i);
}
int node;
vectorint::iterator iter_beg;
vectorint::iterator iter_end;
printf(“ORDER:”);
while (!Que.empty())
{
node = Que.top();
Que.pop();
printf(” %d”, node);
iter_end = Vec[node].end();
/* 将选出的节点的相邻节点入度减一,如果减后入度为0,入队列 */
for (iter_beg = Vec[node].begin() ; iter_beg != iter_end ; iter_beg++)
{
degree[*iter_beg]–;
if (0 == degree[*iter_beg])
Que.push(*iter_beg);
}
Vec[node].clear();
}
printf(“\n”);
clear();
}
}
Sample Input
5 6
1 4
4 3
1 5
5 3
1 3
2 4
0 0
Sample Output
ORDER: 1 2 4 5 3
怎么用C语言实现多级反馈队列调度算法?
调度算法的实施过程如下所述:(1)应设置多个就绪队列,并为各个队列赋予不同的优先级。(2)当一个新进程进入内存后,首先将它放入第一队列的末尾,按FCFS的原则排队等待调度。当轮到该进程执行时,如他能在该时间片内完成,便可准备撤离系统;如果它在一个时间片结束时尚未完成,调度程序便将该进程转入第二队列的末尾,再同样地按FCFS原则等待调度执行;如果它在第二队列中运行一个时间片后仍未完成,再依次将它放入第三队列……,如此下去,当一个长作业进程从第一队列依次降到第N队列后,在第N队列中便采取时间片轮转的方式运行
c语言中优先级队列如何实现优先级相同的元素先进先出?
最原始的办法:
1,用链表实现队列
2,队列的添加是从链表尾向前遍历
T* newNode;
T* tmp = (T*)tail;
while (tmp != NULL) {
if (newNode-pri tmp-pri) {
tmp = tmp-prev;
continue;
}
else if (newNode-pri = tmp-pri) {
newNode-next = tmp-next;
tmp-next-prev = newNode;
tmp-next = newNode;
newNode-prev = tmp;
}
}
3,取队列时从链表头开始取
c语言优先级队列的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于编程实现一个优先级队列、c语言优先级队列的信息别忘了在本站进行查找喔。