zoukankan      html  css  js  c++  java
  • 进程优先队列

    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
    

      

  • 相关阅读:
    C#终止线程的方法
    Socket编程(TCP、UDP)
    频率域滤波基础之五(读数字图像处理学习halcon)
    hihoCoder #1127:二分图最小点覆盖和最大独立集
    hihoCoder #1033 : 交错和 (数位Dp)
    HDU-5536 Chip Factory (字典树)
    hihoCoder #1040 (判断是否为矩形)
    hihoCoder:#1079(线段树+离散化)
    HDU-5532 Almost Sorted Array (LIS)
    UVALive-7303 Aquarium (最小生成树)
  • 原文地址:https://www.cnblogs.com/blacop/p/6556471.html
Copyright © 2011-2022 走看看