pqueue.h

来自「优先级队列类的实现」· C头文件 代码 · 共 63 行

H
63
字号

//利用数组实现的优先级队列
//也可用来排序噢 ^^
#ifndef PQUEUE
#define PQUEUE

#include <assert.h>
#include <iostream.h>
#include <stdlib.h>

const int maxPQSize=50;	    			//缺省元素个数

template <class Type>
class PQueue 
{
public:
	PQueue();							
    ~PQueue(){ delete[] pqelements; }
    void PQInsert(const Type &item);
    Type PQRemove();  
	void MakeEmpty(){ count=0;}
    bool IsEmpty()const { return count==0; }		
    bool IsFull()const { return count==maxPQSize; }	
    int Length()const { return count; }
private:
    Type *pqelements;				//存放数组
    int count;						//队列中元素个数	
};

template <class Type> 
PQueue<Type>::PQueue():count(0) 
{
    pqelements=new Type[maxPQSize];	
    assert(pqelements!=0);			  //分配断言
}

template <class Type> 
void PQueue<Type>::PQInsert(const Type &item)
{
    assert(!IsFull());				//判队满断言  
	pqelements[count]=item;	
	count++;
}

template <class Type>
Type PQueue<Type>::PQRemove() 
{
    assert(!IsEmpty());				//判队空断言
    Type min=pqelements[0];			
    int minindex=0;
    for(int i=1;i<count;i++)		//寻找最小元素
		if(pqelements[i]<min)      //存于min
		{
			min = pqelements[i];   
			minindex = i; 
		}
    pqelements[minindex]=pqelements[count-1];	
    count--;				  //删除		
    return min;
}


#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?