博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
进程优先队列
阅读量:7191 次
发布时间:2019-06-29

本文共 1672 字,大约阅读时间需要 5 分钟。

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace StackQueue.algo {    //进程优先队列    public struct pqItem {        //通常会把存储在优先队列中的数据项作为键值对来构造,其中关键字就是指优先级别,而值则用来识别数据项。        //例如,可以按照如下形式对一个操作系统进程进行定义:        public int priority;        public string name;    }//public struct pqItem    public class PQueue : Queue {        //大家不能把未修改的 Queue 对象用于优先队列。 DeQueue 方法在被调用时只会把队列中的第一个数据项移除。        //但是,大家可以从 Queue 类派生出自己的优先队列类,同时覆盖 DeQueue 方法来实现自己的需求。        //大家把这个类称为 PQueue。所有 Queue 的方法都可以照常使用,同时覆盖 Dequeue 方法来移除具有最高优先        //级的数据项。为了不从队列前端移除数据项,首先需要把队列的数据项写入一个数组。然后遍历整个数组从而找到        //具有最高优先级的数据项。最后,根据标记的数据项,就可以在不考虑此标记数据项的同时对队列进行重新构建。        //下面就是有关 PQueue 类的代码:        public PQueue() { } //构造器        public override object Dequeue() {             object[] items;            int min;            items = this.ToArray(); //this转成数组            min = ((pqItem)items[0]).priority; //找到最高优先级item            for (int x = 1; x <= items.GetUpperBound(0); x++) //遍历//找到最高优先级item                if (((pqItem)items[x]).priority < min) {                     min = ((pqItem)items[x]).priority; //标记最高优先级item                }            this.Clear(); //清空this数组            int x2;            for (x2 = 0; x2 <= items.GetUpperBound(0); x2++)                if (((pqItem)items[x2]).priority == min && ((pqItem)items[x2]).name != "")  //遍历//找到最高优先级item                    this.Enqueue(items[x2]); //将 最高优先级item 入队            return base.Dequeue(); //出队        } //重写Dequeue()方法    }//public class PQueue : Queue}//namespace StackQueue.algo

  

转载于:https://www.cnblogs.com/blacop/p/6556471.html

你可能感兴趣的文章
MySQL基础之 标准模式通配符
查看>>
聊一聊python的单例模式
查看>>
第十一篇、RxSwift
查看>>
复分析学习9——全纯函数各阶导数在紧集上的一致估计
查看>>
run_test() 验证平台的入口
查看>>
PHP网站,两个域名在一个空间,如何做301转向
查看>>
Mysql系列五:数据库分库分表中间件mycat的安装和mycat配置详解
查看>>
Web References - There was an error downloading 'http://localhost:/xxx/xxx.asmx'
查看>>
Python之禅及释义
查看>>
laravel5.4 开发简书网站
查看>>
设置类库项目的程序集名称和默认命名空间
查看>>
对属性NaN的理解纠正和对Number.isNaN() 、isNaN()方法的辨析
查看>>
【转】iOS lame编译 arm64 armv7s armv7 x86_64 i386指令集
查看>>
LeetCode-二叉树的最大深度
查看>>
Linux内核剖析(五)Linux内核的构建过程
查看>>
19、生鲜电商平台-安全设计与架构
查看>>
Django_06_项目完成
查看>>
寻找子字符串int find_substr(char *s1, char *s2)
查看>>
Manifest.xml中不要出现重复的uses-permission声明
查看>>
UFS文件系统简明学习笔记
查看>>